Python3 Functions introduction
Python Functions
Functions:
Function is a group of related statements that perform a specific task.
Functions helps us to break our program into smaller and modular chunks. As our program grows larger and larger, functions make organized and manageable.
** It avoid repetition and makes code reusable.
Syntax::
def function_name(parameters):" " "Doc String" " "Statement(s)
1. keyword "def" marks the start of function header
2. Parameters(arguments) through which we pass values to a function. These are optional
3. A colon(:) to make the end of function header
4. Doc string describe what function does. This is optional
5. "return statement to return a value from the function. This is optional
How function perform :
Example:
def print_name(name):" " "This function prints the name" " "print("Hello"+str(name))
Function Call
Once we have defined a function, we can call it from anywhere
print_name('Sujith')
output: Hello Sujith
Doc String
The first string after the function header is called the docstring and is short for documentation string.
Although optional, documentation is a good programming practice, always document your code
Doc string will be written in triple quotes so that docstring can extend up to multiple lines
print(print_name.__doc__)
This function prints the name
return Statement
The return statement is used to exit a function and go back to the place from where it was called.
Syntax:
return [expression]
-> return statement can contain an expression which gets evaluated and the value is returned.
-> if there is no expression in the statement or the return statement itself is not present inside a function, then the function will return None Object
Eg program:
def get_sum(lst):" " "This function returns the sum of all the elements in a list" " "_sum=0for num in lst:_sum+=numreturn _sums=get_sum([1,2,3,4])print(s)
output: 10
Scope and Life Time of Variables
-> Scope of a variable is the portion of a program where the variable is recognized
-> variable defined inside a function is not visible from outside. Hence, they have a local scope
-> Lifetime of a variable is the period throughout which the variable exists in the memory
-> The lifetime of variable inside a function is as long as the function executes
-> Variable are destroyed once we return from the function.
Example:
global_var="This is global variable"
def test_life_time( ):
" " "
This function test the life time of a variable
" " "
local_var="This is local variable"
print(local_var)
print(global_var)
#calling function
test_life_time( )
print(global_var)
print(local_var)
output: This is local variable
This is global variable
This is global variable
-----------------------------------------------------------------------
---
NameError
NameError: name 'local_var' is not defined
Python program to print Highest Commom Factor (HCF) of two numbers :
def computeHCF(a, b):smaller=b if a>b else ahcf=1for i in range(1,smaller+1):if(a%i==0) and (b%i==0):hcf=ireturn hcfnum1=98num2=78print("H.C.F of {0} and {1} is: {2}" .format(num1, num2, computerHCF(num1,num2)))
output: H.C.F of 98 and 78 is: 2
Comments
Post a Comment