Curriculum
Course: Grade XI Computer Science
Login
Text lesson

Le-7 Functions

School Revise · Class 11 Computer Science · Chapter 7

Functions

Code 083, Class 11. A function is a named block of code we can reuse. Here we define our own functions, pass values to them, return results and understand scope.

def

defines a function

2

built-in and user made

What this chapter is about

A function is a named block of code that does one job. We call it by its name whenever we need it, which saves repeating code and keeps programs tidy.

1. Defining and calling

We define a function with def, give it parameters in brackets, and use return to send a result back. We call it by writing its name with values (arguments).

def add(a, b): return a + b print(add(5, 3)) Output: 8

2. Default and positional arguments

Positional arguments are matched by order. A default argument has a value ready, so it may be left out when calling.

3. Scope, built-in and user-defined

A variable made inside a function is local and cannot be seen outside. A variable outside is global. Python has built-in functions like print() and len(), and the ones we write are user-defined.

Practise with the interactive

Build a small function, set its arguments, and see the value it returns. The interactive opens right here in the lesson.

Try it in the code lab

Write and run real Python right here in the lesson, then work through the practice problems with answers.

Starting Python...

Coding practice problems, with answers

Type each one into the code lab above, then open the card to see the worked solution and its output.

Problem 1. Write a function that adds two numbers.

SOLUTION

def add(a, b): return a + b print(add(5, 3))

OUTPUT

8
Problem 2. Write a function for the area of a rectangle.

SOLUTION

def area(l, b): return l * b print(area(8, 5))

OUTPUT

40
Problem 3. Write a function that returns the larger of two numbers.

SOLUTION

def bigger(a, b): if a > b: return a return b print(bigger(5, 9))

OUTPUT

9
Problem 4. Write a function that checks whether a number is even.

SOLUTION

def is_even(n): return n % 2 == 0 print(is_even(8))

OUTPUT

True
Problem 5. Write a greet function with a default name.

SOLUTION

def greet(name=”student”): print(“Hello,”, name) greet() greet(“Ravi”)

OUTPUT

Hello, student Hello, Ravi
Problem 6. Write a function to find a factorial.

SOLUTION

def fact(n): f = 1 for i in range(1, n + 1): f = f * i return f print(fact(5))

OUTPUT

120
Problem 7. Write a function that returns the square of a number.

SOLUTION

def square(x): return x * x print(square(6))

OUTPUT

36
Problem 8. Write a function that prints a times table.

SOLUTION

def table(n): for i in range(1, 4): print(n, “x”, i, “=”, n * i) table(2)

OUTPUT

2 x 1 = 2 2 x 2 = 4 2 x 3 = 6
Problem 9. Show local and global scope.

SOLUTION

x = 10 def show(): x = 5 print(“inside”, x) show() print(“outside”, x)

OUTPUT

inside 5 outside 10
Problem 10. Write a function that returns the sum of a list.

SOLUTION

def total(nums): return sum(nums) print(total([2, 4, 6]))

OUTPUT

12

Practice set A, multiple choice

1. Which keyword defines a function?

def.

2. Which keyword sends a value back from a function?

return.

3. A variable made inside a function is …

local, and cannot be seen outside.

4. print() and len() are examples of …

built-in functions.

Quick summary

A function is a named, reusable block of code. We define it with def, pass values as arguments (matched by position or given as defaults), and use return to send a result back. Variables made inside are local; those outside are global. print() and len() are built-in functions, and the ones we write are user-defined.

Open the Virtual Lab

These free Class 11 Computer Science notes explain functions, defining and calling, parameters and arguments, return values, default arguments, local and global scope with worked Python programs and practice, for CBSE students across India and the Gulf including the UAE, Saudi Arabia, Qatar, Oman, Kuwait and Bahrain.

© 2026 School Revise. All rights reserved. Original content aligned to the CBSE and NCERT Class 11 Computer Science syllabus. Unauthorised copying is not permitted.

Layer 1
Login Categories