[Type B] Working with Functions – Chapter 3 Sumita Arora
Table of Contents
17. Write a program with the non-void version of the above function and then write the flow of execution for both the programs.
Solution:
Python
def next_num(num):
next_num = num + 1
return "Next number after " + str(num) + " is " + str(next_num)
# Call the function with the provided arguments and print the results
print(next_num(4))
print(next_num(6))
print(next_num(8))
print(next_num(2 + 1))
print(next_num(4 - 3 * 2))
print(next_num(3 - 2))
Flow of Execution for:
- Void Version: 1 → 6 → 1 → 2 → 3 → 7 → 1 → 2 → 3 → 8 → 1 → 2 → 3 → 9 → 1 → 2 → 3 → 10 → 1 → 2 → 3 → 11 → 1 → 2 → 3
(Control doesn’t return to function call statement as nothing is being returned by the function for void functions, they only print) - Non-void Version: 1 → 6 → 1 → 2 → 3 → 6 → 7 → 1 → 2 → 3 → 7 → 8 → 1 → 2 → 3 → 8 → 9 → 1 → 2 → 3 → 9 → 10 → 1 → 2 → 3 → 10 → 11 → 1 → 2 → 3 → 11