Functions in Python

Python allows you to divide your program into different simplified modules in the form of functions. Each function contains a group of statements combined together to perform a particular operation or task.
Functions help you to create a much organised and manageable program. In addition, a function is a 'write once use everywhere' entity. Once you have created a function, you can use it anywhere in the program multiple times.

Types of functions in Python

  1. Built-in Functions: Python owns a big collection of built-in function which is stored in the python library. For example print() function, which is designed to print a value as output. There are many such functions present in the python library which can be used just by calling them. These functions are also called library functions.
  2. User-defined functions: These functions are created by the users themselves as per the need of the program.

How to create a function in Python?

Syntax:
def function_name() :
    Function body

To create a function, you need to define it using the def keyword. Basically, the definition of the function has five components:

  1. Keyword - def
    It tells the interpreter that a function is defined ahead.
  2. Identifier - function_name
    This is the name of the function which is an identifier. While naming the function, you must follow the rules to create an identifier.
  3. Parentheses - ()
    a pair of parentheses must be placed after the function name. They are used to pass parameters to the function when required.
  4. Colon - :
    Python syntax uses a colon to indicate the beginning of the body of the function( or loops ).
  5. Function body 
    The body of the function holds the statements which are instructed to do the given task. Each statement in the body of the function is indented(usually four spaces). As the indentation is removed, the function body ends.

Example:

def hobby(): #creating the function.
    print("My hobby is travelling.")
print("I love to explore new places.")

Output:

>>>


Why the output is blank even when the function body contains two print statements?
Here, the function is only defined. To make it work, you need to call it. When the function is called, it produces the output and returns it to the caller of the function.

How to call a function in Python?

Syntax:
function_name()

To call a function, you just need the name of the function followed by the parentheses.

Let us call the function created in the above program.

def hobby(): #creating the function.
    print("My hobby is travelling.")
    print("I love to explore new places.")
print("What are your hobbies?") 
hobby() #calling the function.

Output:

What are your hobbies?
My hobby is travelling.
I love to explore new places.


Arguments in Python Function

Argument is an input given to a function. The function then operates on these arguments and produces a result. The argument acts as a local variable to the function. There are two types of arguments:

  1. Formal Parameters: When you define a function, formal parameters are given to the function. These parameters act like a placeholder and they are treated as the local variable of the function.
  2. Actual Parameters: When you call the function, actual parameters are given to the function. These values replace the formal parameters in the function.

Let us create a program using a function with arguments.
This program will simply add 2 numbers.

def sum( x, y ): #creating the function.
    print("sum =", x+y )
print("Enter two numbers:")
a=int(input("a ="))
b=int(input("b ="))
sum( a,b ) #calling the function.

Output:

Enter two numbers:
a = 7
b = 9
sum = 16

Explanation:

  • In the above program, when the function sum() is defined, two formal parameters x & y are passed to it. The body of the function contains one statement which prints the sum of the x & y.
  • After the function, a message is printed and the user is asked to enter two numbers. These two values are assigned to the variables a & b.
  • When the function is called, the value of a and b is passed as the actual parameter to the function sum().
  • As the function is called, the control jumps to the function definition where x is replaced by a & y is replaced by b.
  • Now the body of the function is executed. The result produced by the function is returned back to the place from where it was called.