Concepts / Understanding Function Arguments

Understanding Function Arguments

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.

  • Programming

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.

StringSpamargbound valuePrintfirst operationInteger17Printsecond operationUnsupported valueoperation failsFloat3.141592653589793binds tobinds tobinds toused bythencannot complete operation
Which data types can pass through the function's printing operations, and where would an unsupported value fail?
Key point

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.

Function callprint_twice(value)arg = Spamstring callPrint argfirst timeCall completetwo displayed valuesarg = 17integer callPrint argsecond timearg =3.141592653589793float callwith Spamwith 17with 3.141592653589793execute bodyexecute bodyexecute bodythenthen
What happens step by step when the same function receives a string, an integer, or a floating-point number?
Worked example

Three argument traces

Determine what print_twice displays for the calls with Spam, 17, and 3.141592653589793.

  1. 1
    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.

  2. 2
    Integer call

    When print_twice receives 17, the integer 17 is bound to arg. The same two print operations display 17 twice.

  3. 3
    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.

Result

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.

ArgumentValue bound to argFirst outputSecond output
StringSpamSpamSpam
Integer171717
Floating-point number3.1415926535897933.1415926535897933.141592653589793

The operation sequence stays the same while the value displayed changes with the argument.

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.

Printable valueoperation succeedsprint_twiceprints arg twiceTwo outputscall completesUnsupported valueoperation failsFunction failurecall does not completepassed as argprinting succeedspassed as argprinting fails
What is the difference between an argument that supports the function's operation and one that causes the operation to fail?
Best practice

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

  • Common mistake

    Assuming a function accepts only one data type because its parameter has one name.

    What it looks like

    Treating arg as if it must always hold a string.

    Why it fails

    The function's operations, rather than the parameter name, determine which values are compatible.

    Do this instead

    Examine what the function does with arg. In print_twice, printable strings, integers, and floats all work.

  • Common mistake

    Expecting the function's behavior to change when the argument's type changes.

    What it looks like

    Expecting print_twice to perform a different sequence for 17 than for Spam.

    Why it fails

    The function still binds the value to arg and performs the same two print operations.

    Do this instead

    Keep the function's operation sequence fixed and change only the value bound to the parameter.

  • Common mistake

    Checking only the first operation in a function.

    What it looks like

    Concluding that an argument is compatible after one successful operation without checking the rest of the function body.

    Why it fails

    A later operation could fail even if an earlier operation succeeds.

    Do this instead

    Trace every operation performed on the argument.

Testing Your Trace

EASY

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.
MEDIUM

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

  1. 1
    A function can accept different data types when its operations are compatible with those types.
  2. 2
    print_twice binds its argument to arg, prints that value, and prints it again.
  3. 3
    Strings, integers, and floating-point numbers produce different displayed values but follow the same function behavior.
  4. 4
    To assess compatibility, inspect and test every operation performed on the argument.
  5. 5
    An unsupported value can cause the function to fail when one of its operations cannot handle that value.

At a glance

  • 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.

Related concepts

More concepts starting with U