Unit 4 - Functions in Python
Unit 4 - Functions in Python
saadsheriff1895@gmail.com
J732KOTCSB
Program: MCA
Specialization: Data Science
Semester: 2
Course Name: Python for Data Science
Course Code: 21VMT5S204
Unit Name: Functions in Python
Functions:
Functions are used when we want to call a particular sets of codes in different
parts of a program. For instance, when we want to check if the addition of two
numbers more than once in a program. It is easy after function declaration,
which is done once, and then the function is simply called wherever it is
Proprietary content. All rights reserved. Unauthorized use or distribution prohibited.
saadsheriff1895@gmail.com
J732KOTCSBUser-defined functions:
While there are a lot of pre-defined functions, there can be a lot of other sets
of code that a user might want to repeat in a program, that are not pre-
defined. For purposes like these, user-defined functions come in handy.
Through a user-defined function, a programmer can write his set of code in the
block function and call it everywhere in a program, whenever it has to be
reused.
Syntax of a function:
- This block is called the function definition. It is the part where you
write all the features and codes of the function.
- ‘def’ is a keyword in the python. It is short for define/definition.
- It is followed by functionName which is the name of the function that
is going to be defined by the user.
- functionName is followed by a parenthesis which contains
arguments. Arguments are values of variables that are used within
the function.
These arguments are also called local variable since they can only be
used within the block of codes of the function. They go unrecognised
outside the function. However, one must note, it is not compulsory to
write arguments in a python function.
- The arguments or the parenthesis followed by a colon. Inside the
block enter all the conditions/statements for the case that the
function was created.
saadsheriff1895@gmail.com
J732KOTCSB
saadsheriff1895@gmail.com
J732KOTCSB
lambda function:
lambda function is a function written in a single line. it does not have any
name, and can take any number of arguments but it can only have one
expression in it. It is called Anonymous function because it is nameless.
One must note that the output for a one line expression given by both, the
lambda function and a normal def function are the same. def is usually used
when the function has multiple lines of code.
A lambda function does not have a return statement. Since it is a single line
function, when executed, the value of the expression is displayed in the
output.
Example:
saadsheriff1895@gmail.com
J732KOTCSB
In the above example, the lambda function is written as a def function. The
output is the same, however, the number of lines are more when written
inside the def function.
Example: With arguments, without return
1. Calculator, factorial of a number, and reverse of a number
To make a basic calculator using functions.:
saadsheriff1895@gmail.com
J732KOTCSBIn the above example, the function calculator contains numerous arithmetic
operations. Then the function is called, with arguments as a=20 and b=5. When
executed, it gives the output for each operation. A lambda function is stored in
variable x. since only one expression can be evaluated in the lambda function,
an amalgamation of operations is used with local variables a and b. Evaluation
of arithmetic operations happens in a left to right order. The solution is then
printed.
saadsheriff1895@gmail.com
J732KOTCSBIn the above example, when function factorial is executed with 8 as argument
value, the interpreter goes inside the function defined factorial, with local
variable a. Thus, a=8. Another local variable is assigned s=1 inside factorial. The
loop is then executed, since the condition is fulfilled, i.e. 8 !=0, the loop is
entered. s is reassigned as s*a, and then a is decremented. The loop keeps
repeating until the value of a is 0, when that condition is not fulfilled, the loop
will be terminated and then the factorial stored in s is displayed.
Examples: Without arguments, with return
1. Finding the sum of all multiple of 5 between 50-200:
saadsheriff1895@gmail.com
J732KOTCSB
The same factorial problem is executed without arguments and with return.
num is used as the local variable instead of entering arguments.
Number 1: 12321
saadsheriff1895@gmail.com
J732KOTCSB
Number 2: 12345
Example 2:
Armstrong number (without arguments, without return)
When the sum of the cube of every digit of a number is equal to the original
number, it is called an Armstrong number.
Output:
saadsheriff1895@gmail.com
J732KOTCSB
Number= 153
= > 13 + 53 + 33
= > 153. Thus, the number is an Armstrong number.
saadsheriff1895@gmail.com
J732KOTCSB