Clarifying Function Calls; Notes

Resource: Practical Programming – An Introduction to Computer Science Using Python 3, 2nd Edition by Paul Gries, Jennifer Campbell, and Jason Montojo

Chapter 3: Designing and Using Functions
ep44/ 3.1 Functions that Python Provides

An example of a built-in function is abs , which produces the absolute value of a number:

Example 1:
>>> abs(-9)                                               [function call]
9

Example 2:
>>> abs(3.3)                                             [function call]
3.3

General form of a function call

<<function_name>> (<<arguments>>)

argument: an expression that appears between the parentheses of a function call

In Example 1, the argument is -9.
In Example 2, the argument is 3.3.

Example 3: Calculating the difference between a day and night temperature

>>> day_temperature = 3
>>> night_temperature = 10
>>> abs(day_temperature – night_temperature)
7

  • The argument for function call abs(day_temperature – night_temperature) is day_temperature – night_temperature.

 

  • day_temperature refers to 3 and night_temperature refers to 10.

 

  • Python evaluates the expression day_temperature – night_temperature (3 – 10) to -7.

 

  • This value of -7 is passed to function abs , which returns the value 7.

 

Rules for executing a function call 

  1. Evaluate each argument one at a time, working from left to right.

  2. Pass the resulting values into the function.

  3. Execute the function. When the function call finishes, it produces a value.