Python functions are explained with examples.


Functions in computer programs are code blocks that execute line by line. It is possible for a user to provide zero or more inputs as arguments to a function, and that function may return nothing or a value.

The number of lines in a function can range from one to thousands; even an empty function can be used.

In this tutorial, we will learn how to use functions in the Python programming language

How to Define a function with a def keyword?

Function definition refers to the writing of the code that implements the functionality. It contains the name of the function, the number of arguments, the optional return value, and the code sequence of the function logic.

The following creates a function print_hello. It takes zero arguments, returns nothing, and prints a string (My first hello function()) on standard output. The keyword def is used in Python to define a function.

def print_hello(): #Start of function
     print("My first hello function()");
     pass; #End of function 
 #Calling the function
 print_hello();
My first hello function()

The function should be defined before it is called. Else it will give an error.

How to pass arguments to a function?

The input values can be passed to the function as arguments. After the function name, you can pass multiple arguments within the parentheses (()) by separating them using a comma.

There is no limit on the argument count. The following example illustrates how to create a function named ADD. It takes two integer values, returns nothing, adds them using a + operator, and prints the result.

def ADD(x,y): #Start of function
     print("Result = ", x + y);
     pass; #End of function 
 ADD(4,8);
Result =  12

Variable number of arguments –

As shown in the example above, the function adds two arguments. But what if we wish to write a function that can add any number of values? Python allows you to pass a variable number of arguments in a function.

def ADD(*args): #Start of function
     i =0;
     result = 0;
     while len(args) > i:
         result = result + args[i];
         i+=1
     print("Result = ", result);
     pass; #End of function 
 ADD(4,8);
 ADD(4,8,13);
 ADD(4,8,5,8);
D:\Python Examples>py "Python Functions.py"
 Result =  12
 Result =  25
 Result =  25

Pass arguments with names in any order-

Fundamentally, the arguments to a function should be passed as specified while the definition of the function. But if you want to pass in any order, it should be a type of key and value pair. The key is the variable’s name, and the value is the actual content.

def Address(City, Country): #Start of function
     print(" I live in the city", City, "of Country", Country);
     pass
 Address(City="Delhi", Country="India");
 Address( Country="India", City="Delhi");
I live in the city Delhi of Country India
I live in the city Delhi of Country India

Arguments with default values.

While the declaration of a function, you can set the values of input parameters. If a parameter’s value is passed by the called, the passed value is set to the parameter. If not, it will contain the default value given in the definition of the function.
It is useful when a routine or function uses most of the time the same values. The following example demonstrates how we can set default values.

def Address(City, Country = "India"): #Start of function
     print(" I live in the city", City, "of Country", Country);
     pass
 Address("Delhi");
 Address("Mumbai");
 Address("London", "UK");
I live in the city Delhi of Country India
  I live in the city Mumbai of Country India
  I live in the city London of Country UK

How to return a value from a function?

A function produces a result that may be useful for the calling program. The function can return the value using the keyword return. Once the execution control reaches the line with the return keyword, the execution reaches the calling line, and the value next to the return is available to the caller using an assignment statement.

Following is an example of an ADD function. This time it returns the sum of values passed.

def ADD(x,y): #Start of function
     return x + y;
     pass;
 sum = ADD(4,8);
 print("ADD function returns result = ", sum);
ADD function returns result =  12