Comp 110 Arguments vs. Parameters

Parameters

A parameter is a placeholder for a value used in a function definition in order to allow for coding without knowing the actual value that is going to be supplied.

Example:

function findSum(x: number, y: number): number {
    return x + y;
}
//when writing the function findSum, we want to be able to plug in any numbers!
//the parameters 'x' and 'y' act as placeholders so that we can interact with the values without knowing their value

Arguments

Arguments are what satisfy the parameter of a function, and they must be the same type as the parameters given. 

They are the expressions/objects that the user is choosing to pass into a function call.

Example:


function findSum(x: number, y: number): number {
    return x + y;
}

findSum(1, 2);
//when we call findSum, we must choose two number objects to pass into the function...these are arguments!

What is the argument passed into a function isn't the same type as its corresponding parameter? Error!


findSum("4", 3);
//will throw an error because "4" is a string object and therefore does not match the needed parameter!