Computer Science

Functions in Python Class 12 CBSE (083): Complete Notes with Examples

Class 12 · Computer Science

Functions in Python (CBSE Class 12 Computer Science - 083)

Functions are one of the most important concepts in Python programming. A function is a reusable block of code that performs a specific task. Instead of writing the same code repeatedly, we can define it once inside a function and call it whenever required.

Learning Objectives

  • Understand the concept of functions.
  • Learn different types of functions.
  • Create user-defined functions.
  • Understand arguments and parameters.
  • Learn positional and default parameters.
  • Understand functions returning values.
  • Understand flow of execution.
  • Learn local and global scope of variables.

What is a Function?

A function is a named block of statements that performs a particular task. Functions improve code readability, reduce duplication, and make programs easier to maintain.

Advantages of Functions

  • Reduces code repetition.
  • Makes programs modular.
  • Improves readability.
  • Easier debugging.
  • Easy maintenance.
  • Code can be reused multiple times.

Types of Functions

Python mainly provides three types of functions.

Type Description Example
Built-in Functions Functions already available in Python. print(), len(), max()
Functions Defined in Modules Functions available after importing a module. math.sqrt(), random.randint()
User-defined Functions Functions created by the programmer. def add():

1. Built-in Functions

These functions are already available in Python.

Examples


print("Hello")

x = len("Computer")
print(x)

print(max(12,45,21))

print(min(5,9,2))

Output


Hello
8
45
2

2. Functions Defined in Modules

Python provides many modules containing useful functions. These functions become available after importing the required module.

Example 1


import math

print(math.sqrt(49))

Output


7.0

Example 2


import random

print(random.randint(1,10))

The output will be a random number between 1 and 10.


3. User-defined Functions

Functions created by the programmer according to the requirement are called user-defined functions.

Syntax


def function_name():
    statements

Example


def welcome():
    print("Welcome to CodeStep Academy")

welcome()

Output


Welcome to CodeStep Academy

Creating a User-defined Function

A function is created using the def keyword followed by the function name, parentheses, and a colon.

Syntax


def function_name():
    statements

Example


def display():
    print("Python Functions")

display()

Parameters and Arguments

Although these terms are often used interchangeably, they have different meanings.

Parameter Argument
Variable written in function definition. Actual value passed during function call.
Receives data. Sends data.

Example


def greet(name):      # name is parameter
    print("Hello", name)

greet("Sunil")        # Sunil is argument

Output


Hello Sunil

Positional Parameters

Arguments are matched with parameters according to their position.

Example


def student(name, age):
    print(name)
    print(age)

student("Riya",17)

Output


Riya
17

If the order is changed, the output also changes.


student(17,"Riya")

Output


17
Riya

Default Parameters

A default parameter has a predefined value. If the user does not pass an argument, the default value is used.

Syntax


def function(parameter=value):

Example


def country(name="India"):
    print(name)

country()

country("Japan")

Output


India
Japan

Functions Returning Values

A function can return one or more values using the return statement.

Returning One Value


def square(n):
    return n*n

result = square(6)

print(result)

Output


36

Returning Multiple Values


def calculate(a,b):
    sum = a+b
    product = a*b
    return sum, product

x,y = calculate(5,4)

print(x)
print(y)

Output


9
20

Flow of Execution

Flow of execution refers to the order in which statements are executed in a program.

  1. The program starts from the first statement.
  2. When a function is called, control moves to that function.
  3. The statements inside the function are executed.
  4. When the function finishes or encounters a return statement, control returns to the calling statement.

Example


def message():
    print("Inside Function")

print("Start")

message()

print("End")

Output


Start
Inside Function
End

Scope of Variables

The scope of a variable determines where it can be accessed.

1. Local Scope

Variables created inside a function are called local variables.

Example


def demo():
    x = 10
    print(x)

demo()

The variable x exists only inside the function.


2. Global Scope

Variables created outside all functions are global variables.

Example


x = 100

def demo():
    print(x)

demo()

print(x)

Output


100
100

Using Global Keyword

To modify a global variable inside a function, use the global keyword.

Example


count = 0

def increment():
    global count
    count = count + 1

increment()

print(count)

Output


1

Difference Between Local and Global Variables

Local Variable Global Variable
Declared inside a function. Declared outside all functions.
Accessible only inside that function. Accessible throughout the program.
Destroyed after function execution. Exists until the program ends.

Common Programming Errors

  • Calling a function before defining it.
  • Passing incorrect number of arguments.
  • Forgetting the return statement.
  • Confusing parameters with arguments.
  • Trying to access a local variable outside the function.
  • Modifying a global variable without using the global keyword.

Exam Tips

  • Know the difference between parameters and arguments.
  • Practice programs with return statements.
  • Remember the difference between local and global variables.
  • Understand the flow of execution for tracing programs.
  • Practice writing user-defined functions with default and positional parameters.

Frequently Asked Questions (FAQs)

1. Why do we use functions?

Functions reduce code repetition, improve readability, and make programs modular.

2. Which keyword is used to create a function?

The def keyword is used to create a function.

3. What is the difference between parameters and arguments?

Parameters are variables in the function definition, while arguments are actual values passed during the function call.

4. Can a function return multiple values?

Yes. Python allows a function to return multiple values separated by commas.

5. What is the difference between local and global variables?

Local variables exist only inside a function, whereas global variables are accessible throughout the program.


Summary

  • A function is a reusable block of code.
  • Python provides built-in, module, and user-defined functions.
  • User-defined functions are created using the def keyword.
  • Parameters receive data, while arguments send data.
  • Default parameters use predefined values if no argument is supplied.
  • Functions can return one or multiple values.
  • The flow of execution moves from the main program to the function and back.
  • Variables can have local or global scope depending on where they are declared.