1 of 16

Topic 1: Pre-defined and User-defined Functions

  • Learning outcomes:�
    • (a) Identify concepts of functions. (1 hour)

CP125

2 of 16

Table of Contents (TOC)

  • Type of functions
  • Definition of pre-defined function and user-defined functions
  • Advantages of using functions
  • Reusable feature
  • Example of pre-defined functions
  • Example of user-defined functions
  • The characteristics of a user-defined functions
  • How user-defined function works
  • How to create user-defined function
  • Exercise

CP125

3 of 16

Type of function

  • pre-defined functions
  • User-defined functions

Definition of pre-defined functions

Definition of user-defined functions

  • A function that are readily available for use
  • A function that is defined by a programmer

CP125

4 of 16

TYPE OF FUNCTIONS

PRE-DEFINED FUNCTIONS

USER-DEFINED FUNCTIONS

  • Definition: A function that are readily available for use.
  • Definition: A function that is defined by a programmer.
  • Created by the user to perform a specific task or to solve problems.
  • Example:
  • Example :
  • input() – allowing user input
  • print() – prints to the standard output device
  • pow() – returns the value of x to the power of y
  • max() – returns the largest item in an iterable

def my_function():

print("Hello from a function")

​

my_function()

Function

Definition

Function Call

CP125

5 of 16

  • Function makes your code reusable, better organized, and more readable.
  • A function is a set of statements so they can be run more than once in a program.

Advantages of using functions

CP125

6 of 16

Advantages of using functions: Reusable feature

Without function

User-defined function

a,b = 10,5

average = (a+b)/2

print("Average is: ", average)

​

c,d =100,5

average = (c+d)/2

print("Average is: ", average)

​

e,f =100,50

average = (e+f)/2

print("Average is: ", average)

​

g,h = 9,50

average = (g+h)/2

print("Average is: ", average)

def Average(a,b):

average = (a+b)/2;

print("Average is: ", average)

​

Average(10,5)

Average(100,5)

Average(100,50)

Average(9,50)

CP125

7 of 16

print("Hello, World!")

input('Enter your name:')

x = pow(4, 3)

a = (1, 2, 3, 4, 5)

x = sum(a)

Example of pre-defined functions

​

print() function to display String

input() function allows user input

sum() function returns a value of the sum

pow( ) function returns the value of x to the power of y

CP125

8 of 16

Python has a set of pre-defined functions

For more information, visit https://www.w3schools.com/python/python_ref_functions.asp

CP125

9 of 16

def message():

print("Hello")

message()

def my_function(fname):

print(fname + " Refsnes")

​

my_function("Emil")

my_function("Tobias")

my_function("Linus")

Example 1

Example 2

CP125

10 of 16

● It groups a block of code together so that we can call it by name.

​

● It enables us to pass values into the the function when we call it.

​

● It can return a value (even if None).

​

● When a function is called, it has its own namespace (function space). Variables in

the function are local to the function only (and disappear when the function exits).

​

​

The characteristics of a user-defined functions

CP125

11 of 16

How user-defined function works

Figure 1 shows

  • when you invoke a function, Python remembers the place where it happened and jumps into the invoked function;
  • the body of the function is then executed;
  • reaching the end of the function forces Python to return to the place directly after the point of invocation.

Figure 1

CP125

12 of 16

  • In Python, calling a function before declaring it is not possible.
  • Unlike compiled programming languages (such as Java), Python is an interpreted language, it does not allow invoking/ called a function before its definition.
  • The order of function definitions matters, and you must define a function before you can call it.
  • This is considered as a runtime error

def message():

print("Enter a value: ")

print("We start here.")

message()

print("We end here.")

print("We start here.")

message()

print("We end here.")

def message():

print("Enter a value: ")

Function

Definition

Function Call

Function

Definition

Function

Call

Calling a function before declaring it is not possible

How user-defined function works

CP125

13 of 16

How to create a user-defined function

Write Function Call

Step 2

Create Function Definition

  • Function Header
  • Function Body

Step 1

def message():

print("Enter a value: ")

print("We start here.")

message()

print("We end here.")

Example of a program

CP125

14 of 16

Exercise

Question 1: The input() function is an example of a:

a) user-defined function

b) pre-defined function

Question 2: The following snippet is an example of a:

​

​

a) user-defined function

b) pre-defined function

def message():

print("Hello")

message()

CP125

15 of 16

Exercise

Question 4: The following snippet is an example of a:

​

a) user-defined function

b) pre-defined function

Question 3: What happens when you try to invoke a function before you define it?

x = pow(4, 3)

print(x)

CP125

hi()

​

def hi():

print("hi!")

16 of 16

Summary

  • Type of functions
  • Definition of pre-defined function and user-defined functions
  • Advantages of using functions
  • Reusable feature
  • Example of pre-defined functions
  • Example of user-defined functions
  • The characteristics of a user-defined functions
  • How user-defined function works
  • How to create user-defined function
  • Exercise

CP125