python


Python Function


What is a Function?

A function in Python is a collection of statements having a particular name followed by parenthesis .
To run a function , we have to call it and when we call a function all the statements inside the function are executed.
So we don’t have to write the code again and again
This is called code re-usability. 


Function vs Methods

Functions are block of codes defined individually .
But if a function is defined inside a class , it becomes a method
So , methods and functions are same except their placement in the program.
Also we can call a function directly using it’s name but when we call a method we have to use either object name or class name before it

For example:


print(“Welcome to CODEMISTIC”)
Here print( ) is a function as we are calling it directly.

message=“Welcome to CODEMISTIC”
print(message.lower())
Here lower( ) is a method which belongs to the class str and so it is called using the object message.

Steps required for Function

To create and use a function we have to take 2 steps:
1. Function Definition: Creating or writing the body of a function is called defining it. It contains the set of statements we want to run , when the function execute.
2. Function Call: A function never runs automatically . So to execute it’s statements we must call it


Syntax for function defination


def function_name(param 1,param 2, . . .):
        statement(s)    

1. Keyword def marks the start of function header.
2. It is followed by a function name to uniquely identify it.
3. Parameters (arguments) through which we pass values to a function. They are optional.
4. A colon (:) to mark the end of function header.
5. One or more valid python statements that make up the function body . All the statements must have same indentation level


How to call a function?

Once we have defined a function, we can call it from another function, program or even the Python prompt.
To call a function we simply type the function name with appropriate parameters.
Syntax:


function_name(arguments)
    

Returning values from a function

To return a value or values from a function we have to write the keyword return at the end of the function body along with the value(s) to be returned Syntax:



return  <expression>
Example:

def add(a,b):
     c=a+b
    return c
x=add(27,2)
print("Sum of 27 and 2 is",x)
y=add(20,7)
print("Sum of 20 and 7 is",y)
Output:

Sum of 27 and 2 is 29
Sume of 20 and 7 is 27


Returning multiple values from a function

In languages like C or Java , a function can return only one value . However in Python , a function can return multiple values using the following syntax:
Syntax:


return  <value 1, value 2, value 3, . . . >
For example:

return a,b,c
When we do this , Python returns these values as a tuple, which just like a list is a collection of multiple values.


Recieving multiple values from a function

To receive multiple values returned from a function , we have 2 options: Syntax 1:


var 1,var 2,var 3=()
Syntax 2:

var=()
In the first case we are receiving the values in individual variables . Their data types will be set according to the types of values being returned
In the second case we are receiving it in a single variable and Python will automatically make the data type of this variable as tuple


Example:


def calculate(a,b): 
    c=a+b
    d=a-b
    return c,d
x,y=calculate(5,3)
print("Sum is",x,"and difference is",y)
z=calculate(15,23)
print("Sum is",z[0],"and difference is",z[1]) 
 

    
Output:

Sum is 8 and difference is 2
Sum is 28 and difference is -8
    
Here Python will automatically set x and y to be of int type and z to be of tuple type.