1 of 36

Topic 1: Pre-defined and User-defined Functions

  • Learning outcomes:�

(c) Identify the components of a function:�

      • Function Header
      • Function Body
      • Function Call

(1 hour)

CP125

2 of 36

Table of Contents (TOC)

  • Introduction to Namespace
  • Three components of a function

i. Function Header

ii. Function Body

iii. Function Call

  1. Example Function Call
  2. How the program execute when the function is called
  3. Function Call without argument(s)
  4. Function Call with argument(s)
  5. Parameter vs argument

CP125

3 of 36

Introduction to Namespace

​

## Function space ##

def hi():

print("hi!")

​

CP125

  • Global space refers to the namespace that is outside of the function space.
  • In this course we use the term global space and main space interchangeably.
  • Moving forward, the main space is the term that we will use.

​

## Main space ##

hi()

​

4 of 36

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

5 of 36

​

Four Elements in Function Header

​

  1. def keyword is used at the start of 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

6 of 36

​

Examples of Function Header

​

Function header without parameter

Function header with parameter

CP125

  • The function header name is using snake_case naming convention
  • Parameter - Input value(s) for the function to process.
  • Example - no1, no2 and no3 that will be processed to find the average

​

​

def find_total(a, b):

​

def display_lect(name):

​

def find_avg(no1, no2, no3):

def status():

​

def message():

​

​

7 of 36

Exercise 1�

​

CP125

8 of 36

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

9 of 36

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

10 of 36

(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()

​

​

11 of 36

(i) This function named display_msg() is to display “Welcome back”. Identify the errors from this snippet below.

​

Exercise 1.2

CP125

12 of 36

(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()

​

​

13 of 36

(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()

​

​

14 of 36

(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()

​

​

15 of 36

ii. Function Body

​

  • The body of function consists of one or more valid statements.(Eg: to calculate max, to print value)

​

  • Each statement must be indented with the same indentation to form a block.

​

​

CP125

16 of 36

ii) Function Body (without or with return values)

​

  1. Function body without return value

(no return keyword)

  1. Function body with return value �(must have return keyword)

CP125

17 of 36

​

Example of Function Body

​

  1. Function body without

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

18 of 36

iii) A. Function Call

​

  • Function without function call is meaningless.

​

  • Therefore, we need to create function call.

​

  • To call a function, use the function name followed by parenthesis(), with or without arguments.

​

​

​

CP125

19 of 36

iii) A. Function Call - continued

​

  • Once the function is invoked (called) in main program space, the parameters inside the parentheses () is called arguments.
  • The number of arguments must match with parameters of the function.
  • Arguments must have values

​

CP125

Arguments

Parameters

Function call

20 of 36

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

21 of 36

  1. When the function greet() is called from the main space, the program's control transfers to the function definition in function space.
  2. All the code inside the function is executed.

iii) B. How the program execute when the function is called

CP125

22 of 36

iii) C. Function Call without argument(s)

​

  1. 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.

23 of 36

​

  1. Function Call without argument(s)

​

function call

1.1 Without return value

Output:

​

CP125

24 of 36

​

  1. Function Call without argument(s)

​

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

25 of 36

iii) D. Function Call with argument(s)

​

​

  1. Function call with argument(s)

1.1 Without return value

1.2 With return value

CP125

26 of 36

​

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

27 of 36

Parameter vs Argument

​

  • Parameter: in function header (function space)
  • Argument: in function call (main program space)�

​

CP125

28 of 36

Recap

​

​

CP125

29 of 36

parameters�(variable)

2, 3

​

4, 20

​

length, width

​

arguments

(values)

Output:

​

CP125

30 of 36

parameters used in function header

arguments used in function call

CP125

31 of 36

Output:

​

we could assign function call to a variable

CP125

32 of 36

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

33 of 36

Exercise 2�

​

CP125

34 of 36

(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

35 of 36

(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

36 of 36

Summary

  • Introduction to Namespace
  • Three components of a function

i. Function Header

ii. Function Body

iii. Function Call

  1. Example Function Call
  2. How the program execute when the function is called
  3. Function Call without argument(s)
  4. Function Call with argument(s)
  5. Parameter vs argument

​

CP125