Arguments

New:

  • argument

In the previous lesson we’ve written the function call pow(3, 2) and said we’ll learn about general properties and features of functions, so let’s start with those values in parenthesis. We can say those two are arguments. Argument is a value specified in the parentheses in a function call.

The main goal of the pow function is to raise the first number to the power of the second number. This operation of exponentiation hidden in the function definition is equivalent to the line of code 3 ** 2. The exponentiation i.e. raising to power in Python is represented with symbol ** (it is an arithmetic operator). If you could see the results of pow(3, 2) and then of 3 ** 2, you'd see the same result 9.

Let's look at several more calls of the same function:

pow(5, 4)
pow(2, 6)
pow(12, 4)

When the program runs (in REPL.it the program runs when is pressed), arguments are utilized by function's definition. Above, it happens three times for three distinct calls which calculate 54, 26 and 124, respectively. Writing values within parentheses in a function call is referred to as passing the arguments. A comma is the only symbol that can separate multiple arguments.

What happens if you deliberately do something pow wasn't designed to do? For example, what if you pass one argument instead of two? Even though one can find out the answer in the official documentation, programmers often prefer to experiment. It’s usually quicker (and more fun) to just try things:

You'll see an error:

TypeError: pow() missing required argument 'exp' (pos 2)

Python error messages usually start with the category: SyntaxError, TypeError, NameError, etc1. Then follows the main part: description. pow() missing required argument 'exp' (pos 2) indicates that our call is missing an argument at position 2. The call must contain the second argument which is named exp (short for "exponent"). That name and the amount of arguments are set in the function's definition. The author of pow decided to allow it to work with two arguments2, which makes perfect sense for the purpose of this function. This time the interpreter provided a pretty good error description. But sometimes, descriptions can be cryptic and less obvious, especially when your program is large and complex.

The author of pow had defined it to accept numbers only, and moreover at least two of them. Other functions have other rules and limitations. When you learn to create your own functions, you will decide the amount of values to be passed as arguments.


Summary:

  1. Values passed to the function in the call are arguments.
  2. A particular function has limitations on the maximum and minimum amount of arguments and the type of each argument.
  3. Passing the wrong amount of arguments raises an error. Passing the wrong type(s) of value(s) as arguments raises errors.
1

Categories of errors are broad and exist partly for internal purposes. The indicated category may not be directly connected to the issue. For example, TypeError was raised, but the issue was with the amount of arguments.

2

Actually, pow can accept 3 arguments. The 3rd argument is not required, if it's passed then pow performs an additional mathematical operation which affects the result. The ability to accept different amounts of arguments is explained in the second course. You'll be able to write such functions yourself.