Topic 1: Pre-defined and User-defined Functions
CP125
Table of Contents (TOC)
CP125
Type of function | |
|
|
Definition of pre-defined functions | Definition of user-defined functions |
|
|
CP125
TYPE OF FUNCTIONS
PRE-DEFINED FUNCTIONS
USER-DEFINED FUNCTIONS
def my_function():
print("Hello from a function")
my_function()
Function
Definition
Function Call
CP125
Advantages of using functions
CP125
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
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
Python has a set of pre-defined functions
For more information, visit https://www.w3schools.com/python/python_ref_functions.asp
CP125
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
● 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
How user-defined function works
Figure 1 shows
Figure 1
CP125
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
How to create a user-defined function
Write Function Call
Step 2
Create Function Definition
Step 1
def message():
print("Enter a value: ")
print("We start here.")
message()
print("We end here.")
Example of a program
CP125
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
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!")
Summary
CP125