A function in a program contains a block of statements that are performing some particular task.
As a program grows, the need for creating functions,
where each function is performing a specific task becomes prominent. To understand functions,
we should learn about how to:
Define a function.
Call a function.
How a function is defined in Python?
Before a function is called to perform a particular task, we must define it.
Definition of a function constitutes an intended block of statements(single or multiple), that is going to be executed when this function is called.
Definition of a function starts with the following elements in the specified order -
Using the def keyword to define a function in Python, followed by
The name of the function, followed by
A pair of parenthesis(), which could be empty or may have formal arguments, followed by
Finally, an intended block of statements, representing the work to be done the function.
Note: The name of a function followed by an empty pair of parentheses i.e. ( ) signifies that we are not going to pass any value to function when it is called, whereas,
defining a function with formal arguments means a function should be called with the same number and type of arguments.
In this article, we will explain how to define a function with an empty pair of parentheses ().
We will explain how to define a function with formal arguments in the next article.
Example of defining a function in Python
Let's give you an example and define a function named welcome in a Python program.
This function, when called, will print a welcome message to the user.
# Defining a function with empty parenthesis
def welcome()
print('Welcome to the program!')
How a function is called?
Once a function is defined, we can easily call it by just using its name,
followed by an empty pair of parenthesis(), or a pair of parenthesis with formal arguments within, depending on the function definition.
If a function is defined with
an empty pair of parentheses then we don't have to pass any value while calling it. We will learn about how to define and call a function with arguments in the next article.
#Calling the function welcome
welcome()
Advertisement
Example of a function in Python
Let's suppose a program has to first display a welcome message whenever it is executed, following by executing some routine work.
In order to display this welcome message, we have created a separate function in this program called welcome, which will be called
at the beginning of the program to display a welcome message to a user. Let us see how to perform this task.
# Python - Defining and calling a function
# Defining a function named welcome
def welcome():
print('Welcome to the program!')
# welcome() function is called to welcome a user
welcome()
a,b = 2,3
result = a + b
print('Result of 2 + 3 is : ', result)
Output-
Welcome to the program!
Result of 2 + 3 is : 5
Calling multiple functions in Python
Let's suppose a program has two functions, one is called at the beginning of the program to notify that program has started and the other
one is called to notify when the program has finished its routine work.
Let's see how we do this.
# Python - defining and calling multiple functions
# welcome() function is defined
def welcome():
print('Welcome to the program!')
# goodbye() function is defined
def goodbye():
print('The program is going to end. Goodbye!')
# welcome() function is called at the beginning of the program
welcome()
a,b = 10,20
result = a * b
print('Result of 10 x 20 is : ', result)
# goodbye() function is called at the end of the program
goodbye()
Output-
Welcome to the program!
Result of 10 x 20 is : 200
The program is going to end. Goodbye!
We could even pass values to a function when it is called.
For doing this, we will have to first define a function with formal arguments within the parenthesis(). For more on this, please continue with our next article.