Procedural Programming/Functions in Python
Functions in Python
A function is a set of blocks,it does not execute itself. We have to call the function to execute the functions, it consists of 3 parts: that declaration ,definition or calling.
When to use functions?
- For reusability of the code we define the functions.
- For defining each individual functionality, we define individual functions. Hence breaking long programs up into smaller components.
- For reusability of lines of code in the other programs, we define function as a module.
Syntax:
def fun_name(arguments): Body of function fun_name(argument)
Be careful with names of functions, we wouldn’t want to call a function the same name as a built-in function in Python.
User Defined Function:
Creating Function without parameters:
def message(): print("Hello") message() message()
Output:
Python 3.8.4 (tags/v3.8.2:7b3vbb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> == RESTART: C:/Users/ASUS/AppData/Local/Programs/Python/Python38-32/geeting.py = Hello Hello >>>
Defining Function with parameter(s):
We can define function with parameters in three ways:
- It takes the default value.
- It also takes the non default value.
- It also takes the assignment value.
Defining function with parameter having default value:
- These are those parameters which are followed by an equal sign (=) and default value.
- In this, if we don’t provide an argument while calling the function, it’s default value will be used.
def message(name='Amit'): print('Hello ',name) message()
Output:
Python 3.8.4 (tags/v3.8.2:7b3cb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> ================== RESTART: C:/Users/PC/Desktop/Hello.py ================= Hello Amit
Calling function with different arguments:
def message(name='Amit'): print('Hello ',name) message("Rahul") message("Kamlesh")
Output:
Python 3.8.4 (tags/v3.8.2:7b3cb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> ================== RESTART: C:/Users/PC/Desktop/Hello.py ================= Hello Rahul Hello Kamlesh
Calling function with different arguments:
def message(name): print('Hello ',name) message('Rahul') message('Kamlesh')
Output:
Python 3.8.4 (tags/v3.8.2:7b3cb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> ================== RESTART: C:/Users/PC/Desktop/Hello.py ================= Hello Rahul Hello Kamlesh
Passing Arguments to Function:
An argument is a variable, value passed to a function as input. We can pass arguments to function in two ways:
- As Positional Arguments
- As Keyword Arguments
Passing argument as positional arguments:
While calling a function, positional arguments are arguments that need to be included in the proper position i.e, when the function is called
- The first position argument is always listed first.
- The second position argument is always listed second.
- The third positional argument needs to be listed third, etc.
Example: Defining a function whose first parameter is name and second parameter is age and passing arguments to function positionally.
def info(names, ages): print('Name',names) print('Age',ages) info('Archana',27)
Output:
Python 3.8.4 (tags/v3.8.2:7b3cb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> ================== RESTART: C:/Users/PC/Desktop/Hello.py ================= Name Archana Age 27
Passing argument as keyword arguments:
While calling a function, keyword arguments are arguments that are passed to a function which is preceded by a keyword and an equals sign.
Example: Defining a function whose first parameter is name and second parameter is age and passing arguments to function with keywords.
def info(names, ages): print('Name',names) print('Age',ages) info(names='Archana',ages=27)
Output:
Python 3.8.4 (tags/v3.8.2:7b3cc59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> ================== RESTART: C:/Users/PC/Desktop/Hello.py ================= Name Archana Age 27
Keyword Arguments Specified by a Dictionary
- Keyword arguments can also be passed to functions using a dictionary object.
- General syntax to use is: function(**dictionary)
def information(name, age): print('Name',name) print('Age',age) data = {'name':'Sursen','age':40} information(**data)
Output:
C:/Users/ASUS/AppData/Local/Programs/Python/Python38-32/dfdf.py === Name Sursen Age 40 >>>
Defining function with parameter taking variable number of arguments:
When we have a maximum number of arguments we can pack the value passed with value as list ,dictionary or tuple value.
- * is used to pack arguments as a tuple when passed into the parameter.
- ** is used to pack arguments as a dictionary when passed into the parameter.
Example 1: Defining a function which adds multiple values passed as positional arguments.
def add(*argument): print('Tuple:-',argument) a=0 for i in argument: a=a+i print('Sum:',a) add(34,3,34,3) add(34,24,234,234,324)
Output:
Python 3.8.4 (tags/v3.8.2:7b3cb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> ================== RESTART: C:/Users/PC/Desktop/Hello.py ================= == RESTART: C:/Users/ASUS/AppData/Local/Programs/Python/Python38-32/assdsd.py == Tuple:- (34, 3, 34, 3) Sum: 74 Tuple:- (34, 24, 234, 234, 324) Sum: 850 >>>
Function with return statement:
- A print statement is used for printing the value on the python console if you want to save the value or reuse the value then you can also use return statement.
- Return statement is optional in the program.
- Value is returned back to the calling statement of the function.
Example: Defining a function that computes addition of two numbers and further the result is evaluated for even or odd.
def add(num1, num2): a= num1+num2 print('Sum is',a) return a x = int(input("Enter 1st no.: ")) y = int(input("Enter 2nd no.: ")) res= add(x, y) if (res%2==0): print('Even') else: print('Odd')
Output:
================== RESTART: C:/Users/PC/Desktop/Hello.py ================= Enter 1st no.: 67 Enter 2nd no.: 98 Sum is 165 Odd >>>
Returning multiple values from function:
Example: Defining a function that computes addition and subtraction of two numbers and return result as tuple.
def arithmetic(n1, n2): a = n1+n2 s = n1-n2 return a, s x = int(input("Enter 1st no.: ")) y = int(input("Enter 2nd no.: ")) res = arithmetic(x, y) print(res, type(res))
Output:
Python 3.8.4 (tags/v3.8.2:7b3cb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> ================== RESTART: C:/Users/PC/Desktop/Hello.py ================= Enter 1st no.: 10 Enter 2nd no.: 4 (14, 6)
Example: Defining global variable and using it inside and outside function scope.
x= int(input("Enter no. ")) # x is global variable def arithmetic(a,b): total=a+b+x print('Sum',total) arithmetic(4,5) if (x%2==0): print(x,"is Even") else: print(x,"is Odd")
Output:
Python 3.8.4 (tags/v3.8.2:7b3cb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> ================== RESTART: C:/Users/PC/Desktop/Hello.py ================= Enter no. 12 Sum 21 12 is Even
Example: Defining a function and trying to modify global variables inside function.
n= int(input("Enter no. ")) def arith(a,b): to=a+b+n print('Addition',to) arith(67,43) print(n)
Output:
Python 3.8.4 (tags/v3.8.2:7b3cb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> ================== RESTART: C:/Users/PC/Desktop/Hello.py ================= ====== RESTART: C:/Users/ASUS/AppData/Local/Programs/Python/Python38-32/abc.py ====== Enter no. 56 Addition 166 56 >>>
Example: Defining a global and local variable with same name
n= int(input("Enter no. ")) def arith(x,y): n=3 total=x+y+n print('Sum',total) arith(56,53)
Output:
Python 3.8.4 (tags/v3.8.2:7b3cb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> ================== RESTART: C:/Users/PC/Desktop/Hello.py ================ Enter no. 45 Sum 112
Passing the Dictionary in Function
def fun(**dic): print(dic) a={'Name':'Pratik','Address':'Noida'} fun(**a)
Output
{'Name': 'Pratek', 'Address': 'Noida'}