Sections+5.3 5+notes
Sections+5.3 5+notes
3-5 Notes
The concept of top-down design: breaking an algorithm into functions. Recall that an algorithm is set of
clearly defined instructions for solving a problem. Top-down design is based on the concept of divide-
and-conquer.
A hierarchy chart is a flowchart used to represent the relationships between functions in a program.
A local variable is a variable defined only within a function and it is said to have a function scope, since it
is only accessible within that function. A variable’s scope is the part of program that can access the
variable.
A function that accepts arguments (inputs) must be defined using a parameter list in the function
header. Where the function is called, the values of the arguments are passed to the function, hence, we
say they are passed-by-value. These values are inputs to the function and must be used within the
function to complete the task for which the function is designed.
var1, var2, var3, …, varN are called parameters or parameter variable and they form a parameter list.
Note that the parameter variables are separated by coma within parentheses of the function header.
Example:
Write a function that accepts a string as an argument. The function will display:
Write a main function to test the function. The main should accept a string from the user and call the
function above with the string as an argument.
def password(pstring):
if pstring == ‘Module’:
print(‘Your’, pstring, ‘matches the hidden string’)
else:
print(‘Your’, pstring, ‘did not match the hidden string’)
def main():
user_string = input(‘Please, enter a string: ‘)
password(user_string)
#call main function
main()
Interpretation:
password(user_string) is a call to password function with user_string as an argument from the main
function. The value of user_string is passed to the password function so that during function execution,
this value will represent the value of the parameter variable pstring references. The value of pstring is
now compared with the string ‘Module’. When the execution of the function finishes, the program flow
returns to the main function.
Example:
Write a function that will accept three values a (loan amount), n (months), r (annual percentage rate),
calculate and display the monthly payment (p) using the formula below.
as
p= −n
∧s=r /1200
1− (1+ s )
Write a main function the will ask the user to enter three values a, n and r, and call the function.
def main():
loan_amt = float(input('Enter loan amount: '))
interest_rate = float(input('Enter annual percentage rate: '))
months= float(input('Enter the number of months: '))
loan(loan_amt, months, interest_rate)
main()
Parameter variables: a, n, r of loan function. Parameter variables have function scope, changes made to
them will not be retained at the end of the execution of the function.
Local variables of loan function are rp and p.
Local variables of the main function are loan_amt, interest_rate and months
Arguments passed to loan function when called by the main function are loan_amt, interest_rate, and
months. These are called positional arguments, since changing the position of the argument will result in
it being assigned to a different parameter variable of the function.
It is possible to specify a parameter variable by using assignment operator (=) to assign a value to it
when the function is called. An argument that specifies the parameter variable using an assignment
operator is called a keyword argument. If you have to mix positional and keyword arguments in a
function call, all positional arguments must come first before keyword arguments.
Examples:
1. loan(loan_amt, months, interest_rate) – positional arguments
2. loan(1000, 10, 5) – positional arguments
3. loan(a=100, r=5, m = 10) -keyword arguments
4. loan(r=interest_rate, m = 10, a = loan_amt), keyword arguments
5. loan(100, 10, r = 5) – mix of positional and keyword arguments
A global variable is one that can be accessed by all functions in a program or module. A module is a file
that contains a program in Python. A function can only access the global variable but cannot modify it
unless the global statement is indicated in the function. In order for a function to modify or make
changes to a global variable, include the following in the function definition:
where global_variable_1 and global_variable_2 are expected to change within the function.
Example:
Write a program that will simulate a calculator that can perform addition, subtraction and division only.
The program should ask the user to operator (*,- or /) and a number and the program will add, subtract
from or divide the stored value (accumulator) by the number entered by the user.
#Addition function
def add(num):
global accum #global statement
accum += num
#Subtraction function
def subtract(num):
global accum
accum -= num
#Division function:
def multiply(num):
global accum
accum *=num
stop = False
while not stop: #Wait for the user to quit
if __name __ = ‘__main__’ :
calculator()