python functions

Python functions.
How to Define, Declare and use it with examples?


A function is a code of block that executes line by line. It can receive the inputs as the arguments and may return a result.  There is no limit on the number of lines a function can have, it may have from one to thousands of lines. Even empty function is also valid.  

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

A function is a code block that is written once for a functionality and any other code can use it. 

How to Define a function with a def keyword?

The function definition means, to write the code for a function. It includes the name of the function, the number of arguments, and an optional return value. The following creates a function, named print_hello, printing a string on standard output with no arguments. In python def is the keyword that is used to write the function definition.

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

Output->

My first hello function()

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

How to pass arguments to a function?

The input values can be passed to the function in the arguments. You can pass multiple arguments within the parentheses after the function name, just by separating each other with a comma. There is no limit on the argument count. In the following example, we are creating an ADD function that takes two integer values as arguments and adds them.

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

Output->

Result =  12

Variable Number of arguments –

In the above example, the function can add two values. But what if, we want to add any number of values? Python allows passing a variable number of arguments.

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);

Output->

D:\Python Examples>py "Python Functions.py"
 Result =  12
 Result =  25
 Result =  25

Pass arguments with names in any order-

In a basic way, 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 then it should be a type of key and value pair. The key is the name of the variable 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");

Output->

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 value of a parameter 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");

Output->

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 control reaches the calling line, and the value next to the return is available to the caller by 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);

Output->

ADD function returns result =  12