Functions Returning Values and Flow of Execution (Class 12 Computer Science)
Updated 16 October 2026 · Fact-checked
A return statement ends a function and sends a value back to the line that called it. A function with no return (or a bare return) gives back None. Several values written after return come back as one tuple. Flow of execution means Python runs a function's body only when it is called, then resumes at the calling line.
Follow arrow 1 into the function and arrow 2 back with the value; anything written after return never runs, and no return means None.
Illustration drawn for QuestionBuddy.
Understand Returning Values and Flow of Execution
A user-defined function is a named block of code created with def. Calling it runs its body. There are two ways a function can produce a result, and the board tests the difference often:
print()inside the function displays something on the screen, but the caller receives nothing useful.return valuesends the value back to the caller, which can store it, print it or use it in an expression. The function stops at once: statements after areturnin the same block never run.
If a function ends without reaching a return, or has return with nothing after it, it returns None. So x = greet() for a function that only prints makes x None, and print(greet()) prints the greeting and then None. A void function is one that does not return a value.
Returning several values. return a, b, c packs the values into a tuple. The caller can keep the tuple (t = f()) or unpack it into separate names (p, q, r = f()); the number of names must match, or Python raises ValueError. NCERT Program 7-12 returns the area and perimeter of a rectangle this way.
Flow of execution is the order in which statements actually run (NCERT 7.3.4). Python starts at the top. When it meets a def, it only creates the function; the body is not executed. When it meets a call, control jumps to the function, runs its body, and on return (or at the end of the body) comes back to the calling line, which then finishes. A function must be defined before the line that calls it runs, otherwise Python raises NameError: name '...' is not defined. In a call such as g(f(2)), the inner call runs first because its value is the argument of the outer one; inside an expression like f(x) + f(x + 1), calls run left to right.
In exam answers on flow of execution, the usual convention is to number the lines and list them in the order they run: each def line once when it is read, then the main statements, and for each call the call line, the function's lines, and the call line again when control returns.
Output questions in every recent sample paper rely on these rules: spotting a missing return, counting how many times a function prints, or following a value as it is returned through two functions.
Returning Values and Flow of Execution: key facts
- return versus print
return v hands v back to the caller and ends the function print(v) only shows v on the screen no return the function gives back None return alone ends the function and gives back None - print(f()) for a function that only prints shows its output and then None.
- Several values
return a, b returns the tuple (a, b) t = f() t is the whole tuple x, y = f() unpacks it; the count must match - Unpacking 2 values into 3 names raises ValueError.
- Flow of execution
- 1.def line read
- 2.function created, body skipped
- 3.main code runs
- 4.call: jump to the function
- 5.body runs
- 6.return: back to the calling line
- 7.next statement
- Calling before the def has run raises NameError.
- Order of calls
- Nested call g(f(2)): f runs first
- Expression f(1) + f(2): left call first
- Statements after return in the same block: never run
- Track printed output in the order the calls actually run.
How to answer Returning Values and Flow of Execution questions
Use this routine for output questions on functions and for flow-of-execution questions.
- 1Number the lines of the program. Mark every def line and skip its body until a call is reached.
- 2Start at the first statement outside any function. When you reach a call, note the argument values and jump to that function.
- 3Inside the function, write each printed line as it happens. At return, note the value returned and stop the function at once.
- 4If the end of the body is reached without return, the value returned is None.
- 5Go back to the calling line and substitute the returned value (a tuple if several values were returned).
- 6For nested calls, evaluate the innermost call first; for calls inside one expression, go left to right.
- 7For flow-of-execution answers, write the line numbers in the order they run, repeating the call line when control returns to it.
Returning Values and Flow of Execution in real life
The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
A missing return gives None
Suppose a shop's billing program has a function discount(price) that should give 10% off. The programmer wrote print(price * 0.9) inside it instead of return, and then added total = discount(500) + 40 for delivery.
What happens when the bill is calculated, and how should the function be fixed?
Try it first, then show the solutionSolution
- discount(500) prints 450.0 but returns None, so None + 40 raises TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'.
- Fix it with return:
def discount(price): return price * 0.9 total = discount(500) + 40 print(total)
Answer: The faulty version prints 450.0 and then fails with TypeError; the fixed version prints 490.0.
Memory hook: print shows the bill to the customer; return hands it to the cashier.
Returning two values
Imagine an ATM that pays any amount in ₹500 and ₹100 notes, using as many ₹500 notes as possible.
Write notes(amount) that returns the number of ₹500 and ₹100 notes, and show the result for ₹2,800.
Try it first, then show the solutionSolution
- Floor division gives the ₹500 notes; the remainder divided by 100 gives the ₹100 notes.
def notes(amount): n500 = amount // 500 n100 = (amount % 500) // 100 return n500, n100 a, b = notes(2800) print(a, b) print(notes(2800))
Answer: 5 3, and the whole result is the tuple (5, 3).
Memory hook: Two values go out of a function in one tuple envelope.
Tricks to remember Returning Values and Flow of Execution
Short memory aids. Say each one aloud once, then use it on a question the same day.
return is the exit door
The moment Python walks through a return, it leaves the function; nothing after it in that block runs.
Use it when: Questions with print statements after return.
No return, no gift
A function that only prints hands back an empty box: None.
Use it when: Output questions such as print(f()).
Read the recipe, cook on demand
def only reads the recipe; the dish is cooked only when the function is called.
Use it when: Flow-of-execution and NameError questions.
Inside out, left to right
Nested calls run from the inside out; calls side by side run left to right.
Use it when: Order of printed output with nested calls.
Returning Values and Flow of Execution: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: A function that only prints · 1 mark · Very short answerBoard level
Write the output:
def greet(n):
print('Hi', n)
x = greet('Asha')
print(x)Try it first, then show the solutionSolution
- The call prints Hi Asha.
- greet has no return statement, so x is None.
Answer: Hi Asha
None
Example 2: Returning several values · 2 marks · Short answerHarder board level
Write the output:
def stats(L):
return min(L), max(L), sum(L) / len(L)
a, b, c = stats([4, 9, 2])
r = stats([1, 1])
print(a, b, c, r)Try it first, then show the solutionSolution
- stats([4, 9, 2]) returns (2, 9, 5.0), which is unpacked into a, b, c. (1 mark)
- stats([1, 1]) returns the tuple (1, 1, 1.0), stored whole in r; / always gives a float. (1 mark)
Answer: 2 9 5.0 (1, 1, 1.0)
Example 3: Flow of execution by line numbers · 2 marks · Short answerHarder board level
Write the order in which the lines run:
def square(x): # 1
return x * x # 2
def show(a): # 3
b = square(a) + 1 # 4
print(b) # 5
n = 3 # 6
show(n) # 7
print('Done') # 8Try it first, then show the solutionSolution
- Lines 1 and 3 define the functions (bodies skipped); line 6 runs; line 7 calls show. (½ mark)
- Control goes to show (3) and line 4, which calls square: 1 → 2, then back to 4. (1 mark)
- Line 5 prints 10; control returns to 7, then line 8 prints Done. (½ mark)
Answer: 1 → 3 → 6 → 7 → 3 → 4 → 1 → 2 → 4 → 5 → 7 → 8; output 10 and Done
Example 4: Counting upper- and lower-case letters · 3 marks · Short answerHarder board level
Write a function count_case(s) that returns the number of upper-case and lower-case letters in s. Call it for 'Board Exam 2027' and print both counts with labels.
Try it first, then show the solutionSolution
- Loop over the characters and test with isupper() and islower() (1½ marks); return both counts (1 mark); unpack at the call (½ mark).
def count_case(s): up = low = 0 for ch in s: if ch.isupper(): up += 1 elif ch.islower(): low += 1 return up, low u, l = count_case('Board Exam 2027') print('Upper:', u, 'Lower:', l)- B and E are upper case; o, a, r, d, x, a, m are lower case.
Answer: Upper: 2 Lower: 7
Example 5: return inside a loop · 2 marks · Short answerHOTS / competency
Predict the output:
def first_neg(L):
for x in L:
if x < 0:
return x
print('checked', x)
return 'none'
print(first_neg([4, -2, -7]))
print(first_neg([1]))Try it first, then show the solutionSolution
- First call: 4 is checked and printed; -2 is negative, so the function returns -2 at once; -7 is never looked at. (1 mark)
- Second call: 1 is checked; the loop ends without a negative, so the last line returns 'none'. (1 mark)
Answer: checked 4
-2
checked 1
none
Example 6: Order of nested calls · 2 marks · Short answerHOTS / competency
Predict the output:
def f(x):
print('f', x, end=' ')
return x * 2
def g(x):
return f(x) + f(x + 1)
print(g(f(2)))Try it first, then show the solutionSolution
- The argument f(2) runs first: prints 'f 2' and returns 4. (½ mark)
- g(4) evaluates f(4) then f(5): prints 'f 4' and 'f 5', returning 8 and 10. (1 mark)
- g returns 18, which the outer print shows on the same line, because the earlier prints used end=' '. (½ mark)
Answer: f 2 f 4 f 5 18
Where students lose marks in Returning Values and Flow of Execution
The slips examiners see most often, and what to write instead.
Mistake: Using print instead of return in a function whose result is needed
The caller then receives None and later arithmetic fails or prints None.
Wrong working
def area(r):
print(3.14 * r * r)
A = area(2) + 1 # TypeErrorRight working
def area(r):
return 3.14 * r * r
A = area(2) + 1Do this: If the question says 'returns', write return; if it says 'displays', print.
Usually costs: 1 mark in a 2-3 mark function.
Mistake: Writing output for statements after return
return ends the function immediately.
Wrong working
big
never
smallRight working
big small
Do this: Cross out every statement below a return in the same block before tracing.
Usually costs: 1 mark.
Mistake: Forgetting the None printed by print(f())
When f prints and returns nothing, print(f()) shows f's output and then None.
Wrong working
Hi Asha
Right working
Hi Asha
NoneDo this: Always ask what the function returns before writing what print shows.
Usually costs: Half to 1 mark.
Mistake: Listing flow of execution top to bottom
Function bodies run only when called, and control returns to the calling line.
Wrong working
1 → 2 → 3 → 4 → 5 → 6 → 7 → 8
Right working
1 → 3 → 6 → 7 → 3 → 4 → 1 → 2 → 4 → 5 → 7 → 8
Do this: Skip function bodies at definition; jump into them at each call; come back to the call line.
Usually costs: The full 2 marks.
Exam tips
- Read the question verb: 'returns' needs return; 'displays' or 'prints' needs print. Using the wrong one loses the mark even if the logic is right.
- When a function returns two or more values, show the call with unpacking (a, b = f(...)) so the examiner sees you know it is a tuple.
- In output questions with end=' ', keep later output on the same line.
- Define functions before the main code in programs you write; a call that runs before its def raises NameError.
- For flow of execution, writing the numbered order with arrows and the final output is the expected format.
Practice questions on Returning Values and Flow of Execution
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQBoard level
A Python function that has no return statement returns:
(a) 0 (b) an empty string (c) None (d) an errorHint
An empty box.
Answer
(c) None
Q2 · 1 mark · Very short answerBoard level
Write the output:
def add(a, b): return a + b print(add('3', '4'), add(3, 4))Hint
+ on strings joins them.
Answer
34 7
Q3 · 1 mark · MCQBoard level
What happens when this program runs?
print(area(2)) def area(r): return 3.14 * r * r(a) 12.56 (b) None (c) NameError (d) SyntaxError
Hint
Has the def run when the call is reached?
Answer
(c) NameError: name 'area' is not defined, because the call runs before the function is created.
Q4 · 1 mark · Very short answerHarder board level
Predict the output:
def test(n): if n > 5: return 'big' print('never') return 'small' print(test(9), test(2))Hint
Is anything run after a return?
Answer
big small (the print after return never runs).
Q5 · 3 marks · Short answerHarder board level
An electricity board charges ₹5 per unit for the first 100 units and ₹7 per unit after that. Write a function bill(units) that returns the amount, and print the bills for 80 and 150 units.
Hint
Two cases: up to 100, and above 100.
Answer
def bill(units): if units <= 100: return units * 5 return 100 * 5 + (units - 100) * 7 print(bill(80), bill(150))Output: 400 850
Q6 · 1 mark · Assertion-reasonHarder board level
Assertion (A): A Python function can return more than one value.
Reason (R): Values written after return separated by commas are returned together as a tuple.
(a) Both A and R are true and R is the correct explanation of A. (b) Both A and R are true but R is not the correct explanation of A. (c) A is true but R is false. (d) A is false but R is true.Hint
What is the type of the returned object?
Answer
(a) Both are true and R explains A.
Q7 · 2 marks · Short answerHOTS / competency
Number the lines 1 to 7 and write the flow of execution and the output:
def big(a, b): # 1 if a > b: # 2 return a # 3 return b # 4 x = big(3, 8) # 5 y = big(x, 5) # 6 print(x, y) # 7Hint
In the first call the if is false; in the second it is true.
Answer
1 → 5 → 1 → 2 → 4 → 5 → 6 → 1 → 2 → 3 → 6 → 7. Output: 8 8
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1.What does a function return if it has no return statement?Show answer
None.
2.What is the type of the value returned by return 3, 4?Show answer
A tuple, (3, 4).
3.What error comes from calling a function before its def has run?Show answer
NameError: name '...' is not defined.
4.Does the body of a function run when Python reads the def line?Show answer
No. def only creates the function; the body runs when the function is called.
5.In g(f(2)), which function runs first?Show answer
f, because its result is the argument of g.
6.What happens to statements after return in the same block?Show answer
They are never executed.
Returning Values and Flow of Execution: frequently asked questions
What is the difference between return and print in a Python function?
print shows a value on the screen and the function still returns None. return sends a value back to the caller, which can store or use it, and also ends the function.
Can a Python function return more than one value?
Yes. return a, b sends back the tuple (a, b). The caller can unpack it, as in x, y = f(), or keep the whole tuple in one variable.
What is flow of execution in Python?
It is the order in which statements actually run. Python goes top to bottom, but a function's body runs only when the function is called; control then jumps into the function and returns to the calling line when the function ends.
Which NCERT sections cover this topic?
Class XI NCERT Computer Science Chapter 7 (Functions): 7.3.3 Functions Returning Value and 7.3.4 Flow of Execution, including Program 7-12 on returning multiple values. The CBSE Class 12 syllabus lists 'function returning value(s)' and 'flow of execution'.
What is a void function?
A function that does not return a value. In Python it still returns None automatically when it ends.