W9 Lecture 0
W9 Lecture 0
FUNCTIONS
PYTHON PROGRAMMING
DEFINITION
CEC-Swayam
WHY FUNCTION ?
Less Development
Easy Debugging
Time
CEC-Swayam
SYNTAX OF FUNCTION DEFINITION
return[expression]
CEC-Swayam
EXAMPLE
def function():
print("Hello World")
•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.
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.
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.
CEC-Swayam
Default argument allow to specify a value
for a parameter.
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