Functions in Python
Last Updated: 11-09-2018
A function is a set of statements that take inputs, do some specific computation and produces output. The idea is to put some commonly or repeatedly done task together and make a function, so that instead of writing the same code again and again for different inputs, we can call the function.
Python provides built-in functions like print(), etc. but we can also create your own functions. These functions are called user-defined functions.
# A simple Python function to check
# whether x is even or odd
evenOdd( x ):
# Driver code
Pass by Reference or pass by value?
One important thing to note is, in Python every variable name is a reference. When we pass a variable to a function, a new reference to the object is created. Parameter passing in Python is same as reference passing in Java.
# Here x is a new reference to same list lst
# Driver Code (Note that lst is modified
# after function call.
[10, 11, 12, 13, 14, 15]
[20, 11, 12, 13, 14, 15]