Calling Procedures Lesson

Running to the DOM

After editing the code, try running the code and see if it displays under the cell! If it shows under the cell correctly, you edited it correctly!

1. Basic Call

How would you go about calling the greet function? Edit the code to add a call.

def greet():
    print("Hello, world!")

# call the "greet" procedure here

2. Calling with Parameters

Try changing the name within the parameter to your own name.

def greet(name):
    print("Hello,", name)

# Put your name and a friends name into the call!
greet("Etahn")
greet("JUdas")

Hello, Etahn
Hello, JUdas

3. Call within a Call

try calling the “jump” procedure within the “dance” procedure!

def jump():
    print("Jump up!")

def dance(): # call the jump procedure in here as many times as you want!
    jump()
    print("Spin around!")
    jump()
    print("Spin around!")


dance()

Jump up!
Spin around!
Jump up!
Spin around!

Let’s try it with numbers!

Change the #s below to any two numbers to check the sum! Once completed, try and change the function to a different math procedure (such as subtraction, multiplication, or division). If you want an extra challenge, how can we add more variables, or make the math function more complex within the procedure?

def add_numbers(a, b):
    sum_value = a + b
    return sum_value

def subtract_numbers(a, b):
    difference_value = a - b
    return difference_value
def multiply_numbers(a, b):
    product_value = a * b
    return product_value

answer = add_numbers(5, 10)
print(answer)  
answer2 = subtract_numbers(5, 10)
print(answer2)
answer3 = multiply_numbers(5, 10)
print(answer3)  
 
15
-5
50

Extra Practice Problems

1. Calling Procedures with Return Values

Sometimes, you’ll call one function inside another to use its returned result.

2. Chained Calls with Multiple Steps

You can call one function to get data, and then pass that data into another.

def square(num):
    return num * num

def sum_of_squares(a, b):
    total = square(a) + square(b)
    print("The sum of squares is:", total)

sum_of_squares(3, 4)
def get_age():
    return 16

def check_voting_eligibility(age):
    if age >= 18:
        print("You can vote!")
    else:
        print("You are not old enough yet.")

check_voting_eligibility(get_age())