Understanding Function Arguments
Functions can accept different data types as arguments if the function's operations are compatible with those types.
One Function, Several Value Types
A function does not always need to be restricted to one data type. Its compatibility with an argument depends on what the function does with that argument. If the function only prints its argument, then a string, an integer, and a floating-point number can all work because each can be printed.
What do you think happens?
What do you expect when print_twice is called first with the string Spam, then with the integer 17, and then with the floating-point number 3.141592653589793?
Reveal answer
Answer: Each call prints its argument twice. The first call prints Spam two times, the second prints 17 two times, and the third prints 3.141592653589793 two times.
The function performs the same printing operation in every call. Only the value bound to its parameter changes.
Following an Argument Through Operations
When a function is called, the supplied value is bound to the function's parameter. For print_twice, the parameter is arg. The function then prints arg once and prints arg again. It does not first ask whether arg is a string or a number. It simply applies its operation to the received value.
The function's behavior is determined by its operations, not by the data type of the argument. To decide whether a type is compatible, inspect what the function does with the value and check whether that operation succeeds for the value.
Tracing Three Calls
The three calls follow the same execution pattern. First, the supplied value is bound to arg. Next, the function prints arg. Finally, it prints arg again. The data type changes from call to call, but the function body and its sequence of operations remain the same.
Three argument traces
Determine what print_twice displays for the calls with Spam, 17, and 3.141592653589793.
String call: When print_twice receives Spam, the string Spam is bound to arg. The function prints arg, so Spam is displayed, and then prints arg again, so Spam is displayed a second time.
Integer call: When print_twice receives 17, the integer 17 is bound to arg. The same two print operations display 17 twice.
Floating-point call: When print_twice receives 3.141592653589793, that floating-point value is bound to arg. The same two print operations display the value twice.
All three calls complete the same sequence: bind the argument to arg, print it, and print it again. Their displayed values differ because their arguments differ.
| Argument | Value bound to arg | First output | Second output |
|---|---|---|---|
| String | Spam | Spam | Spam |
| Integer | 17 | 17 | 17 |
| Floating-point number | 3.141592653589793 | 3.141592653589793 | 3.141592653589793 |
Compatibility Comes From the Function Body
A function's parameter does not by itself lock the function to one data type. Instead, compatibility is determined by the operations in the function body. For print_twice, the relevant operation is printing. Strings, integers, floats, booleans, and many other types can be printed, so they can be passed to this function. A value that the print operation cannot handle would cause the function to fail.
To verify that a function accepts a particular type, test the function with a value of that type and check whether every operation in the function succeeds. Do not decide compatibility from the parameter name alone.
Mistakes in Argument Tracing
Assuming a function accepts only one data type because its parameter has one name.
The function's operations, rather than the parameter name, determine which values are compatible.
Fix:
Examine what the function does with arg. In print_twice, printable strings, integers, and floats all work.Expecting the function's behavior to change when the argument's type changes.
The function still binds the value to arg and performs the same two print operations.
Fix:
Keep the function's operation sequence fixed and change only the value bound to the parameter.Checking only the first operation in a function.
A later operation could fail even if an earlier operation succeeds.
Fix:
Trace every operation performed on the argument.
Testing Your Trace
Suppose print_twice is called with a boolean value. Predict whether the call can complete, identify what becomes bound to arg, and describe how many times the value is printed. Then explain why your prediction follows from the function's operation.
Hints
- The source identifies booleans as values that can be printed.
- Trace the same sequence used for the string, integer, and floating-point calls.
- The function prints its argument twice.
For a new function, list each operation it performs on its argument. Use that list to decide which data types should be tested and whether each tested type completes every operation.
Hints
- Compatibility depends on the operations in the function body.
- Do not stop tracing after the first operation.
- A type is compatible only when the required operations succeed.
Key Takeaways
- A function can accept different data types when its operations are compatible with those types.
- print_twice binds its argument to arg, prints that value, and prints it again.
- Strings, integers, and floating-point numbers produce different displayed values but follow the same function behavior.
- To assess compatibility, inspect and test every operation performed on the argument.
- An unsupported value can cause the function to fail when one of its operations cannot handle that value.
Key Takeaways
- Function compatibility comes from the operations performed on an argument, not from the argument's type alone.
- print_twice works with strings, integers, and floats because the print operation can handle those values.
- Every call follows the same trace: bind the argument to arg, print it, and print it again.
- To verify a type, test it and check that all operations in the function succeed.