Remember
def creates a callable function. Its body executes when called, and return passes a result back to the caller.
def creates a callable function. Its body executes when called, and return passes a result back to the caller.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
def double(number):
return number * 2
result = double(3)
print(result)Top-level scope
function doubleObject 3 · int6def creates a callable function. Its body executes when called, and return passes a result back to the caller.
(no output)
What do you expect this program to print?
double(3)
|
v
PARAMETER: number = 3
BODY: number * 2
RETURN: 6
|
v
result = 6Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
def double(number):
return number * 2
result = double(3)
print(result)Top-level scope
function doubleObject 3 · int6The example has finished. Compare the final output with the values and references above.
6
What do you expect this program to print?
def double(number):
return number * 2
result = double(3)
print(result)As you write more code, you’ll notice that some parts of your code repeat. Instead of writing the same thing again and again, we can group that code into something called a function.
Functions let you give a name to a block of code. Later, you can use that name to run the code whenever you want — as many times as you want!
This is how you define a simple function in Python:
def function_name():
# block of code
print("Hello!")
Explanation:
def is a keyword used to define a function.function_name is the name you choose for your function (no spaces, only letters, numbers, and underscores).() is where we can pass information (called parameters) — more on that below.Once the function is defined, you can use it like this:
def say_hello():
print("Hello!")
say_hello()
Hello!
Explanation: The line say_hello() tells Python to run the code inside the function.
You can also send information to a function by using parameters:
def greet(name):
print("Hello, " + name + "!")
greet("Arjun")
greet("Bob")
Hello, Arjun!
Hello, Bob!
Explanation: The word you pass inside greet() goes into the name variable inside the function.
Sometimes you want a function to calculate and give back a value. For that, we use the return keyword:
def add(a, b):
return a + b
result = add(3, 4)
print(result)
7
Explanation: The function add() calculates 3 + 4 and returns 7, which we print.
# No input, no return
def show_message():
print("Welcome!")
# Input, no return
def greet_user(name):
print("Hi, " + name)
# Input and return
def multiply(x, y):
return x * y
# Using the functions
show_message()
greet_user("Zara")
print(multiply(3, 5))
Welcome!
Hi, Zara
15
Functions are like mini-programs inside your main program. You can call them whenever you need them. This makes your code easier to read, write, and fix.
Connect what you noticed in the example with the idea below.
def creates a callable function. Its body executes when called, and return passes a result back to the caller.
Defining a function immediately executes its body.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.