Python Functions Overview

Introduction

A function is nothing but a piece of programming code which perform specific task. Once a function is created in you program you can reuse that function number of times that reduces the duplication of our code.
In python functions are of the following two types:
  1. Built-in functions
  2. User-define Functions
Built-in function are functions which are pre-defined in Python like Math Function and String Function. User define functions are the function which are defined by user. 
 
Defining a Function in Python
  1. def function-name(arguments):    
  2.      ---------    
  3.      ---------  #Body of the Function  
  4.      ---------    
  5.      return Value;   
Here,
def is the keyword in python which is used to define the function,
function-name is user defined function name, this name can be anything defined by user,
arguments are list of argument which you want to pass into the argument, and
return returns some value from the function. 
 
Calling a Function

Above function can be called like the following:
  1. retunValue=function-name(arguments)  
Example
  1. """A Simple Function 
  2. which don't takes any argument and  
  3. don't return any value"""  
  4.   
  5. def MyFunction():  
  6.     print("Hello, C# Corner :)")  
  7.     return   
  8.   
  9. #calling "MyFunction" function  
  10. MyFunction()  
Output

 
 
Passing parameters to function
  1. """Passing Value to function"""  
  2.   
  3. def Sum(a,b):  
  4.     return a+b  
  5.   
  6. result=Sum(5,6)  
  7. print(result)  
Output

 
 
Returning Multiple Values from the function

In Python we can return multiple values also. To return multiple values we use comma(,) when we return value.
Example
  1. #Function Returns Multiple Values  
  2. def ReturnMultipleValues():  
  3.     a=8  
  4.     b=9  
  5.     c=10  
  6.     return a,b,c  
  7.   
  8. i,j,k=ReturnMultipleValues();  
  9. print(i)  
  10. print(j)  
  11. print(k)  
Output

 
 
Calling Function With Argument

We can call a function in Python by the following different ways:
  1. Required Arguments
  2. Keyword Arguments
  3. Default Arguments
  4. Variable Length Argument 
Required Argument

When arguments are passed into the function in exact order as function is defined.

Example
  1. def MyFunction(arg):  
  2.     print(arg)  
  3.     return  
  4. MyFunction("Hello C# Corner :)")  
Output

 
 
Keyword Argument

When function is called by its parameter name.
  1. def MyFunction(arg1,arg2):  
  2.     print(arg1)  
  3.     print(arg2)  
  4.     return  
  5.   
  6. MyFunction(arg2="Somani",arg1="Sourabh")  
Output
 
 
Default Argument 

Sometimes we can define a function in Python with default argument value, so when the function will be called and if we do not pass the argument, then by default it will take default values.
  1. def MyFunction(a=0,b=0):  
  2.     print(a+b)  
  3.     return  
  4.   
  5. MyFunction()  
  6. MyFunction(a=5)  
  7. MyFunction(b=6)  
  8. MyFunction(a=10,b=20)  
Output

 
 
Variable-Length Arguments

Sometimes we don't know the number of arguments which we will pass into the function. So we can define a function with variable length argument, So the number of arguments may vary according to need.

An asterisk(*) sign is placed before the variable name so argument list may very according to need. 

Example: We want to add n numbers which we will pass to the function.
  1. def Add(*args):  
  2.     sum=0  
  3.     for i in args:  
  4.         sum+=i  
  5.     return sum  
  6. print(Add(5,6,7,8,9,10))  
  7. print(Add(1,2,3,4,5,6,7,8,9,10))   
Output 

 
Thanks! 

Up Next
    Ebook Download
    View all
    Learn
    View all
    sourabhsomani.com