Home › Free notes › Class 11 Computer Science › Functions
Functions – Class 11 Computer Science notes, important questions and MCQs
NCERT Class 11 Computer Science, chapter 7. Notes, key terms, important questions with answers and MCQs.
Chapter overview
A function is a named block of code that performs a specific task and runs only when it is called. Functions make programs modular, reduce repetition and make code easier to read, test and reuse. Python has built-in functions, functions in modules such as math, random and statistics, and user-defined functions created with def. Concepts include parameters and arguments, return values, default arguments, and the scope of variables.
User-defined functions
- A function is defined with def name(parameters): followed by an indented body; it is called by name with arguments; return sends a value back to the caller, and a function without return returns None.
- Parameters are variables in the function definition; arguments are values passed in the call; default parameters, such as def greet(name, msg='Hello'), are used when an argument is not passed, and must come after non-default parameters.
- The flow of execution jumps to the function when it is called and returns to the calling point afterwards; a function can return multiple values as a tuple.
Scope and modules
- A local variable is defined inside a function and accessible only there; a global variable is defined outside all functions; the global keyword allows a function to modify a global variable.
- Built-in functions include print(), input(), len(), type(), int(), abs(), max(), min(), round() and sum().
- Modules are imported with import math or from math import sqrt; math provides sqrt(), ceil(), floor(), pow() and pi; random provides random(), randint(a, b) and randrange(); statistics provides mean(), median() and mode().
Key terms
- Function
- a named, reusable block of code that performs a task
- Parameter
- a variable listed in a function definition
- Argument
- a value passed to a function when it is called
- Local variable
- a variable accessible only inside the function where it is defined
- Module
- a file containing Python definitions that can be imported
Important questions with answers
Distinguish between parameters and arguments.2 marks
Parameters are the variables listed in the function header when the function is defined, such as a and b in def add(a, b). Arguments are the actual values passed when the function is called, such as 3 and 4 in add(3, 4).
Explain local and global variables with an example.3 marks
A local variable is created inside a function and can be used only there; for example, in def f(): y = 2, y is local. A global variable is created outside all functions and can be read anywhere; to modify it inside a function, it must be declared with the global keyword, for example def g(): global count; count += 1.
What are the advantages of functions? Explain user-defined functions with an example.5 marks
Functions offer several advantages. They divide a large program into smaller, manageable modules, which makes the program easier to understand and develop. They allow code to be reused, avoiding repetition, since the same function can be called many times with different inputs. They make testing and debugging easier because each function can be checked separately. They improve readability and allow different programmers to work on different functions. A user-defined function is created with the def keyword, followed by the function name, parameters in parentheses and a colon, and an indented body. For example, def area_circle(r): return 3.14 * r * r defines a function that returns the area of a circle. It can be called as a = area_circle(5), which assigns 78.5 to a. Functions can have default parameters, such as def interest(p, r=5, t=1): return p * r * t / 100, so interest(1000) gives 50.0 while interest(1000, 6, 2) gives 120.0. A function may return several values as a tuple, as in def min_max(a, b): return min(a, b), max(a, b). Variables created inside a function are local, while those outside are global, and this scope rule prevents accidental changes to data.
Quick revision
- Need for functions; def, call, return.
- Parameters, arguments, default values.
- Local and global scope.
- Built-in functions and modules: math, random, statistics.
Exam tips
- Remember: random.randint(1, 6) includes both 1 and 6.
- Default parameters must be placed after non-default ones.
Practice MCQs
A user-defined function in Python is created with the keyword
- function
- define
- def
- fun
Show answer
Answer: (c) def. Example: def area(r).
A function without a return statement returns
- 0
- an empty string
- False
- None
Show answer
Answer: (d) None. None means no value.
math.sqrt(49) returns
- 7
- 7.0
- 49
- 2401
Show answer
Answer: (b) 7.0. sqrt returns a float.
The value of math.ceil(4.2) is
- 5
- 4
- 4.0
- 4.2
Show answer
Answer: (a) 5. ceil rounds up.
random.randint(1, 10) can return
- only 1 to 9
- only 10
- any float between 1 and 10
- any integer from 1 to 10 inclusive
Show answer
Answer: (d) any integer from 1 to 10 inclusive. Both ends are included.
A variable defined inside a function is
- global
- constant
- built-in
- local
Show answer
Answer: (d) local. It exists only within the function.
Which function header is valid
- def f(a=2, b)
- def f(a, b=2)
- def f(a=2, b, c=3)
- def f(=2, b)
Show answer
Answer: (b) def f(a, b=2). Default parameters must follow non-default ones.
What is printed by: x = 5; def f(): x = 10; then f(); print(x)
- 10
- None
- 5
- an error
Show answer
Answer: (c) 5. The assignment inside f creates a local x.
All chapters of Class 11 Computer Science
Written by Kundali IQ from standard, well-established facts to help with revision. This is not an official NCERT publication; always read your textbook too.