National Economics University
Faculty of Data Science and Artificial Intelligence
DSAI1003 - Fundamental of Programming
Concepts in Python: FUNCTIONS
❑ Instructor: Duc Minh Vu (FDA - NEU)
❑ Email: minhvd [at] [Link]
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 1
Chapter 5 - FUNCTIONS
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 2
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 3
5.1 Functions as Black Boxes
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 4
5.1 Functions as Black Boxes
❑A function is a named sequence of
instructions.
➢price = round(6.8275, 2) #
Sets result to 6.83
❑Arguments are supplied when a function
is called
❑The return value is the result that the
function compute
➢Returning a value from a function is not the
same as producing program output.
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 5
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 6
5.2 Implementing and Testing Functions
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 7
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 8
5.2.1 Implementing a Function
❑When defining a function, provide a name for the function and a
variable for each argument.
❑E.g. a function to compute the volume of a cube with a given side
length
➢Pick a name for the function (cubeVolume)
➢Define a variable for each argument (sideLength): parameter variables
➢def cubeVolume(sideLength) :
✓header of the function.
✓body of the function. The body contains the statements that are executed
when the function is called.
❑to return the result of the function, use the return
statement
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 9
5.2.2 Testing a Function
❑To test the function, your program should contain
➢The definition of the function.
➢Statements that call the function and print the result.
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 10
5.2.3 Programs that Contain Functions
❑Define each function before you call it. Otherwise, compile time error:
❑However, a function can be called from within another function before the former has been defined
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 11
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 12
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 13
Programming Tip 5.1 Function Comments
❑Whenever you write a function, you should comment its behavior.
❑Function comments explain the purpose of the function, the meaning of the parameter
variables and return value, as well as any special requirements.
❑Doxygen ([Link] tool
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 14
Programming Tip 5.2 Naming Functions
❑The name of a function can be any legal identifier, including the name of a
previously defined variable
❑If you name your function the same as a previously-defined variable, or vice
versa, the previous definition is overwritten
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 15
5.3 Parameter Passing
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 16
5.3 Parameter Passing
❑parameter variables (formal parameters): variables are created for receiving the
function’s arguments; and hold the arguments supplied in the function call.
❑Arguments (actual parameters) : the values that are supplied to the function when it is
called
❑Parameter variables hold the arguments supplied in the function call.
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 17
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 18
Programming Tip 5.3 Do Not Modify Parameter
Variables
❑You can modify the values of the parameter variables in the body of a
function
❑Avoid it , using another variable
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 19
5.4 Return Values
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 20
5.4 Return Values
❑The return statement terminates a function call and yields the function result.
❑The return statement can return the value of any expression:
❑When the return statement is processed, the function exits immediately
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 21
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 22
Every branch of a function should return a value.
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 23
Special Topic 5.1 Using Single-Line Compound
Statements
❑When the body contains a single statement, however, compound statements may be
written on a single line.
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 24
HOW TO 5.1 Implementing a Function
❑Step 1 Describe what the function should do
❑Step 2 Determine the function’s “inputs”.
❑Step 3 Determine the types of the parameter variables and the return value.
❑Step 4 Write pseudocode for obtaining the desired result.
❑Step 5 Implement the function body.
❑Step 6 Test your function
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 25
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 26
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 27
5.5 Functions Without Return Values
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 28
❑Some functions may not return a value, but produce output.
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 29
Reading:
WORKED EXAMPLE 5.1 Generating Random Passwords
Computing & Society 5.1 Personal Computing
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 30
5.6 Problem Solving: Reusable Functions
❑Eliminate replicated code or pseudocode by defining a function.
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 31
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 32
Design your functions
to be reusable.
Supply parameter
variables for the
values that can vary
when the function is
reused.
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 33
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 34
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 35
Reading:
5.7 Problem Solving: Stepwise Refinement
Programming Tip 5.4 Keep Functions Short
Programming Tip 5.5 Tracing Functions
WORKED EXAMPLE 5.2 Calculating a Course Grade
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 36
5.8 Variable Scope
❑The scope of a variable is the part of the program in
which you can access it.
❑the scope of a function’s parameter variable is the entire
function.
➢the scope of the parameter variable sideLength is the entire
cubeVolume function but not the main function
❑A variable that is defined within a function is called a
local variable.
➢When a local variable is defined in a block, it becomes
available from that point until the end of the function in
which it is defined.
❑A loop variable in a for statement is a local variable
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 37
5.8 Variable Scope
❑It is possible to use the same variable name more than once in
a program
❑Python also supports global variables: variables that are
defined outside functions.
➢A global variable is visible to all functions defined after it
➢any function that wishes to update a global variable must include a
global declaration
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 38
Reading:
Programming Tip 5.7 Avoid Global Variables
WORKED EXAMPLE 5.4 Graphics: Rolling Dice
5.9 Graphics: Building an Image Processing Toolkit
WORKED EXAMPLE 5.5 Plotting Growth or Decay
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 39
5.10 Recursive Functions (Optional)
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 40
5.10 Recursive Functions (Optional)
❑A recursive function is a
function that calls itself.
❑A recursive computation solves
a problem by using the solution
to the same problem with
simpler inputs.
❑For a recursion to terminate,
there must be special cases for
the simplest inputs
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 41
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 42
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 43
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 44
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 45
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 46
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 47
[Link]
[Link]
10/15/2024 Fundamental of Programming Concepts in Python – SLSCM Lab 48