Comp 110 Primitive Parameters

Primitive Parameters

Primitive parameters are parameters of a function that are of primitive types. This means in a function with parameters, any parameter that has type numberstring, or boolean is a primitive parameter.

When you call a function with parameters, you supply it with arguments. For primitive parameters, the value of the argument you give is assigned to the parameter. Let's get a closer look at what's happening through an example:

import { print } from "introcs";

let x = 4;
let y = 5;
let add = (x: number, y: number): number => {
    x = 2;
    return x + y;
print(add(x, y));

In this code snippet, we have some number variables, a function declaration, and a function call.

When we call add(x, y) we are calling the add function with the first argument as x and the second y. So, we make a new frame on the stack for this function call. What happens is the value of x (which is 4) is assigned to the parameter x within this frame, and the value of y (which is 5) is then assigned to the parameter y that also exists only within the frame

Within the frame, we have x assigned 4 and y assigned 5. Everything we do from within the function call is dealing with these local x and y variables within this specific frame on the stack. So, the line x = 2; within our function is updating our local x (the one that is a part of the function add call's frame on the stack) to be 2 now instead of 4. Then, we return x + y, so we return 2 + 5 which is 7. This is returned to where we called the function from, which happened to be inside a print statement. As a result, we print 7.

The next line in our program is print(x);

What is printed here?

The key is to know is which x we are referencing. We are no longer executing code within our add function, so we are no longer dealing with the x and y variables inside the function call's frame on the stack. This means we're now working with the original x variable we had before we even encountered the function call.

Since our add function had number parameters, these parameters were primitive parameters. So just the value of the argument was copied in for the parameter and used within the function. 

This means that in the last line of our program when we say print(x); we are going to print 4. This is because we are back outside our add function and talking about our original x, which was defined in the global scope. This value is still 4.

The assignment statement x = 2; within the function add did NOT change the value of the global x. This is because when we called the function, since it had primitive parameters we just passed a copy of the value 4 INSTEAD OF a reference to the variable itself. Then, after the end of the function call, we moved on and no longer used the local x (the one we changed), and could still come back to the global x without issues.

When we were going through the function call and using x and y as arguments, all we were doing was assigning the value of x to the parameter it was used as an argument for (also called x) and doing the same for y. This may sound confusing, but all it really means is that we copied the value stored in x into the parameter and when we used y as an argument we did the same there. So, since we were just copying the value (meaning the actual number stored in the variable), the original variable's value never changed.