Topic 1: Pre-defined and User-defined Functions
(c) Identify the components of a function:�
(1 hour)
CP125
Table of Contents (TOC)
i. Function Header
ii. Function Body
iii. Function Call
CP125
Introduction to Namespace
## Function space ##
def hi():
print("hi!")
CP125
## Main space ##
hi()
i. Function Header
ii. Function Body
(indented)
Three components of a function:
i. Function Header
ii. Function Body
iii. Function Call
def greet ( ) :
print (“Hello World!”)
greet( )
iii. Function Call/ invoke
Function Definition
CP125
https://wayground.com/join/quiz/6933f1a86ec2d3942ead0fb9?studentShare=true
Four Elements in Function Header
(2) Function name �(define by user or built in function)
(4) colon (:) at the end of function header
def greet ( ) :
(3) parentheses ( ) with and without parameter/ input values
i. Function Header
CP125
Examples of Function Header
Function header without parameter
Function header with parameter
CP125
def find_total(a, b):
def display_lect(name):
def find_avg(no1, no2, no3):
def status():
def message():
Exercise 1�
CP125
Exercise 1.1 �
(i) Create a function header named determine_grade without parameter.
def _________ _
(ii) Create a function header named display_message without parameter.
_______ _______ () :
CP125
Exercise 1.1 �
(iii) Create a function header named calc_total that accepts two parameters, a and b.
_______ total(___, b):
(iv) Create a function header named calc_sum that accepts two parameters named num1 and num2.
___ ________ (______, ________) :
CP125
(i) This function named display_msg() is to display “Welcome back”. Identify the errors from this snippet below.
Exercise 1.2
CP125
## create function ##
def display_msg();
print("Welcome back")
## main ##
msg()
(i) This function named display_msg() is to display “Welcome back”. Identify the errors from this snippet below.
Exercise 1.2
CP125
(ii) This function named display_msg() is to display “Welcome back”. Identify the error from this snippet below.
Exercise 1.2
CP125
## create function ##
def display_msg():
print("Welcome back")
## main ##
display_msg()
(iii) This function named display_msg() is to display “Welcome back”. Identify the error from this snippet below.
Exercise 1.2
CP125
## create function ##
def display_msg:
print("Welcome back")
## main ##
display_msg()
(iv) This function named display_msg() is to display “Welcome back”. Identify the error from this snippet below.
Exercise 1.2
CP125
## create function ##
defdisplay_msg():
print("Welcome back")
## main ##
display_msg()
ii. Function Body
CP125
ii) Function Body (without or with return values)
(no return keyword)
CP125
Example of Function Body
return value (no return keyword)
(b) Function body with
return value (must have return keyword)
Function body
Function body with return keyword
Function body
Function body with return keyword
def calcAvg(num1,num2,num3):
total = num1 + num2 + num3
avg = total/3
return avg
def greet(message):
return message
CP125
iii) A. Function Call
CP125
iii) A. Function Call - continued
CP125
Arguments
Parameters
Function call
Example Function Call Without Arguments
function call (name must be the same as function name)
function header
function body
function space
main program space
CP125
iii) B. How the program execute when the function is called
CP125
iii) C. Function Call without argument(s)
1.1 Without return value
1.2 With return value
CP125
Return value is the result that a function sends back to the main space after it finishes running.
function call
1.1 Without return value
Output:
CP125
1.2 With return value
CP125
## create function ##
def get_greeting():
return "Hello, welcome back!"
## main ##
print(get_greeting())
## create function ##
def get_greeting():
return "Hello, welcome back!"
## main ##
message = get_greeting()
print(message)
Output:
*Direct print from the returned value
*Store returned value in a variable to be used later
iii) D. Function Call with argument(s)
1.1 Without return value
1.2 With return value
CP125
iii) D. Function Call with argument(s)
2.1 Without return value
2.2 With return value
function call with argument
Output:
function call
with arguments
Output:
CP125
Parameter vs Argument
CP125
Recap
CP125
parameters�(variable)
2, 3
4, 20
length, width
arguments
(values)
Output:
CP125
parameters used in function header
arguments used in function call
CP125
Output:
we could assign function call to a variable
CP125
Exercise 1�
Write a function to calculate area of a rectangle (with and without return value)
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
area = length * width
print("The area of the rectangle is:", area)
CP125
Exercise 2�
CP125
(i) Create a function header named bmi()that accepts two
parameters named height and weight.
(ii) Call the previous function by using the values 1.53 and
55 for height and weight respectively. (Assume there is
a return values)
Exercise 2.1
CP125
(i) Create a function header named sum()that accepts
three integers value named num1, num2 and num3.
(ii) Call the previous function by using the values 10, 20
and 30 for num1, num2, and num3 respectively.
(Assume there is a return values)
(iii) Identify the arguments and parameters from (i) and (ii)
Exercise 2.2
CP125
Summary
i. Function Header
ii. Function Body
iii. Function Call
CP125