Parameters

In the exercise below, why do we need to pass a number into the functional call?

def mult_two_add_three(number):
print(number*2 + 3)

Call mult_two_add_three() here:

mult_two_add_three(1)

Link: https://www.codecademy.com/courses/learn-python-3/lessons/intro-to-functions/exercises/parameters?action=resume_content_item

Please use the </> icon in the middle of the text box menu bar when posting code, in order to preserve Python’s essential indentations. Your code should look like this, much easier to read:

def mult_two_add_three(number):
    print(number*2 + 3)

#  Call mult_two_add_three() here:
mult_two_add_three(1)

If you do not include a number (or a variable whose value is a number) in the function call, the function will not know what value to assign to the variable number when the line print(number*2 + 3) is executed. In the example shown, number will be assigned the value 1.

That name, number, in the function declaration, or header:
def mult_two_add_three(number):
…is a parameter. It is a placeholder that says:

“When this function is called, the number that you find in this same position in the function call will be assigned to the variable number within my function body.”

2 Likes

The drier, technical stuff about parameters is that as positional arguments those arguments are required in a call to the function.

The function dictates the inputs. Whatever we choose to call it/them is up to us as author.

Functions have purpose while objects have value. We give each names that best describe them in those terms.

The purpose of a function will be to perform a task with the objects it is given (known locally by their parameters).

The most primitive function in both maths and programming is the identity function; one which returns an exact copy of itself (sort of).

def f(x):
    return x

y = f(5)
print (y)    # 5

Programming isn’t about coding with literals, though. That 5 would be a dynamic variable.

y = f(x)

Consider,

x = [5]
y = f(x)
print (y)    # [5]
x[:] = y * 5
print (y)    # [5, 5, 5, 5, 5]
print (x)    # [5, 5, 5, 5, 5]

The value association of x and y makes them nothing more than name tags hanging on the same hook. Only by disassociation do we separate these two list references.

This tells us that data structures are passed by reference only, not value.

2 Likes