Functions

Calling functions

In programming, call means to invoke a routine. A function call is a request made by a program or script that performs a predetermined function.

The code in the function hello_world() will not be executed (will not run) until we call the function.

Two ways of calling the function are: by setting a variable to the value returned by the function:

message = hello_world()

or by printing the value returned by the function:

print(hello_world())

When the function is called, the code within the function will run and return a value to the code that called it. A return statement is used within the function to complete this action.

For us to see the effect that the function has, we need to store the return value in a variable, or output it directly to the screen using the print() function.

Run the following code
def hello_world():
# The return value for this function is the string hello, world!
return 'hello, world!'

print('Calling the function hello_world(), outputting what it returns (its return value) to the screen')
print(hello_world())
print('')

The line print(hello_world()), tells the Python interpreter to output whatever was returned by the function called hello_world(). In this case, it will output hello, world!

When we are talking about a function, even in comments or other written materials, we always put the brackets after the function name.

Remember, in programming, call means to invoke a routine. A function call is a request made by a program or script that performs a predetermined function. The call site of a function or subroutine is the location (line of code) where the function is called. A call site is where zero or more arguments are passed to the function, and zero or more return values are received.