Calling Functions
In the previous exercise, you may have noticed that when we define a function and run the code, nothing happens. This is because the function will execute only when it is called. We call a function by using its name followed by parentheses. For example, if we have a function defined like this:
def myFunction():
print("This is my function")
We can call it in the following way:
myFunction() # Outputs: This is my function
When we call a function, its body is executed. In this case, it prints This is my function
. Functions can be called repeatedly and whenever needed within the program. It is important that the function must be defined before it is used. For example, this works:
def myFunction():
print("This is my function")
myFunction()
This does not work:
myFunction()
def myFunction():
print("This is my function")
Instructions
Call the firstFunction
three times.
Start programming for free
3/7