Lect-01 Functions
Lect-01 Functions
● Types of Functions
3
Prof. Harish D. Gadade, COEP Technological University, Pune
Importance/Need of Function
● Understanding, coding and testing multiple separate functions is far
easier.
● Without the use of any function, there will be countless lines in the
code and maintaining it will be a big mess.
● Programmers use functions without worrying about their code details.
This speeds up program development, by allowing the programmer to
concentrate only on the code that he has to write.
● Different programmers working on that project can divide the workload
by writing different functions.
● Like Python libraries, programmers can also make their functions and
use them from different point in the main program or any other program
that needs its functionalities.
4
Prof. Harish D. Gadade, COEP Technological University, Pune
Function Definition
● Function blocks starts with the keyword def.
● The keyword def is followed by the function name and parentheses ((
)).
● After the parentheses a colon (:) is placed.
● Parameters or arguments that the function accept are placed within
parentheses.
● The code block within the function is properly indented to form the
block code.
● A function may have a return[expression] statement. That is, the
return statement is optional.
5
Prof. Harish D. Gadade, COEP Technological University, Pune
Function Definition
● Example:
Def add(x,y):
return(x+y)
Function Definition
a=10
b=20 Function Body
c=add(a,b)
print(c)
● Output: Function Call
30
6
Prof. Harish D. Gadade, COEP Technological University, Pune
Function Call
● The function call statement invokes the function. When a function is
invoked the program control jumps to the called function to execute
the statements that are a part of that function. Once the called
function is executed, the program control passes back to the calling
function.
● Function Parameters : A function can takes zero or more parameters
which are nothing but some values that are passed to it.
● Function name and the number and type of arguments in the function
call must be same as that given in the function definition.
● If the data type of the argument passed does not matches with that
expected in function then an error is generated.
7
Prof. Harish D. Gadade, COEP Technological University, Pune
Function Call
● Example:
def add(x,y):
return(x+y)
Function Definition
a=10
b=20 Function Body
c=add(a,b)
print(c)
● Output: Function Call
30
8
Prof. Harish D. Gadade, COEP Technological University, Pune
Function Examples
● WAP to compute & print area of a circle, area of triangle
and area of square using user-defined function.
● WAP for linear search using user defined function
● WAP to reverse an elements of a list without using indexing
● WAP to to find MAX and Second Max element from a list
9
Prof. Harish D. Gadade, COEP Technological University, Pune