CS22-OOP2(Object Oriented Programming 2)
College of Computer Studies
I. INTRODUCTION
In this lesson.
II. OBJECTIVES
1. Define what a function is in Python. Explain
Module 7 the importance of using functions for code
organization and reusability. Understand the
syntax for defining and calling functions.
FUNCTIONS 2. Explore the concept of function parameters
and how they are used. Learn how to return
values from functions, understand the
difference between parameters and
arguments.
3. Explore common built-in functions in Python.
Understand how to use functions.
III. PRELIMINARY ACTIVITIES
Recap on previous topic and identify its relation with the next lesson.
IV. LESSON PROPER
FUNCTION
You can define your own functions using:
def function-name (var1 , ..., varK ):
body code
var1 , ..., varK are the formal parameters
If the body code executes return expression the result of expression will
be returned by the function. If expression is omitted or the body code
terminates without performing return, then None is returned.
When calling a function name(value,..., value K)body code is executed
with var1=value1
Page 1 of 4
CS22-OOP2(Object Oriented Programming 2)
College of Computer Studies
Why Functions?
Avoid writing the same code multiple times, re-usability
Be able to name a functionality
Clearly state the functionality of a piece of code, abstraction:
Input = arguments, output = return value (and/or side effects)
Encapsulate code with clear interface to the dependency to the
outside world/code
Share functionality in modules/libraries/packages with other users,
code sharing
Increase readability of code, smaller independent blocks of code
Easier systematically testing of code
Local variables in functions
The formal arguments and variables assigned to in the body of a function
are created as temporary local variables.
Global variables
Variables in function bodies that are only read, are considered access to
global variables.
global
Global variables that should be updated in the function body must be
declared global in the body:
global variable, variable, ...
Note: If you only need to read a global variable, it is not required to be
declared global (but would be polite to the readers of your code).
Arbitrary number of arguments
If you would like your function to be able to take a variable number of
additional arguments in addition to the required, add a *variable as the last
argument.
Page 2 of 4
CS22-OOP2(Object Oriented Programming 2)
College of Computer Studies
In a function call variable will be assigned a tuple with all the additional
arguments.
Unpacking a list of arguments in a function call
If you have list L (or tuple) containing the arguments to a function call, you can
unpack them in the function call using *L
L = [x, y, z]
f(*L)
is equivalent to calling
f(L[0], L[1], L[2])
i.e.
f(x, y, z)
Note: that f(L) would pass a single argument to f, namely a list
In a function call several * expressions can appear, e.g. f(*L1, x, *L2, *L3)
Keyword arguments
Previously we have seen the following (strange) function calls
print(7, 14, 15, sep=":", end="")
enumerate(my_list, start = 1)
name = refers to one of the formal arguments, known as a keyword argument. A
name can appear at most once in a function call.
In function calls, keyword arguments must follow positional arguments.
Page 3 of 4
CS22-OOP2(Object Oriented Programming 2)
College of Computer Studies
Keyword arguments, default values
When calling a function argument can be omitted if the corresponding arguments
in the function definition have default values argument = value.
Example- Nested
Function definition
V. PRACTICE EXERCISES/ACTIVITIES
VI. ADDITIONAL RESOURCES
VII. ASSESSMENT
VIII. REFERENCES
Allen B. Downey, ``Think Python: How to Think Like a Computer Scientist‘‘, 2nd edition, Updated
for Python 3, Shroff/O‘Reilly Publishers, 2016
https://www.stat.berkeley.edu/~spector/python.pdf
Page 4 of 4