Introduction to Python Functions
Python Basics
Python Introduction
Python Installation
Overview of Jupyter IDE
Identifiers & Reserved Keywords
Python Variables
Python Numbers
Python Operators
Python Operators and Arithmetic Operators
Comparison and Logical Operators
Assignment and Bitwise Operators
Identity and Membership Operators
Python Flow Control
if else if else statement
While Loop Statement
Python For Loop
Break and Continue Statement
Python Data Types
Python Strings
Python Strings Methods
Python Lists
Python Tuples
Python Dictionary
Python Functions
Introduction to Python Functions
Function Arguments
Recursion Function
Lambda/Anonymous Function
Python - Modules
Python Files
Python - Files I/O
Python - Exceptions Handling
Python - Debugging
What is Python Functions?
Syntax of Function
def function_name(parameters): """docstring""" statement(s) or coding logic return [expression] - Optional
Rules for Creating Functions
Example 1:
# Function to Welcome User def welcomeUser(name): """ This function welcome's the user for python class """ print("Dear "+ str(name) +", Welcome to Python Tutorials")
How to call a Function ?
To call a function we simply type the function name with appropriate parameters
# Calling the Function welcomeUser("Kavsik") Output: Dear Kavsik, Welcome to Python Tutorials
# Reusing the same function with different values welcomeUser("Abdul") welcomeUser("Vidya") welcomeUser("Aravind") Output: Dear Abdul, Welcome to Python Tutorials Dear Vidya, Welcome to Python Tutorials Dear Aravind, Welcome to Python Tutorials
Note: We can also call from another function, program or even the Python prompt.
Example 2:
# Function to check whether a number is even or odd def verifyEvenOdd( num ): if (num % 2 == 0): print("{} is even number".format(num)) else: print("{} is odd number".format(num)) # Calling the Function verifyEvenOdd(2) verifyEvenOdd(5) Output: 2 is even number 5 is odd number
Doc String
# Print doc string of the function print(welcomeUser.__doc__) # Function with Doc String print(verifyEvenOdd.__doc__) # Function without Doc String Output: This function welcome's the user for python class None
Function with return value
Syntax: return [expression]
Example 3:
# Function to adds two numbers and returns the output def add(x,y): return x + y # Calling the Function add(10,25) Output: 35
Example 4:
# Function to add the numbers within the mentioned range def sum(start,end): result = 0 for i in range(start, end + 1): result += i return result # Calling the function and saving the value to a variable value = sum(8,25) print("The Sum of numbers within the mentioned limit is",value) Output: The Sum of numbers within the mentioned limit is 297
Pass by reference vs value
Example 5:
# Function Using pass by reference def updateMe( mylist ): mylist = [40,50,60]; print ("Values inside the function: ", mylist) # Calling updateMe function mylist = [10,20,30]; updateMe( mylist ); print ("Values outside the function: ", mylist) Output: Values inside the function: [40, 50, 60] Values outside the function: [10, 20, 30]
Note: When argument is being passed by reference and the reference is being overwritten inside the called function
Scope and Life Time of Variables
1. Global variables
2. Local variables
Example 6:
# Code to verify the scope of global variable # Declaring the global variable globalVar = "This is global variable" def test_life_time(): print(globalVar) #Using global variable inside the function #calling function test_life_time() #print global variable print(globalVar) #Using global variable outside the function Output: This is global variable This is global variable
Example 7:
# Code to verify the scope of local variable def test_life_time(): # Declaring the local variable localVar = "This is local variable" print(localVar) #Using local variable inside the function #calling function test_life_time() #print local variable print(localVar) #Using local variable outside the function Output: This is local variable --------------------------------------------------------------------------- NameError: Traceback (most recent call last) <ipython-input-25-976ba6276170> in <module>() 10 11 #print local variable ---> 12 print(localVar) #Using localVar variable outside the function NameError: name 'localVar' is not defined