Python Functions
Functions in Python
Functions are set of instructions to perform a specific task. Below is the syntax of functions in python.
Syntax
//Syntax:
def function_name([arg1,...,argn]):
#statements
[return value]
variable_name = function_name([val1,...,valn])
Note: Anything enclosed in [ ] (square bracket) is optional.
Example :
Tryout :
Execute this code in python playground and observe the below
- Actual arguments being copied to formal arguments
- Execution of function body
CODE/PROGRAM/EXAMPLE
observe1="What‘s happening!!"
def passport_check(passport_no):
observe4="actual copied to formal"
observe5="func. execution starts"
if(len(passport_no)==8):
if(passport_no[0]>="A" and passport_no[0]<="Z"):
status="valid"
else:
status="invalid"
else:
status= "invalid"
observe6="func. execution ends"
return status
observe2="function with formal arg."
observe3="calling with actual arg."
passport_status=passport_check("M9993471")
print("Passport is",passport_status)
#observe1,2,3,4,5,6 are temporary variables used to explain this concept
Returning from a function :
Let's see how we can use values returned from a function.