functions in python
- Function definition
There are some simple rules to define a function in Python. They are as follows:
Use the def keyword followed by function name with parentheses ()
Any argument to the function must be placed within these parentheses ()
The code block must start with a colon :
The code within the function must be indented
def function_name(arguments):
return value
Function with variable length argument
There might be a scenario where you need to pass more arguments than specified during
the function definition. In this case, variable length arguments can be passed:
# variable length argument def my(var1,*vari): print('first argument is {0}'.format(var1)) for i in vari: print('variable length argument is {0}',i) my(800) my(100,200,300,400) # Output: """first argument is 800 first argument is 100 variable length argument is {0} 200 variable length argument is {0} 300 variable length argument is {0} 400""" # Key Value def my2(**vari1): for k,v in vari1.items(): print('key{0}:value{1}'.format(k,v)) my2(key1='yogesh',key2='sharma') # Output """keykey1:valueyogesh keykey2:valuesharma"""
Pass by reference versus pass by value
Pass by reference is the term used in some programming languages, where values to the
argument of the function are passed by reference, that is, the address of the variable is
passed and then the operation is done on the value stored at these addresses.
Pass by value means that the value is directly passed as the value to the argument of the
function. In this case, the operation is done on the value and then the value is stored at the
address.
# Pass by Reference Example def call(list1): list1.extend([2,3]) print('List1 inside function ',list1) list1=[20,30,40] print('List Value before function calling',list1) call(list1) print('List Value after function calling',list1) # Output """List Value before function calling [20, 30, 40] List1 inside function [20, 30, 40, 2, 3] List Value after function calling [20, 30, 40, 2, 3]""" # Function call by value def call1(a): a=a+4; print('value of a in function body is ',a) a=5 print('value of a before function calling',a) call1(a) print('value of a after function calling',a) # Output: """value of a before function calling 5 value of a in function body is 9 value of a after function calling 5"""
First Example Concept
Here, in the function definition, we pass the list to the pass_ref function and then we
extend the list to add two more numbers to the list and then print its value. The list extends
inside the function, but the change is also reflected back in the calling function.
Second Example Concept
The preceding example might make you think it is called by value, as the change happening
inside the Python function does not get reflected back in the calling function. It is still a pass
by reference, as, in this situation inside the function, we made new assignment, that is, a=
a+4. Although you might think that a = a + 4 is changing the number stored in a, but it is
actually reassigning a to point to a new value: