Functions in Python
We will now describe Python’s functions. We’ve already used functions that are part of the language, such as len()
. In this section, we’ll do the following:
- Explain why functions are useful
- Provide an example of a function to implement an ALU
- Explain how variables can be private to a function or shared between functions (scope)
- Describe how parameters are passed to functions
- Describe how a function returns a result
Writing the Python code to deal with each arithmetic or logical operation implemented by a simulator would be tedious because so much code would be replicated by individual instructions. Instead, we can create a Python function (that is, a subroutine or procedure) that carries out both the arithmetic/logic operation and the appropriate flag-bit setting.
Consider a Python function called alu(f,p,q)
that returns an integer that is a function of the f
, p
, and q
parameters. The operation to be performed...