Functions:
In Python, a function is a group of related statements that
performs a specific task.
Functions help break our program into smaller and modular
chunks. As our program grows larger and larger, functions make it more
organized and manageable.
Furthermore, it avoids repetition and makes the code reusable.
Syntax of Function
def function_name(parameters):
"""docstring"""
statement(s)
Above shown is a function definition that consists of the
following components.
*Keyword def that marks the start of the function header.
*A function name to uniquely identify the function. Function
naming follows the same rules of writing identifiers in Python.
*Parameters (arguments) through which we pass values to a
function. They are optional.
*A colon (:) to mark the end of the function header.
*Optional documentation string (docstring) to describe what
the function does.
*One or more valid python statements that make up the
function body. Statements must have the same indentation level (usually 4
spaces).
*An optional return statement to return a value from the
function.
Example of a function:
def greet(name):
"""
This function
greets to
the person passed
in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
greet('Paul')
we get:
Hello, Paul. Good morning!
This is how a function works
Pass by reference and by value:
f you pass immutable arguments like integers, strings or tuples to a function, the passing acts like call-by-value. The object reference is passed to the function parameters. They can't be changed within the function, because they can't be changed at all, i.e. they are immutable. It's different, if we pass mutable arguments. They are also passed by object reference, but they can be changed in place in the function. If we pass a list to a function, we have to consider two cases: Elements of a list can be changed in place, i.e. the list will be changed even in the caller's scope. If a new list is assigned to the name, the old list will not be affected, i.e. the list in the caller's scope will remain untouched.
Example for call
by reference :
def add_more(list):
list.append(50)
print("Inside Function", list)
mylist = [10,20,30,40]
add_more(mylist)
print("Outside Function:", mylist)
Output:
Inside Function
[10,20,30,40,50]
Outside Function [10,20,30,40,50]
Example for Call
by Value:
def test(string):
string = "Pragati Engineering College"
print("Inside Function:", string)
string = "Pragati"
test(string)
print("Outside Function:", string)
Output:
Inside Function: Pragati Engineering College
Outside Function: Pragati
Python utilizes a system, which is known as “Call by Object Reference” or “Call by assignment”. In the event that you pass arguments like whole numbers, strings or tuples to a function, the passing is like call-by-value because you can not change the value of the immutable objects being passed to the function. Whereas passing mutable objects can be considered as call by reference because when their values are changed inside the function, then it will also be reflected outside the function.
Comments
Post a Comment