0% found this document useful (0 votes)
9 views4 pages

Sections+5.3 5+notes

The document explains the concept of top-down design in programming, emphasizing the importance of breaking algorithms into functions and understanding variable scopes. It provides examples of function definitions, parameter lists, and the use of local and global variables, along with practical coding examples for a password checker and a loan calculator. Additionally, it discusses the distinction between positional and keyword arguments in function calls.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Sections+5.3 5+notes

The document explains the concept of top-down design in programming, emphasizing the importance of breaking algorithms into functions and understanding variable scopes. It provides examples of function definitions, parameter lists, and the use of local and global variables, along with practical coding examples for a password checker and a loan calculator. Additionally, it discusses the distinction between positional and keyword arguments in function calls.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Section 5.

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.

Syntax for a void function that will accept N arguments:

def function(var1, var2, var3, …, varN):


statement 1
statement 2
etc.

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:

1. ‘Your string matches the hidden string’ if string is equal to ‘Module’


2. ‘Your string did not match the hidden string’ if string is not equal to ‘Password’

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:

pstring is a parameter variable to the password function.

user_string is a local variable to main function.

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 loan(a, n, r):


rp = r/1200
p = a*rp/(1.0 - (1+rp)**(-n))
print('Monthly payment is: $' + format(p, ',.2f'))

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:

global global_variable_1, global_variable_2, …

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.

accum = 0 #global variable – accumulator

#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

def calculator(): #menu like

stop = False
while not stop: #Wait for the user to quit

play = input('Enter y/n if you want continue: ')


if play == 'n' or play == 'N': #quit
stop = True
print('Thank you for using the program')
else:
#Enter the operator
oper =int(input('Enter the operator:' +\
'\n0 for add' +\
'\n1 for subtract'+\
'\n2 for multiply: '))
#Enter the number
value = int(input('Enter an integer: '))

#Call the appropriate function.


if oper == 0:
add( value )
elif oper == 1:
subtract( value )
elif oper == 2:
multiply( value )
else:
print('Oops!, wrong operator')
print('The current accumulator is', accum)

if __name __ = ‘__main__’ :
calculator()

You might also like