Now that we’ve practiced defining a function, let’s learn about calling a function to execute the code within its body.
The process of executing the code inside the body of a function is known as calling it (This is also known as “executing a function”). To call a function in Python, type out its name followed by parentheses
Let’s revisit our
directions_to_timesSq() function :
def directions_to_timesSq(): print("Walk 4 mins to 34th St Herald Square train station.") print("Take the Northbound N, Q, R, or W train 1 stop.") print("Get off the Times Square 42nd Street stop.")
To call our function, we must type out the function’s name followed by a pair of parentheses and no indentation:
Calling the function will execute the
Walk 4 mins to 34th St Herald Square train station. Take the Northbound N, Q, R, or W train 1 stop. Get off the Times Square 42nd Street stop.
Note that you can only call a function after it has been defined in your code.
Now it’s your turn to call a function!
Click Run to see it execute and print out.
Add an additional print statement to our
Have the statement print
"Take lots of pictures!"
Run your code again and see how your output changes.