Q11]PYTHON programming
Q11]PYTHON programming
Python Functions
Syntax:
def function_name(parameters):
"""docstring"""
statement(s)
return expression
Creating a Function
We can create a Python function using the def keyword.
def fun():
print("Welcome to GFG")
Calling a Function
After creating a function we can call it by using the name of the function followed by
parenthesis containing parameters of that particular function.
def fun():
print("Welcome to GFG")
Output
Welcome to GFG
Arguments of a Function
Arguments are the values passed inside the parenthesis of the function. A function can have
any number of arguments separated by a comma.
def evenOdd(x):
if (x % 2 == 0):
print("even")
else:
print("odd")
Types of Arguments
Python supports various types of arguments that can be passed at the time of the function call.
Let’s discuss each type in detail.
Default arguments
A default argument is a parameter that assumes a default value if a value is not provided in the
function call for that argument. The following example illustrates Default arguments.
myFun(10) Output
('x: ', 10)
('y: ', 50)
Like C++ default arguments, any number of arguments in a function can have a default value.
But once we have a default argument, all the arguments to its right must also have default
values.
Keyword arguments
The idea is to allow the caller to specify the argument name with values so that caller does not
need to remember the order of parameters.
Variable-length arguments
In Python, we can pass a variable number of arguments to a function using special symbols.
There are two special symbols:
Docstring
The first string after the function is called the Document string or Docstring in short. This is used
to describe the functionality of the function. The use of docstring in functions is optional but it is
considered a good practice.
The below syntax can be used to print out the docstring of a function:
Syntax: print(function_name.__doc__)
Example: Adding Docstring to the function
# A simple Python function to check
# whether x is even or odd
def evenOdd(x):
"""Function to check if the number is even or odd""" if (x
% 2 == 0):
print("even")
else:
print("odd")
print(evenOdd.__doc__)
Output
Function to check if the number is even or odd
Syntax:
return [expression_list]
The return statement can consist of a variable, an expression, or a constant which is returned to
the end of the function execution. If none of the above is present with the return statement a
None object is returned.
def square_value(num):
print(square_value(-4)) Output:
4
16
Python Function 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
the same as reference passing in Java.
Example:
# Here x is a new reference to same list lst def
myFun(x):
x[0] = 20
def myFun(x):
Another example to demonstrate that the reference link is broken if we assign a new value
(inside the function). def myFun(x):