2. LECTURE RECAP
Topics covered so far:
Lecture 1:
◦ Course Information & Introduction to Python Programming.
◦ Input, processing and Output
Lecture 2:
◦ Decision Making and Loop
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 2
3. LECTURE 3
Topic to cover:
Functions
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 3
4. LECTURE OBJECTIVES
To understand why programmers divide programs up into sets of
cooperating functions.
To be able to define new functions in Python.
To understand the details of function calls and parameter passing in
Python.
To write programs that use functions to reduce code duplication and
increase program modularity.
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 4
5. Functions
The programs that we have written so far comprise a single function,
usually called main.
A function is a group of statements that exist within a program for the
purpose of performing a specific task.
Instead of writing a large program as one long sequence of statements, it
can be written as several small functions, each one performing a specific
part of the task.
These small functions can then be executed in the desired order to
perform the overall task.
A program that has been written with each task in its own function is
called a modularized program.
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 5
6. Benefits of Modularizing a Program with Functions
Simpler Code
Code Reuse
Better Testing
Faster Development
Easier Facilitation of Teamwork
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 6
7. Defining and Calling a Function
The code for creating a function is known as a function definition.
To execute the function, you write a statement that calls it.
The general format of a function definition in Python:
o def function_name():
o statement
o statement
o etc.
The first line is known as the function header. It marks the beginning of the function
definition.
The function header begins with the key word def, followed by the name of the function,
followed by a set of parentheses, followed by a colon.
Beginning at the next line is a set of statements known as a block (a set of statements that
belong together as a group).
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 7
8. A call to a function initiates a four step process:
The calling program is suspended.
The values of actual parameters are assigned to the formal parameters.
The body of the function is executed.
Control returns immediately following the function call in the calling program.
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 8
10. Exercise 2: More interesting…
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 10
11. Local Variables
A local variable is created inside a function and cannot be
accessed by statements that are outside the function.
Different functions can have local variables with the same
names because the functions cannot see each other’s local
variables.
Anytime you assign a value to a variable inside a function, you
create a local variable.
The term local is meant to indicate that the variable can be
used only locally, within the function in which it is created.
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 11
12. Exercise 3: Using Local Variable
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 12
13. Passing Arguments to Functions
An argument is any piece of data that is passed into a function
when the function is called.
A parameter is a variable that receives an argument that is
passed into a function.
Sometimes it is useful not only to call a function, but also to
send one or more pieces of data into the function.
Pieces of data that are sent into a function are known as
arguments.
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 13
18. Assignment
1. There are three seating categories at a stadium. For a football game, Class A seats cost
N2000, Class B seats cost N1000, and Class C seats cost N500. Write a program that asks
how many tickets for each class of seats were sold, and then displays the amount of
income generated from ticket sales.
2. Write a program that calculates and displays a person’s body mass index (BMI). The BMI
is often used to determine whether a person is overweight or underweight for his or her
height. A person’s BMI is calculated with the following formula:
BMI = weight x 703 / height
where weight is measured in kg and height is measured in meters.
N.B: Use Function for each task.
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 18