Chapter 2 : FUNCTIONS
CBSE Syllabus 2023-24 (Functions)
![]() |
- A function is a block of code that performs a specific task.
- A function usually performs a single task of a large program.
- Functions help in the division of our program into smaller, modular portions known as divide and conquer approach.
Definition: A function is a named sequence of statement(s) that performs a computation. It contains line of code(s) that are executed sequentially from top to bottom by Python interpreter. They are the most important building blocks for any software in Python.
Structure of Python program with functions:
Types of functions:
1. Built-in functions: Function(s) that are built into Python and can be accessed by a programmer.
Examples: input(), print(), sin(), max(L) etc.
Examples: input(), print(), sin(), max(L) etc.
2. Functions defined in module: Function(s) defined in separate python file called module. (The module in imported in python program.)
Examples: math module, random module, statistics module etc.
3. User defined functions: Complete function(s) are defined by the programmer.
User defined functions: Functions which are created by the programmer to fulfill the specific requirement.
Creating a User defined function:
Structure of User defined Function:
def <function-name>(<parameter list>):Statement1Statement2..................................return <variable>
Here 'def' and 'return' are the Python keywords.
Example:
def addNum(a,b):s = a + breturn s
addNum(5,10)
addNum is the name of function which is used to call/ execute the function. a and b are the variables which acts as parameters (inputs) to the function. return is used to send the value (output) from where the function is called.
Types of functions:
1. Function without parameters and without return value(s)
2. Function with parameters but without return value(s)
3. Function without parameter but with return value(s)
4. Functions with parameters and with return value(s)
Arguments and Parameters:
Parameters: The variables defined in function are called parameters. In above example, a and b are the parameters.
Arguments: Arguments are the values which are used to send values to function. In above example, 5 and 10 are the arguments.
Types of Parameters/ arguments:
1. Positional Parameters/ arguments
2. Default parameters
3. Keyword parameters (as per CBSE curriculum 2023-24, this is beyond the syllabus)
1. Positional Parameters/ arguments: Positional arguments are arguments that need to be included in the proper position or order. The first positional argument always needs to be listed first when the function is called. The second positional argument needs to be listed second and the third positional argument listed third, etc.
e.g. if there is a function f1(a,b,c) then the calling statement f1(4,7,2) will assign the value 4 to a, 7 to b and 2 to c. Also it is mandatory to give 3 values as argument.
2. Default parameters: Functions can be specified with default arguments. If values for these arguments are not supplied when the function is called, the default values are used.
e.g. if a function is defined as f1(a, b=5, c=3). This function can be called like
f1(10) OR f1(10,20) OR f1(10,20,30)
f1(10) OR f1(10,20) OR f1(10,20,30)
In the calling statement f1(10) : a = 10 , b = 5 and c = 3
In the calling statement f1(10,20) : a = 10 , b = 20 and c = 3
In the calling statement f1(10,20,30) : a = 10 , b = 20 and c = 30
3. Keyword argument: A keyword argument is an argument passed to a function or method which is preceded by a keyword and an equals sign. The general form is:
f1(keyword=value)
e.g. consider a function f1(a, b, c), we can call this function by specifying the variable name with value so we can make sure that the values will be passed to the required variable only even the sequence is changed (unlike positional parameters where we have to give the values in proper order).
The function f1(a, b, c) can be called as f1(b=4, a=6, c=10).
Function returning values:
Any value(s) can be send to the program from where the function is called. This is called returning values from the function. Value(s) can be retuned from the function using return keyword.
Example:
def add(a,b):
sum = a + b
return sum
s = add(4,5)
print("Sum of numbers =",s)
Note:
1. return statement can return an expression as well
e.g. return x+y-z
2. return statement can return multiple values
e.g. return 3,4,5 returns a tuple (3,4,5)
Scope of a variable:
The scope of a Python variable refers to the area where we may locate it and, if necessary, access it.
Global and Local variable:
Global variables are those we declare and define outside of any functions. Their values are available throughout the program.
Local variables are declared inside the functions. Their values are available inside that function only.
Global Keyword:
If we need to create a global variable which is actually local to any function, then we can use the keyword global.
Example:
global x
x = 300
myfunc()
print(x)
OUTPUT
300
def area_circle(r):
a = 3.14*r*r
return a
def area_rectangle(l,b):
a = l*b
return a
def area_triangle(b,h):
a = 0.5*b*h
return a
while True:
print("\n\nEnter your choice")
print("1. Area of Triangle")
print("2. Area of rectangle")
print("3. Area of Circle")
print("4. Exit")
ch=int(input())
if ch==1:
x=float(input("Enter base "))
y=float(input("Enter height "))
print("Area=",area_triangle(x,y))
if ch==2:
x=float(input("Enter length "))
y=float(input("Enter breadth "))
print("Area=",area_rectangle(x,y))
if ch==3:
radius=float(input("Enter radius "))
print("Area=",area_circle(radius))
if ch==4:
exit()
No comments:
Post a Comment