Chapter 5 part 1 pfe
Chapter 5 part 1 pfe
1
Functions as Black Boxes
2
Argument and return value
• These values are called the arguments of the function call. Note
that they are not necessarily inputs provided by a human user.
They are simply the values for which we want the function to
compute a result.
• The “output” that the round function computes is called the
return value.
3
Arguments and Return Value
4
Return Value
price = round(6.8275, 2)
•When the round function returns its result, the return value is
stored in the variable price.
•Do not confuse returning a value with producing program output.
If you want the return value to be printed, you need to add a
statement such as print(price).
5
Implementing and Testing
Functions
• We will start with a very simple example: a function to compute
the volume of a cube with a given side length.
def cubeVolume(sideLength) :
6
Defining a Function
def cubeVolume(sideLength) :
•This line is called the header of the function. Next, specify the
body of the function.
•The body contains the statements that are executed when the
function is called.
•The volume of a cube of side length s is s × s × s = s3. However,
for greater clarity, our parameter variable has been called
sideLength, not s, so we need to compute sideLength ** 3.
•We will store this value in a variable called volume:
volume = sideLength ** 3
•In order to return the result of the function, use the return
statement:
return volume
7
Defining a function
def cubeVolume(sideLength) :
volume = sideLength ** 3
return volume
8
Testing a Function
9
Function Calling
10
Programs with functions
print(cubeVolume(10))
def cubeVolume(sideLength) :
volume = sideLength ** 3
return volume
• The compiler does not know that the cubeVolume function will
be defined later in the program.
11
• However, a function can be called from within another function before the
former has been defined. For example, the following is perfectly legal:
def main() :
result = cubeVolume(2)
print("A cube with side length 2 has volume", result)
def cubeVolume(sideLength) :
volume = sideLength ** 3
return volume
main()
• Note that the cubeVolume function is called from within the main function
even though cubeVolume is defined after main. To see why this is not a
problem, consider the flow of execution. The definitions of the main and
cubeVolume functions are processed. The statement in the last line is not
contained in any function. Therefore, it is executed directly. It calls the
main function. The body of the main function executes, and it calls
cubeVolume, which is now known.
12
Parameter Passing
13
Return Values
def cubeVolume(sideLength) :
return sideLength ** 3
14
Return Function
def cubeVolume(sideLength) :
if sideLength < 0 :
return 0
# Handle the regular case.
.
.
.
15
Functions Without Return Values
-------
!Hello!
-------
16
Functions Without Return Values
17
Functions Without Return Values
boxString("Hello")
19
Using return for argument validation
def boxString(contents) :
n = len(contents)
if n == 0 :
return # Return immediately
print("-" * (n + 2))
print("!" + contents + "!")
print("-" * (n + 2))
20
Lets try to answer some book
questions
• How do you generate the following printout, using the boxString
function?
-------
!Hello!
-------
-------
!World!
-------
• What is wrong with the following statement?
print(boxString("Hello"))
21
Lets try to answer some book
questions
• How would you modify the boxString function to leave a space
around the string that is being boxed, like this:
---------
! Hello !
---------
22
23
Reusable Functions
24
Reusable Functions
def readIntUpTo(high) :
value = int(input("Enter a value between 0 and " +
str(high) + ": "))
while value < 0 or value > high :
print("Error: value out of range.")
value = int(input("Enter a value between 0 and "
+ str(high) + ": "))
return value
25
Book problem discussion
26
Using a Debugger
27
Debugger
28
Debugger essential
• Once you have started the debugger, you can go a long way with just four
debugging commands:
• “set breakpoint”, “run until breakpoint”, “step over”, and “step inside”.
The names and keystrokes or mouse clicks for these commands differ
between debuggers, but all debuggers support these basic commands.
• When you start the debugger, it runs at full speed until it reaches a
breakpoint. Then execution stops. The line containing the breakpoint that
causes the stop is displayed, but it has not yet been executed.
• You can now inspect variables and step through the program a line at a
time, or continue running the program at full speed until it reaches the
next breakpoint. When the program terminates, the debugger stops as
well.
• Running to a breakpoint gets you there speedily, but you don’t know what
the program did along the way. For a better understanding of the program
flow, you can step through the program a line at a time. One command,
usually called “step into”, steps inside function calls, and another
command, called “step over” skips over function calls. You should step
into a function to check whether it carries out its job correctly. Step over a
function if you know it works correctly.
29
Recursive Functions
30