8.1 Parameters and Arguments

Some terminology. When we define a function we can specify parameters that will become variables in the function’s code. When we use a function we can provide the data to be assigned to these parameters.

For example, suppose we define a function as follows.

Simple function
1def add (a,b): 
2  return a+b

This says, define a function labelled add that has two parameters a and b. When called this function expects two values to be provided, these will be assigned to variables a and b and the statements in the function (in this case the single return statement on line 2) will be executed.

We call this function in our code as follows.

Calling the add function
1add(1,2)

This will call the add function and assign 1 to a and 2 to b. The return statement is run and the function returns a+b (so, 3 in this example).

It is important to understand that the code inside the function is not run when defining the function. It it only run when the function is called and values are provided to a and b.

Once defined a function can be called as often as we like.

Repeated calls
1add(1,2) 
2add(15,3) 
3add(5.3,6.2)

These return 3, 18, and 11.5 respectively.