Topic Overview and Lesson Plan

Hello, Space Explorer! Today, our focus is on calling functions in Python. By the end of the lesson, you'll understand how to call functions, utilize transitive function calling, employ named parameters, and return multiple values from a function.

Understanding Function Calling in Python

Much like how using a remote control can switch on your television, calling a function tells Python to execute a block of code. It's akin to giving directions to your spaceship's computer. Let's use a common scenario in space journeys: greeting a new crew member. Consider this example:

def greet(name):
    print("Hello " + name + ", welcome aboard the spacecraft!")

greet("Astro Kid")  # Prints: "Hello Astro Kid, welcome aboard the spacecraft!"

In this instance, we instructed our spaceship's computer to greet the new team member, Astro Kid! Just as in saying "Hello" to a new crew member, calling a function executes the predefined code within that function.

Practical Explanation of Transitive Function Calling

In spaceship jargon, think of transitive function calling as delegating tasks on board your spacecraft. So, in fact, transitive function calling is just when you call one or multiple functions inside another function, delegating execution. Suppose we want to increase the heat in our spaceship's engine — instead of doing it ourselves, we assign the task to another function:

def power_up_engine(level):
    print("Powering up the engine to level ", level)

def engine_start():
    print("The engine has started!")

def launch_spacecraft():
    print("Prepare for launch. ", end='')
    power_up_engine(3)
    engine_start()

# Time to hit the space road!
launch_spacecraft()
"""
Prints:

Prepare for launch. Powering up the engine to level 3
The engine has started!
"""

Here, as we prepare to launch the spacecraft, we call another function — power_up_engine(3) — to power up our spacecraft's engine. Afterward, we call another function - engine_start() to delegate the processing of engine post-startup operations. You can see how one function can call another to perform a task.

Exploration of Named Parameters

Parameters are called named when you provide their name in the function call, explicitly mapping the provided value in the call to the function parameter. Let's see it in action:

# Function definition
def greet_crew_member(name, message):
    print("Hello, " + name + ". " + message)

# Greet a new member, calling function **without** named parameters
greet_crew_member("Astro Kid", "Welcome aboard!")
"""
Prints: "Hello, Astro Kid. Welcome aboard!
"""

# Greet another new member using named parameters
greet_crew_member(name="Star Ranger", message="You're just in time for the cosmic pizza party!")
"""
Prints: "Hello, Star Ranger. You're just in time for the cosmic pizza party!
"""

# Greet another new member using shuffled named parameters
greet_crew_member(message="You're just in time for the cosmic pizza party!", name="Star Explorer")
"""
Prints: "Hello, Star Explorer. You're just in time for the cosmic pizza party!
"""

Note how we used named name and message parameters in the second and third greet_crew_member calls - this way, we make sure parameters won't be messed up, and we clearly state what is what. Named parameters can be provided in any order. This is especially helpful when the function takes many arguments of the same data type; this way, you won't accidentally mess up the parameters' order when calling the function, and the function call will also be easier to read.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal