An interactive lesson and quiz on Functions and Procedures.
Please enter your name to begin.
Subprograms help us write structured, reusable code. There are two types:
A procedure is a block of code that you call to perform a task. That's it.
Analogy: A "Print" button. You click it, it *does* an action (prints), but it doesn't *give* you a value back.
procedure display_menu():
print("1. Add")
print("2. Exit")
endprocedure
display_menu()
A function is a block of code that performs a task and must return a single value.
Analogy: A calculator's `sqrt(9)` button. You give it '9', it *does* a calculation, and it *gives* you '3' back to use.
function get_choice():
choice = input()
return choice
endfunction
user_choice = get_choice()
How subprograms send and receive data.
Subprograms need data to work on. We pass data *into* them.
# 'num1' is the PARAMETER
function add_five(num1):
total = num1 + 5
return total
endfunction
# '10' is the ARGUMENT
result = add_five(10)
print(result) # Outputs 15
The "Classroom Rule": Variables created in the main program are like notes on the main whiteboard (global), which everyone can see. Variables created inside a subprogram are like scratch work on a student's private mini-whiteboard (local).
Please answer all 5 questions on this page before proceeding.
def greet():print("Hello", name)
The "placeholder" variable in a definition is called a...
# Call the procedure from Q6
greet()
The "actual value" you pass in is called an... (Note: Don't forget the ' ' for a string!)
def add(num1, num2):total = num1 + num2total
Please answer all 5 questions on this page before proceeding.
Drag from here:
Drag from here:
Please answer both questions on this page before proceeding.
def add_nums(a, b):
total = a + b
print(total)
result = add_nums(5, 3)
# 'result' will be 'None'
Re-type only the 3-line function definition correctly.
def welcome(name):
name = input("Enter name:")
print("Welcome", name)
welcome("Alex") # This "Alex" is ignored!
Re-type the procedure definition correctly so it uses its parameter.
def get_number():
local_var = 10
get_number()
print(local_var) # NameError
Re-type all the lines of code, but corrected so it runs without an error. Store the returned value in a variable named `num`.
def calc_area(length, width):
return length * width
area = calc_area(10) # Missing argument
print(area)
Re-type the last two lines correctly, giving it a width of 5.
Please answer all 4 questions on this page before proceeding.
01 def my_function(a):
02 temp = a + 1
03 return temp
04
05 score = 10
06 name = "Dru"
07
08 print(score)
09 print("name")
10 print(my_function(score))
11 print(score)
01 def func_a(x):
02 x = x + 1
03 return x
04
05 def func_b(y):
06 y = y * 2
07 print(y)
08
09 x = 10
10 y = 5
11
12 x = func_a(x)
13 func_b(y)
14 print(x)
Please answer both questions on this page before proceeding.
def welcome(name):
name = input("Enter name:")
print("Welcome", name)
Please answer all 4 questions on this page before proceeding.
Time to write the full subprograms yourself! (Use Python-style syntax: `def`, `return`, etc.)
Please answer all 4 questions on this page before proceeding.
Tackle 3, 4, and 6-mark subroutine questions. These walkthroughs focus on logic error spotting, array iteration, and variable scope.
This is your 1st attempt.