Why Functions Exist, and How to Write One
Quick answer User-defined functions are not in the CBSE Class 11 exam syllabus for 2025-26 — they are a Class 12 topic — but the def statement, the function body and the difference between defining and calling are worth learning early because they remove copy-paste from your programs.
Read this before you spend time here. Chapter 7 of your NCERT Class 11 book teaches user-defined functions. User-defined functions are not part of the CBSE Class 11 Computer Science (083) exam syllabus for 2025-26. They moved to Class 12, where Unit 1 opens with "Revision of Python topics covered in Class XI" and then teaches Functions. So nothing on this page can be asked in your Class 11 annual examination or school unit tests. (Class 11 has no board paper at all — the board exam comes at the end of Class 12.)
Two honest conclusions follow. First, if Unit 2 (Computational Thinking and Programming-1) is not yet solid — lists, tuples, dictionaries, strings, loops, the built-in methods — go and revise that first, because that is where your marks are. Second, once Unit 2 is solid, this chapter is a genuine head start: every single thing on this page is examinable next year, and students who arrive in Class 12 already comfortable with def find the whole year easier.
You have been using functions since day one. Every time you wrote len(marks) or max(marks) you called a function. Someone wrote that code once, and you reuse it by name.
marks = [78, 91, 65, 88]
print(len(marks))
print(max(marks))
print(sum(marks))
print(round(sum(marks) / len(marks), 2))Output:
4
91
322
80.5A user-defined function is the same idea, except you write the reusable block yourself.
The problem functions solve. Suppose a shop bills three orders, each with 18% GST:
amount = 250 * 3
print("Payable: Rs", round(amount + amount * 0.18, 2))
amount = 1499 * 2
print("Payable: Rs", round(amount + amount * 0.18, 2))
amount = 80 * 12
print("Payable: Rs", round(amount + amount * 0.18, 2))Output:
Payable: Rs 885.0
Payable: Rs 3537.64
Payable: Rs 1132.8The logic is written three times. If GST changes to 12%, you must edit three places and you will miss one. That is the real cost of copy-paste: not typing effort, but the bugs that appear when one copy is updated and the others are not.
Writing your first function. The header is def name(parameters): — the keyword def, a name that follows the same rules as a variable name, brackets, and a compulsory colon. Everything indented under the header is the function body.
def greet():
print("Namaste, welcome to Class 11 CS")
greet()
greet()Output:
Namaste, welcome to Class 11 CS
Namaste, welcome to Class 11 CSNotice that greet without brackets would not print anything — the brackets are what actually run the function. greet is the function; greet() is the act of running it.
WORKED EXAMPLE — the GST bill as a function. The first line inside the body is a docstring: a string in triple quotes that says what the function does. Python stores it, and you can read it back later.
def bill_total(price, qty):
"""Print the amount payable including 18% GST."""
amount = price * qty
gst = amount * 0.18
print("Item amount : Rs", amount)
print("GST @18% : Rs", round(gst, 2))
print("Payable : Rs", round(amount + gst, 2))
print("--- Order 1 ---")
bill_total(250, 3)
print("--- Order 2 ---")
bill_total(1499, 2)Real output:
--- Order 1 ---
Item amount : Rs 750
GST @18% : Rs 135.0
Payable : Rs 885.0
--- Order 2 ---
Item amount : Rs 2998
GST @18% : Rs 539.64
Payable : Rs 3537.64Now the GST rate lives in exactly one line. Change 0.18 once and every bill in the program is correct.
Flow of execution. This trips up almost everyone. The def block does not run when Python reads it — it only creates the function and moves on. The body runs only when you call it.
print("Line 1: before the def")
def show():
print("Line 3: inside the function")
print("Line 2: after the def, before the call")
show()
print("Line 4: after the call")Output:
Line 1: before the def
Line 2: after the def, before the call
Line 3: inside the function
Line 4: after the callBecause the function is created only when the def line executes, calling it earlier fails. This program:
show()
def show():
print("Hello")ends with (last line of the traceback):
NameError: name 'show' is not definedA function body can never be empty. If you want to write the header now and the logic later, put pass in the body:
def not_written_yet():
pass
print(not_written_yet())
print(type(not_written_yet))Output:
None
Two useful facts fall out of that output. A function that does not send anything back gives you None, and a function is itself a kind of object with a type, just like an int or a list.
- User-defined functions are NOT in the CBSE Class 11 2025-26 exam syllabus — they are Class 12 Unit 1. Revise Unit 2 first; treat this page as a head start, not exam prep.
- The syntax is def name(parameters): followed by an indented body. The colon is compulsory and the body can never be empty — use pass as a placeholder.
- Defining a function does not run it. Python creates the function when the def line executes; the body runs only when you write name().
- A function must be defined before the line that calls it runs, otherwise you get NameError: name '...' is not defined.
- A docstring — a triple-quoted string as the first statement in the body — documents the function and is stored in name.__doc__.
