Parameters

A parameter lets you pass data into a sub-program when you call it.

  • In the example below, the main program calls greet_user() twice.
  • Each call passes in different data: “Amira” and “Liam”.
  • The procedure receives the data in the parameter name.
  • It then uses name to display a personalised message.
def greet_user(name):      # 'name' is the parameter (the input)
    print("Hello", name)   # use the data that was passed in

# main program
greet_user("Amira")
greet_user("Liam")
Hello Amira
Hello Liam

Actual and formal parameters

There are two categories of parameters that you need to know about:

  • Formal parameter
    • This is the placeholder variable written in the procedure definition.
  • Actual parameter
    • This is the real value you pass in when you call the procedure.
def greet_user(name):      # 'name' is a formal parameter 
    print("Hello", name)  

# main program
greet_user("Amira") # "Amira" is an actual parameter
greet_user("Liam")  # "Liam" is an actual parameter

Higher software design

At Higher level there are three main parts to program design: top-level design, data flow, and refinements.

  • Top-level design
    • This shows the overall structure of the program. It is broken down into the main steps or subprograms that will be needed. Think of it as a map of the big building blocks.
  • Data flow
    • This describes what data is passed into each subprogram when it runs. It makes clear which values or arrays a sub-program needs in order to do its job.
  • Refinements
    • These are the detailed step-by-step instructions for how each subprogram will work internally. A refinement expands one box of the top-level design into precise logic

Example

A program stores ages in an array and outputs the minimum and maximum values.

  1. Find and display the youngest age
  2. Find and display the oldest age
1 Find and display the youngest age IN ages
2 Find and display the oldest age IN ages
1.1 Set min_age to first element in ages array
1.2 For i from 1 to length of ages -1
1.3          If ages[i] < min_age then
1.4                 Set min_age to ages[i]
1.5 Display min_age
2.1 Set max_age to first element in ages array
2.2 For i from 1 to length of ages -1
2.3          If ages[i] > max_age then
2.4                 Set max_age to ages[i]
2.5 Display max_age
def show_youngest(ages):  # procedure that finds the minimum
    min_age = ages[0]
    for i in range(1, len(ages)):
        if ages[i] < min_age: 
            min_age = ages[i] 
    print("Youngest age is", min_age) 

def show_oldest(ages): # procedure that finds the maximum 
    max_age = ages[0] 
    for i in range(1, len(ages)): 
        if ages[i] > max_age:
            max_age = ages[i]
    print("Oldest age is", max_age)

# main program
ages = [15, 17, 16, 18, 14, 19]
show_youngest(ages)  # call procedure with array
show_oldest(ages)    # call procedure with array