Transcript functions

CMPT 120
Topic: Functions – Part 1
Learning outcomes
At the end of this course, a student is expected to:
• Create (design) small to medium size programs using
Python:
• Decompose a Python program into functions
• Use the core features of Python to design programs to
solve problems: variables, expressions, terminal input and
output, type conversion, conditionals, iteration, functions,
standard library modules
• Design programs requiring approximately 100 lines and 6
functions (of well-designed code)
• Describe the benefits of using functions
• Construct functions such that:
•
•
•
•
Functions have a single purpose (decomposition)
Functions are reusable (generalisation)
Functions include parameters and local variables
Functions return values
• etc…
2
Last Few Lectures
• Binary encoding
Binary and decimal numeral systems
Converting a binary number to a decimal number
Converting a decimal number to a binary number
How characters,
integers (unsigned, signed),
floating point numbers
(have a look at the examples posted on
our course web site)
are represented in memory
• How many values (range of values) can a 8-bit
character, x-bit unsigned integer, x-bit signed
integer represent
•
•
•
•
3
Today’s Menu
• Functions
• Built-in
• User-defined
• Create our own functions
• Design function interface
• Syntax
• Location of functions in program
• Guideline
• Generalization
• Why creating functions
4
So far …
… we have used functions that were already built
into Python by calling them
• Built-in functions (some came from modules)
• For example: print(…), input(…), type(…),
random.randint(15, 25)
• Built-in methods
• For example: <string>.upper( ),
<string>.isalpha( )
5
Remember (from Lecture 7):
Terminology related to functions
1. We call a function by name
• Example of a function call:
userName = input("Please, enter your name: ")
2. The name of the function is input
3. The expression(s) in parentheses is (are) called the
argument(s) of the function
userName = input("Please, enter your name: ")
4. We say that a function takes an argument(s)
5. The result of the function is what the function produces
• In the above example, the result of the function is what the user
has typed, i.e., her/his name, which is assigned to userName
‘Anne’
6. We say that a function returns a result and the result is
called the returned value
6
Different kinds of functions
Activity:
Function takes
arguments
Function does not
take arguments
Function returns a
result
(i.e., a returned value)
Function does not
return a result
(i.e., a returned value)
7
So, what is a function?
• Description: (adapted from our textbook)
• A named sequence of Python statements that
has one purpose
8
Are there “functions” in the
real world?
1. Mom says to Louise “Go make your bed!“
• Purpose: Make bed
• Parameter(s): None required
• Returned value: None returned
2. Mom says to Louise “Here’s $5, go buy a bag of
apples!“
• Purpose:
• Parameter(s):
• Returned value:
9
Are there “functions” in the
real world?
3. Mom says to Louise “Go find my cell phone!“
• Purpose:
• Parameter(s):
• Returned value:
4. Mom says to Louise “Go put these bags on
the kitchen counter!“
• Purpose: Put bags on kitchen counter
• Parameter(s):
• Returned value:
10
Back to computer world,
let us create our own functions
• User-defined functions (as opposed to built-in functions)
11
Problem Statement - 1
• Let us create a function that returns the largest
number out of 3 numbers
• Requirements:
• cannot use the max( ) built-in function
• cannot use the word max to name your function
12
Creating our own functions
As part of Step 2 of Software Development Process
• Now need to consider the function interface
-> how the function interface with the rest of the program
1. Purpose of function -> reflected in its name
2. Algorithm
3. Data
• Input – Does the function need data from the main program?
• Output - Does the function need to return result to the main
program?
• Others
13
Syntax of function definition
def <functionName>( <parameter(s)> ) :
< 1 or more statements >
return <expression>
• def -> means “here is the definition of a function”
• <functionName> (Good Programming Style - GPS)
• Function name is descriptive: it describes its purpose
• Function name syntax same as for variable name
syntax
•
•
•
•
Function definition ->
Header of a function ->
Body of a function ->
One return statement
14
Where to put functions in our
Python program
Python program
Header
comment
block
+ import
statement(s)
…
Function
definitions
# Main part of my program
The main part of
my program
15
Function call –> situation 1
Python program
Header
comment
block
+ import
statement(s)
Function
definition
The main part of
my program
16
Function call –> situation 2
Python program
Header
comment
block
+ import
statement(s)
Function
definitions
The main part of
my program
17
Function call –> situation 3
Python program
Header
comment
block
+ import
statement(s)
Function
definitions
The main part of
my program
18
Function call ->
Would this situation work?
Python program
Header
comment
block
+ import
statement(s)
Function
definition
The main part of
my program
Function
definition
19
Solution
to Problem Statement - 1
• See Largest_version_1.py on our course web site
20
Generalization guideline
• We always strive to create functions (programs)
that solve as many similar problems as possible
• Characteristic of good programming style (GPS)
21
Revisit Problem Statement - 1
• Let us create a function that returns the largest
number out of 3 numbers
• Requirements:
• cannot use the max( ) built-in function
• cannot use the word max to name your function
• A more generalised version of the above function,
would be:
22
Solution to
Revisited Problem Statement - 1
• See Largest_version_2.py on our course web site
23
Problem Statement - 2
• Let us create a function that prints x number of
a symbol in a row
Sample Runs:
>>> !!!!!
>>> ***
24
Solution
to Problem Statement - 2
• See PrintRow.py on our course web site
25
Why creating functions?
1.
Incremental development:
• Dividing a long program into functions allows you to implement,
test and debug the parts one at a time and then assemble them
into a working whole.
2.
“Logical” decomposition:
• Creating a new function gives you an opportunity to name a
group of statements, which makes your program easier to read
and debug.
3.
No more repeated code:
• Functions can make a program smaller by eliminating repetitive.
• Repeated code is also very error-prone.
4.
Easy modification:
• Later, if you make a change, you only have to make it in one
place.
5.
Code reuse:
• Well-designed functions are often useful for many programs.
Once you write, test and debug one, you can reuse it
26
Summary
• Functions
• Built-in
• User-defined
• Create our own functions
• Design function interface
• Syntax
• Location of functions in program
• Guideline
• Generalization
• Why creating functions
27
Next Lecture
• Hand tracing functions
•
•
•
•
Stack frame
Execution flow
Immutable versus mutable
Scope of local variable and parameters
28