CHAPTER-3 Working with functions notes
CHAPTER-3 Working with functions notes
XII – Science
Computer Science (083)
CHAPTER: 3 Working with Function
Function: Functions are the subprograms that perform specific task. Functions are the
small modules.
Types of Functions:
Built-in Functions
1. Built-in functions: These are pre-defined functions and are always available for use.
Eg: type( ) , int( ), float( ), len( ),print( ) etc.
3. User Defined Functions: The functions those are defined by the user are called user
defined functions.
Defining function in Python:
➢ Statement to perform operation written as a separate block with function name /
header is called function definition.
➢ A function once defined can be invoked as many times as needed by using its name,
without having to rewrite its code.
➢ The syntax to define a function is: def function-name ( parameters) :
statement(s)
:
➢ Where:
• Keyword def marks the start of function header.
• A function name to uniquely identify it. Function naming follows the same rules of
writing identifiers in Python.
• Parameters (arguments) through which we pass values to a function. They are optional.
• A colon (:) to mark the end of function header.
• One or more valid python statements that make up the function body. Statements must
have same indentation level.
• An optional return statement to return a value from the function.
• Example:
(i) Function with parameter:
def display(name):
print("Hello " + name + " How are you?")
(ii) Function without parameter:
def greet( ):
print( “Good Morning”)
❖ Some important terms:
1. Function Header: The first line of function definition that begins with keyword def and
ends with a colon (:), it specifies the name of the function & its parameters.
2. Parameters: variables that are listed within the parentheses of function header.
3. Function Body: The block of statement inside function header that defines the action
performed by the function. The function body may or may not return any value.
4. Indentation: The blank space in the beginning of a statement within a block.
• Note: The function definition does not execute the function body; this gets executed
only when the function is called or invoked.
Calling / invoking / using a function:
➢ To use a function that has been defined earlier, you need to write a function call
statement.
➢ Syntax: <function_name>(<value to be passed to argument>)
➢ Eg: if want to call function display defined above, function call statement will be:
display(“Rohan”)
➢ Carefully notice that number of values being passed is same as number of parameters.
➢ Variables/values passed using parentheses ( ) , in function definition is a function
parameters and in function call is argument.
➔ Now, its function call statement would be similar to ones shown below:
1. Passing literal as argument:
cube (4) #pass value 4 to variable X
2. Argument (Actual Parameter): When a function is called, the values that are passed
in the call are called argument also called actual parameters. At the time of the call
each actual parameter is assigned to the corresponding formal parameter in the
function definition.
Passing Parameters:
➢ The number of argument should be same as number of parameters required.
➢ Python supports three types of formal argument/parameter:
1. Positional arguments (Required arguments)
2. Default arguments
3. Keyword arguments (Named arguments)
1. Positional/Required arguments:
➢ When the function call statement must match the number of argument and order
of arguments as defined in the function definition, this is called the positional
argument matching.
➢ Eg: def check(a,b,c)
Function call: check (x,y,z) OR check(2,x,y)
2. Default arguments:
➢ Python allows us to assign default values to a function’s parameters which are
useful in case a matching argument is not passed in the function call statement.
➢ It is useful when actual parameters are/is skipped during function call.
➢ The default values are specified in the function header of function definition.
➢ The default values are considered only if no value is provided for that parameter in
the function call statement.
➢ “A parameter having default value in the function header is known as a default
parameters”
➢ Eg: def interest(principal,time,rate=0.10)
➢ Function call: si_int = interest(5000,2,0.5)
✓ Here no value is skipped in function call. hence it will not use default value and
the value for variable is principal =5000, time=2, rate=0.5
➢ But if the function call, si_int = interest(5000,2)
✓ Here, the value for rate i.e., last value is missing. Hence it will use default value
0.10 of rate. Now the values are principal = 5000, time = 2 , rate = 0.10
• Important: In a function header, any parameter cannot have a default value unless all
parameters appearing on its right have their default values.
• EG:
i. def interest(principal, time =2 , rate) #Invalid
ii. def interest(principal=5000, time = 2, rate = 0.10) #valid
iii. def interest (principal=5000, time, rate) #Invalid
iv. def interest(principal=5000, time = 2, rate) #Invalid
v. def interest(principal, time , rate = 0.10) #valid
vi. def interest(principal, time = 2, rate = 0.10) #valid
❖ Advantage of default parameter:
➢ Default parameters can be used to add new parameters to the existing function.
➢ It can be used to combine similar functions into one.
3. Keyword / Named Argument:
➢ Default arguments give you flexibility to specify the default value for parameter so
that it can be skipped in the function call if needed.
➢ However, still you cannot change the order of the arguments in the function call. You
have to remember the order of the arguments.
➢ To have complete control over the values, Python offers another type of arguments:
keyword argument
➢ “Keyword arguments are the named arguments with assigned value being passed in
the function call statement.”
➢ Eg: si_int =interest( rate = 0.10, principal = 5000, time = 2)
o Here, you can change the order of parameters because you used keyword
argument i.e. value passed with its name.
Composition:
➢ “Composition in general refers to using an expression as a part of a large expression
or statement as a part of large statement.”
➢ An arithmetic expression: greater((4+5),(3+4))
➢ A logical expression: test (a or b)
➢ Function call as part of large function call i.e., composition : int(str(53))
Scope of variable:
➢ “part of program within which a name is legal and accessible, is called scope of
variable.”
➢ There are broadly two kinds of scopes:
i) Global scope ii) Local scope
Built-in namespace 4
Global namespace 3
Enclosing namespace 2
Local namespace 1
❖ Some Examples:
• Case: 1 Variable in global scope:
def sum( x , y):
z=x+y
print(n1,n2) #it will first check local scope and enclosing scope but variable
n1 and n2 are not in local or enclosing environment so now it will
check global environment and prints its value.
return z
n1 = int(input( “Enter first number:”))
n2 = int(input( “Enter second number:”))
s = sum(n1 , n2)
print(s)
• Case: 2 Variable neither in local scope nor in global scope:
def greet( ) :
print(“hello”, name)
print(“Hi”)
greet( )
➔ If variable used in code is neither assigned in local nor in global then will Python return
ERROR.
• Important: Once a variable is declared global in function, you cannot undo the
statement. One should avoid using global statement in Python. After a global statement,
the function will always refer to global variable.
Output:
List before function call: [1]
List received is: [1]
The value got changed
Value now changes to: [3]
from [1] to [3] inside
Value after returning from function: [3]
function and change GOT
REFLACTED to __main__
B) Passing a Mutable Type Value to a function – Adding/deleting items to it
def myfun(a):
print(“List received is:”,a)
a.append(2)
a.extend([5,1])
print (“List after adding some elements:”, a)
a.remove(5)
print (“List after all changes:”, a)
#__main__
List = [1]
print(“List before function call: “, List)
myfun(List)
print(“List after function call: “, List)
Output:
List before function call: [1]
List received is: [1]
Value now changes to: [1,2,5,1]
Output:
List before function call: [1 , 4]
List received is: [1 , 4]
List after all changes: [3 , 5, 6] The value got changed from [1, 4]
to [3, 5, 6] inside function and
List after function call: [1 , 4] change DID NOT GET REFLACTED
to __main__