0% found this document useful (0 votes)
21 views9 pages

Lect-01 Functions

Uploaded by

YASH WANKHEDE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views9 pages

Lect-01 Functions

Uploaded by

YASH WANKHEDE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Functions in Python

Dr. Harish D. Gadade


Functions in Python
● Introduction

● Importance of design of functions

● Types of Functions

● Rewriting earlier solutions using functions

● Parameters and Arguments in a Function(Positional Arguments, Keyword


Arguments,Parameter with Default Values)

● Return Statements(Returning multiple values,Assign Returned Multiple


Values to Variables)

● local and global scope,


2
Prof. Harish D. Gadade, COEP Technological University, Pune
Introduction to Function
● Python enables its programmers to break up a program into segments
commonly known as functions, each of which can be written more or less
independently of the others. Every function in the program is supposed
to perform a well-defined task.

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

You might also like