6.4 Parameters, Parameter Passing, and Scope

We have used functions to implement abstraction. We have broken problems down into smaller, more manageable pieces and have implemented functions that we can call again and again. In the previous section, we pushed this idea one step further by passing functions as parameters to other functions. In this section, we explore in more detail the underlying mechanics of functions and parameter passing.

Consider the function shown in Listing 6.7, which computes the hypotenuse of a right triangle. Using the Pythagorean theorem, a2 + b2 = c2, this function takes in the lengths of the two shorter sides, a and b, and computes and returns the length of the long side, c. In the function definition, the parameters ...

Get Python Programming in Context, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.