Comp 101 Parameters and Optional Parameters

What are parameters?

Parameters allow functions to require additional pieces of information in order to be called and are specified within the parentheses of the function definition:

// Function definition
let <name> = (<parameters>): <returnType> => {
    <function body>
};

They look very similar to variable definitions - because they are! Parameters are just local variables whose scope is inside their function. 

Functions do not have to require parameters, (think of our main function!) but they can often be helpful!

Another way to think of parameters is like the ingredients to a recipe! If our function body are the steps to make our recipe, and our return type is whatever we want to cook/bake, then the parameters are the ingredients - and you cannot make the recipe without the ingredients!


Optional parameters

Optional parameters are parameters that are not required by the function, and so the arguments don't have to be specified. 

We can use our recipe example to imagine this, as if we were baking a cake, we would need some variation of butter, eggs, flour, sugar, etc.. and therefore these would be the required parameters for our cake baking 'function.' 

However, we may have optional ingredients, such as coconut, flavourings, and extra fillings. Because these are not needed to produce our cake, they would be the optional parameters, as they are extras that can be provided to the recipe to provide additional elements!


Optional parameter syntax

We've already seen this in our emoji problem set - where we have the optional parameters and for some of our shapes classes!

new Rectangle(width: number, height, number, x?: number, y?: number): Rectangle

Optional parameters are specified by the ? operation after the variable's name

<name>?: <type>

However, optional parameters cannot just go anywhere in the list of parameters - they must always come lastwhy is this?

Imagine we wanted to declare a rectangle of width 50 and height 15, using the constructor above, we'd simply type:

let rectangle = new Rectangle(60, 15);

This would be no problem! However, imagine if we changed the syntax for declaring a rectangle to the following:

new Rectangle(x?: number, y?, number, width: number, height: number): Rectangle

If we tried declaring our rectangle the same was we did above, 60 would now be assigned to the rectangle's x property, and 15 to its y property, but our rectangle would have no dimensions - and the dimensions are the essential parameters for the rectangle class!

Therefore, we always make sure we put our optional parameters after all our required parameters, to ensure we've provided all the information the function needs. 

Handling undefined parameters

Because we don't need to specify optional parameters, we need to make sure they've been defined before we use them. We can do this with a special check using  undefined 

let buildName = (firstName: string, lastName?: sting | undefined) => {
    if (lastName !== undefined) {
        return firstName + " " + lastName;
    } else {
        return firstName;
    }
};