Please refer to Functions Class 12 Computer Science Notes and important questions below. The Class 12 Computer Science Chapter wise notes have been prepared based on the latest syllabus issued for the current academic year by CBSE. Students should revise these notes and go through important Class 12 Computer Science examination questions given below to obtain better marks in exams

## Functions Class 12 Computer Science Notes and Questions

The below Class 12 Functions notes have been designed by expert Computer Science teachers. These will help you a lot to understand all the important topics given in your NCERT Class 12 Computer Science textbook. Refer to Chapter 4 Functions Notes below which have been designed as per the latest syllabus issued by CBSE and will be very useful for upcoming examinations to help clear your concepts and get better marks in examinations.

**Learning Outcomes: Understand the concept of functions in Python.**

**Definition:** Functions are the subprograms that perform specific task. Functions are the small modules.

**Types of Functions:**

There are three types of functions in python:

Library Functions: These functions are already built in the python library.

Functions defined in modules: These functions defined in particular modules. When you want to use these functions in program, you have to import the corresponding module of that function.

User Defined Functions: The functions those are defined by the user are called user defined functions.

**Library Functions in Python:**

These functions are already built in the library of python.

For example: type( ), len( ), input( ) etc.

**Functions defined in modules:****Functions of math module:**

To work with the functions of math module, we must import math module in program.

**import math img**

S. No. | Function | Description | Example |

1 | sqrt( ) | Returns the square root of a number | math.sqrt(49) 7.0 |

2 | ceil( ) | Returns the upper integer | math.ceil(81.3) 82 |

3 | floor( ) | Returns the lower integer | math.floor(81.3) 81 |

4 | pow( ) | Calculate the power of a number | math.pow(2,3) 8.0 |

5 | fabs( ) | Returns the absolute value of a number | math.fabs(-5.6) 5.6 |

6 | exp( ) | Returns the e raised to the power i.e. e3 | math.exp(3) 20.085536923187668 |

**Function in random module:**randint( )- function generates the random integer values including start and end values.

Syntax: randint(start, end)

It has two parameters. Both parameters must have integer values.

Example:

import random n=random.randint(3,7)

*The value of n will be 3 to 7.

**User defined functions:**

**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:**

def display(name):

print(“Hello ” + name + ” How are you?”)

**Function Parameters:**

**A functions has two types of parameters:**

Formal Parameter: Formal parameters are written in the function prototype and function header of the definition. Formal parameters are local variables which are assigned values from the arguments when the function is called.

Actual Parameter: When a function is called, the values that are passed in the call are called actual parameters. At the time of the call each actual parameter is assigned to the corresponding formal parameter in the function definition.

Default Parameters: Python allows function arguments to have default values. If the function is called without the argument, the argument gets its default value.**Example :**

def ADD(x, y): #Defining a function and x and y are formal parameters

z=x+y

print(“Sum = “, z)

a=float(input(“Enter first number: ” ))

b=float(input(“Enter second number: ” ))

ADD(a,b) #Calling the function by passing actual parameters

In the above example, x and y are formal parameters. a and b are actual parameters.

**Calling the function:**

Once we have defined a function, we can call it from another function, program or even the Python prompt. To call a function we

simply type the function name with appropriate parameters.**Syntax:**

function-name(parameter)**Example:**

ADD(10,20)

OUTPUT:

Sum = 30.0

**The return statement:**

The return statement is used to exit a function and go back to the place from where it was called.

There are two types of functions according to return statement:

a. Function returning some value

b. Function not returning any value

a. Function returning some value :

Syntax:

return expression/value**Example-1: Function returning one value**

def my_function(x):

return 5 * x**Example-2 Function returning multiple values:**

def sum(a,b,c):

return a+5, b+4, c+7

S=sum(2,3,4) # S will store the returned values as a tuple print(S)

**OUTPUT: (7, 7, 11)****Example-3: Storing the returned values separately:**

def sum(a,b,c):

return a+5, b+4, c+7

s1, s2, s3=sum(2, 3, 4) # storing the values separately

print(s1, s2, s3)**OUTPUT: 7 7 11**

b. Function not returning any value : The function that performs some operations but does not return any value, called void function.

def message():

print(“Hello”)

m=message()

print(m)

OUTPUT: Hello

None

**Scope and Lifetime of variables:**

Scope of a variable is the portion of a program where the variable is recognized. Parameters and variables defined inside a function is not visible from outside. Hence, they have a local

scope.**There are two types of scope for variables:**

i) Local Scope

ii) Global Scope

Local Scope: Variable used inside the function. It cannot be accessed outside the function. In this scope, the lifetime of variables inside a function is as long as the function executes. They are destroyed once we return from the function. Hence, a function does not remember the value of a variable from its previous calls.

Global Scope: Variable can be accessed outside the function. In this scope, Lifetime of a variable is the period throughout which the variable exits in the memory.**Example:**

def my_func(): x = 10

print(“Value inside function:”,x)

x = 20

my_func()

print(“Value outside function:”,x)**OUTPUT:**

Value inside function: 10

Value outside function: 20

Here, we can see that the value of x is 20 initially. Even though the function my_func()changed the value of x to 10, it did not affect the value outside the function.

This is because the variable x inside the function is different (local to the function) from the one outside. Although they have same names, they are two different variables with different scope.

On the other hand, variables outside of the function are visible from inside. They have a global scope.

We can read these values from inside the function but cannot change (write) them. In order to modify the value of variables outside the function, they must be declared as global variables using the keyword global.

**Learning Outcomes: Understand the concept of functions and recursion.**

**• Definition:** Recursion refers to a programming technique in which function calls itself either directly or indirectly.

• Python program to find ab using recursion:

def power(a,b):

if(b= =0):

return 1

else:

return a*power(a,b-1)

n=int(input(“enter the number: “))

p=int(input(“enter the power: “))

print(“Power of “,n,”to”,p,”is:”,power(n,p))**OUTPUT:**

enter the number: 3

enter the power: 4

Power of 3 to 4 is: 81

• Python program to find the factorial of a number using recursion:

def factorial(n):

if n == 1:

return n

else:

return n*factorial(n-1)

num=int(input(“enter the number: “))

if num < 0:

print(“Sorry, factorial does not exist for negative numbers”)

elif num = = 0:

print(“The factorial of 0 is 1”)

else:

print(“The factorial of “,num,” is “, factorial(num))

OUTPUT:

enter the number: 5

The factorial of 5 is 120

• Python program to print the Fibonacci series using recursion:

def fibonacci(n):

if n<=1:

return n

else:

return(fibonacci(n-1)+fibonacci(n-2))

num=int(input(“How many terms you want to display: “))

for i in range(num):

print(fibonacci(i),” “, end=” “)

OUTPUT:

How many terms you want to display: 8

0 1 1 2 3 5 8 13

**• Binary Search:** It is a popular algorithm that searches the given element in minimum possible comparisons. It searches a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half.

Repeatedly check until the value is found or the interval is empty.**Note:** The given array or sequence must be sorted to perform binary search.

• Python program to find an element in an array using Binary search recursively:

def binarySearch(list, beg, end, x):

while beg<= end:

mid = beg + (end – beg)//2;

# Check if x is present at mid

if list[mid] == x:

return mid

# If x is greater, ignore left half

elif list[mid] < x:

beg = mid + 1

# If x is smaller, ignore right half

else:

end = mid – 1

return -1

list = [ 2, 3, 4, 10, 40 ]

x = 10

position = binarySearch(list, 0, len(list)-1, x)

if position != -1:

print( “Element is present at index” , position)

else:

print (“Element is not present in list”)