PYTHON PROGRAMMING (UNIT-4)
Q) Defining functions and calling functions?
Defining function:
A function is a reusable block of programming statements designed to
perform a certain task. To define a function, Python provides
the def keyword. The following is the syntax of defining a function.
Syntax:
def function_name(parameters):
statement1
statement2
...
...
return [expr]
The keyword def is followed by a suitable identifier as the name of the
function and parentheses. One or more parameters may be optionally
mentioned inside parentheses. The : symbol after parentheses starts an
indented block.
The first statement in the function body can be a string, which is called
the docstring. It explains the functionality of the function/class. The
docstring is not mandatory.
The function body contains one or more statements that perform some
actions. It can also use pass keyword.
Optionally, the last statement in the function block is the return statement.
It sends an execution control back to calling the environment. If an
expression is added in front of return, its value is also returned to the calling
code.
Calling function:
To call this function, write the name of the function followed by parentheses
in parameters . The parameters are optional:
myfunction(parameters)
example:
def display( ):
print(‘college’)
display( )
output: college
……………………………. END ………………..
Q) passing arguments ?
While defining a function in Python, you can pass argument(s) into the
function by putting them inside the parenthesis.
The basic syntax for doing this looks as shown below:
def functionName(arg1, arg2):
# What to do with function
Example:
def add(a,b)
sum=a+b
print(‘total:’,sum)
add(2,3)
output: total:5
………………… end …………..
Q) keyword arguments ?
You can also send arguments with the key = value syntax.
Keyword arguments (or named arguments) are values that, when passed into
a function, are identifiable by specific parameter names. A keyword argument
is preceded by a parameter and the assignment operator, = .
Example:
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = "reshma", child2 = "tejaswini", child3 = "geetha")
output:
The youngest child is geetha
……………………. End ……………
Q) variable length arguments?
Non keyword arguments:
*args (Non-Keyword Arguments)
The special syntax *args in function definitions in Python is used to pass a
variable number of arguments to a function. It is used to pass a non-
keyworded, variable-length argument list.
The syntax is to use the symbol * to take in a variable number of
arguments; by convention, it is often used with the word args.
What *args allows you to do is take in more arguments than the
number of formal arguments that you previously defined. With *args, any
number of extra arguments can be tacked on to your current formal
parameters (including zero extra arguments).
Example:
def arg_type_test(*args):
print(type(args))
arg_type_test(1, 2)
output:
<class 'tuple'>
Keyword arguments:
**kwargs (Keyword Arguments)
The special syntax **kwargs in function definitions in Python is used to pass
a keyworded, variable-length argument list. We use the name kwargs with
the double star. The reason is that the double star allows us to pass through
keyword arguments (and any number of them).
A keyword argument is where you provide a name to the variable as
you pass it into the function.
One can think of the kwargs as being a dictionary that maps each
keyword to the value that we pass alongside it. That is why when we
iterate over the kwargs there doesn’t seem to be any order in which they
were printed out.
Example:
def kwarg_type_test(**kwargs):
print(kwargs)
kwarg_type_test(a="hi")
kwarg_type_test(roses="red", violets="blue")
output:
{'a': 'hi'}
{'roses': 'red', 'violets': 'blue'}
…………………….. end ………….
Q) anonymous functions?
Lambda functions, also known as anonymous functions, are small, one-time-
use functions in Python.
Syntax of a Lambda Function:
lambda arguments: expression
Lambda functions can have any number of arguments but only one
expression.
a lambda function is created using the lambda keyword.
The keyword followed by one or more arguments.
An expression is provided for the [Link] is the part of the code that gets
executed/returned.
Example:
add=lambda num : num+4
print(add(6))
output: 10
………………. End …………..
Q) fruitful functions?
Fruitful function:
A function that returns a value is called fruitful function.
Example:
def add( ) :
a=15
b=24
c=a+b
return c
c=add( )
print( c )
output:39
Void Function
A function that perform action but don’t return any value.
Example:
def add( ):
a=10
b=20
c=a+b
print(c)
add( )
output: 30
……………………… end …………….
Q) scope of the variables in a function ?
A variable scope specifies the region where we can access a variable.
Based on the scope, we can classify Python variables into 2 types:
1. Local Variables
2. Global Variables
Local Variables:
When we declare variables inside a function, these variables will have a local
scope (within the function). We cannot access them outside the function.
Example:
def greet():
# local variable
message = 'Hello'
print('Local', message)
greet()
# try to access message variable
# outside greet() function
print(message)
output:
Local Hello
NameError: name 'message' is not defined
Global Variables:
In Python, a variable declared outside of the function or in global scope is
known as a global variable. This means that a global variable can be accessed
inside or outside of the function.
Example:
# declare global variable
message = 'Hello'
def greet():
# declare local variable
print('Local', message)
greet()
print('Global', message)
output:
Local Hello
Global Hello
……………….. end ………………..