PP_UNIT-3(PART-2)
PP_UNIT-3(PART-2)
PP_UNIT-3(PART-2)
Functions
Function:
Functions are self-contained chunks of code that perform a particular task.
(OR)
Functions are self-contained block of statements that perform a particular task.
• For every function, it has name that identifies what it does and this name is used to call
the function to perform its task when needed.
• Function may have arguments and many return values.
• Python programming has a lot of built-in-functions.
The basic form of a function in python is:
• All function definitions begin with the keyword “def”, followed by the function name
and the argument list in the parenthesis.
• The closing parenthesis of the argument list is followed by colon.
• Finally, there will be a bunch of statements going under the function with same level of
indentation like the ‘if-statements’
Note 1: def keyword is used to define function
Note 2: The arguments are the information that you pass into the function.
General syntax of keyword def is as follows:
def function_name(parameters):
statement-1
statement-2
----------------
----------------
----------------
Note 1: There is no need to specify the datatype of the parameters
Note 2: A function can take any number and the type of input parameters
Note 3: A function can return any number and any type of result
Uses of Functions:
• Code reusability
• Breaking a big program into smaller functions, the different programmers working on
that project can divide the work load.
We can do two things with function:
• define it
• call it
Example:
# Function to say hello on call the function
def greet():
print(“Hello”)
#Function call
greet()
Output:
Hello
Note 1: The names given in the function define are called parameters.
Note 2: The values we specify in the function call are called arguments.
Example:
def add(a,b):
c=a+b
return c
print(add(10,20))
In the above program
a, b are parameters
10, 20 are arguments
Example:
def greet(name):
print(“Hello {}” .format(name))
greet(“Raju”)
greet(“Hari”)
Note: When we call a function with arguments, the values of those arguments are copied to
their corresponding parameters inside the function.
Example:
def greet(name,gender):
if gender is ’f’:
print(“Hello Ms.{ }” .format(name))
elif gender is ‘m’:
print(“Hello Mr.{ }” .format(name))
else:
print(“Hello { }” .format(name))
greet(“raju”,”m”)
greet(“machine”,””)
greet(“radha”,”f”)
Function calling syntax:
function_name()
(or)
function_name(variable1, variable2, …………..)
Example:
def add(a,b):
c=a+b
return c
print(“Sum of two numbers is “,add(10,20))
arguments
Function arguments:
You can call a function by using the following types of arguments
• Positional (or) required arguments
• Keyword arguments
• Default arguments
• Variable-length arguments
Positional arguments:
Positional arguments are arguments whose values are copied to their corresponding
parameters in order.
Keyword Arguments:
To avoid the positional argument confusion, you can specify arguments by the names of
their corresponding parameters. (Even it is in different order)
It helps this function to identify the arguments by the parameter name.
Default Arguments:
Python allows users to specify default values for parameters.
The default value is used if the caller does not provide a corresponding argument
Note: A default argument assumes a default value if a value is not provided in the function
call for that argument.
Note: You can specify any no. of default arguments in your function
Note: If you have default arguments, then they must be written after the non-default
arguments.
Variable-length argument:
• In some situations, it is not known in advance how many arguments will be passed to a
function.
• In such cases, python allows programmer to make function call with arbitrary no. of
arguments (or) Variable-length arguments.
• When we use arbitrary arguments (or) variable-length argument, then the function
definition uses an asterisk(*) before the parameter name.
Syntax:
def function_name(arg1, arg2, arg3, ……………….):
function statements
return statement
def emp_details(name,age,salary=20000):
def print_args(*arg):
Uses:
1. We use this function when we required nameless function for a short period of time
2. In python we generally use it as an argument to a higher order function
3. Lambda functions are used along with the built in functions like filter and map
A higher order function (HOF) is a function that follows at least one of the following
conditions −
Example:
mylist={1,5,6,4,7,8,12}
newlist = list(filter(lambda x:x%2==0,mylist))
print(newlist)
e.g.
mylist={1,2,3,4,5,6}
newlist = list(map(lambda x:x*2,mylist))
print(newlist)
A recursive function is a function that calls itself during its execution. This enables
the function to repeat itself several times, outputting the result and the end of each iteration.
Example:
def factorial(i):
if i == 1:
return 1
else:
return (i * factorial(i-1))
number = 9
Name spacing:
It is a mapping from names to objects
Namespace is dictionary of variable names (keys) and their corresponding objects (values)
Names Objects
x=3
y=4 x 3
x=y y 4
Request
Write a program that allows the user to obtain information about the file system.
Analysis
File systems are tree-like structures, as shown in below figure
Program: