Psc-Unit1-2-Python Functions
Psc-Unit1-2-Python Functions
1 of 15 7/24/24, 14:54
Python functions & Types of function arguments in pytho... https://colab.research.google.com/drive/1AAb7PJ-xii5KoE...
Built-in library function: These are Standard functions in Python that are available to use.
User-defined function: We can create our own functions based on our requirements.
Syntax:
def functionname( parameters ):
"function_docstring"
function_suite
return [expression]
We can de�ne a function in Python, using the def keyword. We can add any type of
functionalities and properties to it as we require. By the following example, we can
understand how to write a function in Python. In this way we can create Python function
de�nition by using def keyword.
2 of 15 7/24/24, 14:54
Python functions & Types of function arguments in pytho... https://colab.research.google.com/drive/1AAb7PJ-xii5KoE...
After creating a function in Python we can call it by using the name of the functions Python
followed by parenthesis containing parameters of that particular function. Below is the
example for calling def function Python.
If you have experience in C/C++ or Java then you must be thinking about the return type of the
function and data type of arguments. That is possible in Python as well (speci�cally for
Python 3.5 and above).
"""Docstring"""
body of the function
return expression
The following example uses arguments and parameters that you will learn later in this article
so you can come back to it again if not understood.
Docstring
The �rst 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)
3 of 15 7/24/24, 14:54
Python functions & Types of function arguments in pytho... https://colab.research.google.com/drive/1AAb7PJ-xii5KoE...
return num3
# Driver code
num1, num2 = 5, 15
ans = add(num1, num2)
print(f"The addition of {num1} and {num2} results {ans}.")
even
odd
def modify_integer(x):
x = x + 10
print("Inside function:", x)
x = 5
print("Before function call:", x)
modify_integer(x)
print("After function call:", x)
Pass by Value:
A copy of the actual data (value) is passed to the function. Changes made to the parameter
inside the function do not affect the original data. Used in languages like C, C++ for primitive
types.
Pass by Reference:
A reference (memory address) to the original data is passed to the function. Changes made
to the parameter inside the function affect the original data. Used in languages like C, C++ for
non-primitive types and objects.
6 of 15 7/24/24, 14:54
Python functions & Types of function arguments in pytho... https://colab.research.google.com/drive/1AAb7PJ-xii5KoE...
2. Mutability: It allows functions to modify complex objects (like lists, dictionaries) directly,
without needing to return them.
Default argument
Keyword arguments (named arguments)
Positional arguments
Arbitrary arguments (variable-length arguments *args and **kwargs)
Default Arguments
7 of 15 7/24/24, 14:54
Python functions & Types of function arguments in pytho... https://colab.research.google.com/drive/1AAb7PJ-xii5KoE...
x: 10
y: 50
Keyword Arguments
The idea is to allow the caller to specify the argument name with
values so that the caller does not need to remember the order of
parameters.
# Keyword arguments
student(firstname='khushbu', lastname='maurya')
student(lastname='maurya', firstname='khushbu')
khushbu maurya
khushbu maurya
Positional Arguments
8 of 15 7/24/24, 14:54
Python functions & Types of function arguments in pytho... https://colab.research.google.com/drive/1AAb7PJ-xii5KoE...
Case-2:
Hi, I am 27
My age is Suraj
9 of 15 7/24/24, 14:54
Python functions & Types of function arguments in pytho... https://colab.research.google.com/drive/1AAb7PJ-xii5KoE...
first->Indus
mid->University
last->Ahmedabad
Docstring
The �rst 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)
def evenOdd(x):
"""Function to check if the number is even or odd"""
if (x % 2 == 0):
print("even")
else:
print("odd")
function.
# Python program to
# demonstrate accessing of
# variables of nested functions
def f1():
s = 'Indus University'
def f2():
print(s)
f2()
# Driver's code
f1()
Indus University
print(cube(7))
print(cube_v2(7))
343
343
11 of 15 7/24/24, 14:54
Python functions & Types of function arguments in pytho... https://colab.research.google.com/drive/1AAb7PJ-xii5KoE...
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
print(factorial(4))
24
def square_value(num):
"""This function returns the square
value of the entered number"""
return num**2
print(square_value(2))
print(square_value(-4))
4
16
def myFun(x):
def myFun(x):
10
14 of 15 7/24/24, 14:54
Python functions & Types of function arguments in pytho... https://colab.research.google.com/drive/1AAb7PJ-xii5KoE...
y = 3
swap(x, y)
3
2
15 of 15 7/24/24, 14:54