XII CBSE Working With functions(Notes)
XII CBSE Working With functions(Notes)
CHAPTER NOTES
WORKING WITH FUNCTIONS
FUNCTION
A function is a programming block of codes which is used to perform a single, related, specific task. It
only works when it is called. We can pass data, known as parameters, into a function. A function can
return data as a result.
Advantages of Functions
• Reducing duplication of code
• Decomposing complex problems into simpler pieces
• Improving clarity of the code
• Reuse of code
• Information hiding
Built-in functions
The functions whose functionality is predefined in Python are referred to as built-in functions.
The python interpreter has several such functions that are always available for use.
E.g. len(), type(), int(), input()
User-defined functions
Python User Defined Functions allow users to write unique logic that the user defines. I
2. Default arguments:-
A parameter having defined value in the function header is known as a default parameter.
Example:-
def interest(principal, time, rate=10):
si=interest(5400,2) #third argument missing
So the parameter principal get value 5400, time get 2 and since the third argument rate is missing,
so default value 0.10 is used for rate.
Default arguments are useful in situations where some parameters always have
same value.
3. Keyword(or named)arguments:-
Keyword arguments are the named arguments with assigned values being passed in the function call
statement.
Example:-
def interest(prin, time, rate):
return prin * time * rate
print (interest ( prin = 2000 , time = 2 , rate 0.10 )) 28
print (interest ( time = 4 , prin = 2600 , rate = 0.09 ))
print(interest(time=2,rate=0.12,prin=2000))
In the example below, the variable name is the input parameter, whereas the value, “Arn v”, passed
inthe function call is the argument.
def welcome(name):
print("Hello! " + name + " Good Morning!!")
welcome("Arnav")
Output:
Hello! Arnav Good Morning!!
Returning a Function
Scope of variables
Scope means in which part(s) of the program, a particular piece of code or data is accessible or known.
In python there are broadly 2 kinds of Scopes:
• Global Scope
• Local Scope
Global Scope:
A name declared in top level segment (_main_) of a program is said to have global scope and
can be used in entire program.
• Variable defined outside of the all functions are global variables.
Local Scope:
• A name declared in a function body is said to have local scope i.e. it can be used only within this
function and the other block inside the function.
• The formal parameters are also having local scope.
Local variable: A variable that is defined within a function and can only be used within that
particular function.
Global variable: A variable that is defined in the main part of the programming and, unlike local
variables, can be accessed via local and global areas.
Example: