0% found this document useful (0 votes)
3 views18 pages

W9 Lecture 0

Uploaded by

rajfriend
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)
3 views18 pages

W9 Lecture 0

Uploaded by

rajfriend
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/ 18

CEC-Swayam / EMRC Dibrugarh University

Course- Programming in Python


Week-9

FUNCTIONS
PYTHON PROGRAMMING
DEFINITION

• A block of program code which


can be resued. It is an organized
block of code that can perform a
single, specific, and a well-
defined task
CEC-Swayam
FUNCTION CALLING

main program def function():


…...... #function code
• 2.8162.816
function() …....
….... …....
….... function ends

CEC-Swayam
WHY FUNCTION ?

Code Reusability Modularity

Less Development
Easy Debugging
Time

CEC-Swayam
SYNTAX OF FUNCTION DEFINITION

Function header parameters

def function_name(variable1, variable2,…)


documentation string
statement block Function body

return[expression]
CEC-Swayam
EXAMPLE

def function():
print("Hello World")

function() #function call


CEC-Swayam
FUNCTION PARAMETERS

• The name of the function while calling and the


number of arguments must match with the function
definition.
• If there is a mismatched in parameters passed and
declared, then an error will be returned.
• If the data type of the parameters passed doenot
match with that of the function, then an error is
generated.
CEC-Swayam
TYPES OF FUNCTIONS IN PYTHON

•Built-in functions
•User-Defined Functions (UDFs)
•Anonymous functions (lambda functions)
CEC-Swayam
Lambda functions are not declared
using def keyword. Instead, lambda
keyword is used.

Lambda functions are required at


LAMBDA OR place where they have been created
ANONYMOUS in the program.
FUNCTION Any number of arguments ca be
supplied to lambda function, but it
must a single expression.

CEC-Swayam
lambda arguments :
expression

SYNTAX z = lambda x, b : x* b
print(z(7, 18))

x = lambda m, n, c: m + n + c
print(x(6, 2, 7))
CEC-Swayam
Lambda functions have no name.

Lambda function cannot access variables


other than those in their parameter list.

POINTS Lambda function can take N number of


arguments.

Lambda function doesnot have any


return statement.

CEC-Swayam
FUNCTION ARGUMENTS
You can call a function by using the following types of formal arguments −

Required arguments

Keyword arguments

Default arguments

Variable-length arguments

CEC-Swayam
Arguments must be passed on to a
function in correct order.

REQUIRED
ARGUMENTS
The number of arguments passed to a
function must match with the number
with the number of arguments
specified in the function definition.

CEC-Swayam
Keyword argument when used helps to
identify the arguments by the name of the
parameter.

The order of the keyword argument is not


important.
KEYWORD
ARGUMENTS The keyword arguments passed must match
with one of the arguments of the accepting
function.

Keyword arguments make the program code


less complex and easy to understand.

CEC-Swayam
Default argument allow to specify a value
for a parameter.

This allows to call a function with less


DEFAULT numeber of arguments than defined.
ARGUMENTS
Any number of default arguments can be
defined.

Non default argument cannot be followed


by the default argument.
CEC-Swayam
In cases where it is not known in prior
how many arguments will be passed to a
function python allows to make function
VARIABLE call with arbitrary number of arguments.

LENGTH
ARGUMENTS
An asterisk (*) symbol is used before the
parameter name .

CEC-Swayam
Scope of a variable is defined
by the part of the program
where a variable is accessible.
VARIABLE SCOPE
AND LIFETIME
The time for which a varible
exist is called its lifetime.

CEC-Swayam
• Global variables are defined in the main body of
the program and can be used throughout the
program. They are also accesible by all the
function in the program.
GLOBAL AND
• Local Variables are defined within the function
LOCAL VARIABLE and their scope is within that function only. They
are not related with the same name variable
defined outside the function.

CEC-Swayam

You might also like