[Type B] Working with Functions – Chapter 3 Sumita Arora

|
Table of Contents

14. Draw flow of execution for above program.

Python
def sum(a, b, c, d):
    result = 0
    result = result + a + b + c + d
    return result

def length():
    return 4

def mean(a, b, c, d):
    return float(sum(a, b, c, d)) / length()

print(sum(a, b, c, d), length(), mean(a, b, c, d))

Solution: 1 → 6 → 9 → 12 → 1 → 2 → 3 → 4 → 12 → 6 → 7 → 12 → 9 → 10 → 1 → 2 → 3 → 4 → 10 → 6 → 7 → 10 → 12

Flow of execution explained:

  • Execution starts from line 1.
  • Function sum is defined at line 1.
  • Execution continues to line 6, where the function length is defined.
  • Function mean is defined at line 9.
  • The print statement at line 12 is executed:
    • sum(a, b, c, d) calls the sum function, passing a, b, c, d as arguments.
    • length() calls the length function, which returns 4.
    • mean(a, b, c, d) calls the mean function, passing a, b, c, d as arguments.

This flow of execution demonstrates the sequence of events in the program from start to finish.


For some context to the answer, take a look back on these two lines from your textbook’s page 103 or similar for newer editions. It’s under 3.4 – Flow of Execution in a Function Call.

Just so, you don’t end up with answers such as

  • 12 → 1 → 2 → 3 → 4 → 1 → 2 → 6 → 7 → 12 → 9 [OR]
  • 12 → 1 → 2 → 3 → 4 → 12 → 6 → 7 → 12 → 9 → 10
  • Execution always begins at the first statement of the program.
  • If Python notices that it is a function definition, (def statements) then Python just executes the function header line to determine that it is proper function header and skips/ignores all lines in the function body.
Sumita Arora Textbook Class 12 Computer Science
"Spread the light within, illuminate the hearts around you. Sharing is not just an action, but a sacred journey of connecting souls."~ Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *