0% found this document useful (0 votes)
23 views

XII CBSE Working With functions(Notes)

This document provides notes on working with functions in Python, explaining what functions are, their advantages, and the different types including built-in, module-defined, and user-defined functions. It covers the structure of functions, parameters and arguments, and the difference between them, as well as the concepts of local and global scope. Additionally, it discusses name resolution in Python using the LEGB rule.

Uploaded by

Banpinder Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

XII CBSE Working With functions(Notes)

This document provides notes on working with functions in Python, explaining what functions are, their advantages, and the different types including built-in, module-defined, and user-defined functions. It covers the structure of functions, parameters and arguments, and the difference between them, as well as the concepts of local and global scope. Additionally, it discusses name resolution in Python using the LEGB rule.

Uploaded by

Banpinder Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Note: Write the provided notes in CS notebook.

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

Python function types


There are three categories of functions:
• Built-in functions
• Function defined in modules
• User defined functions.

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()

Function defined in modules


These functions are pre-defined in particular modules and can only be used after the specific module is
imported.
E.g. All mathematical functions are defined in the module math.

User-defined functions
Python User Defined Functions allow users to write unique logic that the user defines. I

Structure of functions in Python


Defining a Function
A function in Python is defined as given below:
def< function name >(parameters):
<statements> [<statements>]
……………………..
• Keyword def that marks the start of the function header.
• A function name to uniquely identify the function. 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 the function header.
• One or more valid python statements that make up the function body. Statements must have
the same indentation level (usually 4 spaces).
• An optional return statement to return a value from the function.

Parameters and Arguments


The parameters are the variables that we can define in the function declaration. In fact, we utilized
these variables within the function. .
The arguments are the variables given to the function for execution. Besides, the local variables of the
function take the values of the arguments and therefore can process these parameters for the final
output.

1. Positional argument (required arguments):-


When the functions call statement must match the number and order of arguments as define in the
functions definition this is called the positional arguments.
Example:-
def check(a,b,c):
:
Then possible functions call for this can be:-
check(x,y,z) #3 values(all variables) passed
check(2,x,y) #3values(literal+variables)passed
check ( 2 , 3 , 4 ) # 3 values ( all literal ) passed.
Thus through such function calls-
• The argument must be provided for all parameters (required)
• The values of argument are matched with parameters, position(order)wise(positional)

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))

Calling function and Called function:


• Function which is called by another Function is called Called Function. The called function contains
the definition of the function and formal parameters are associated with them.
• The Function which calls another Function is called Calling Function and actual Parameters are
associated with them.
• In python, a function must be defined before the function call otherwise python interpreter gives
an error.

Difference between Arguments and parameters


These two terms are very interchangeable, so it is not so important to know the difference. The terms
they refer to are almost identical. However, in order to sound more professional, correct terminology is
important.
Function Parameters: Variables that are in brackets when defining the function. When a method is
called, the argument re the d t p ed to the method’ p r meter .
Function arguments: Arguments are used to pass information from the rest of the program to the
function. This information return a result. There is no limit to the number of arguments that can be
written. Depending on the type of function you’re performing, there might even be no Argument

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:

Name Resolution (Scope Resolution)


For every name used within program, python follows name resolution rules known as LEGB rule.
• Local Environment : first check whether name is in local environment, if yes Python uses its value
otherwise movesto (ii)
• Enclosing Environment: if not in local, Python checks whether name is in Enclosing Environment,
if yes Python uses its value otherwise moves to (iii)
• Global Environment: if not in above scope Python checksit in Global environment, if yes Python
uses it otherwise moves to (iv)
• Built-In Environment: if not in above scope, Python checks it in built-in environment, if yes,
Python uses its value otherwise Python would report the error: name <variable> notdefined.

You might also like