Lambda School Pre-course 14: Learn to write and call functions using arguments and parameters

There are nine units in Lambda School’s Full Stack Web Development Pre-course. I’m going to summarize what the pre-course covers for each of them. This material is basically ground-floor information that students need to know before they start the full course.

precourse14

In the previous post about Lambda School’s web development pre-course, we looked at what a function is and the proper syntax to use when writing one. Now, we will look at how to pass data to a function.

Passing Data into Functions

A major reason to use functions is for code reuse. To accommodate this, functions allow data to be passed to them for use inside of the function. By doing so, the same code can be used over with different variables or data. Function variables are called parameters and are used to pass data to the function when its called. Once inside of the function, they’re called arguments.

Parameters

Parameters are the names listed in the parenthesis when a function is declared. They accept and hold data to be used inside of a function. Arguments are the actual values passed to (and received by) the function, so essentially one is the variable and the other is the actual data in the variable.

Parameters are defined when a function is created by naming each parameter inside of the parenthesis following the function’s name. They are separated by commas, and their data types are not specified.

As per the Lambda School text:

A function parameter will represent the data we pass into a function, for use in the function. Essentially when we write a function we assign the data variable names, even without knowing what the data will be. We set these variables inside of the parentheses when we write the function. There is no limit to the amount of parameters we can include in a function, but each variable name must be separated by a comma. We can then use these variables within our function just as we would any other variable.

function myFunc( parameter1, parameter2 ){
// We can use parameter1 and parameter2 in this function just like a variable.
}

Arguments

Arguments are objects that are built into the function. They are passed to a function by value, so the function knows the contents of an argument, but doesn’t know where that data came from. If a function changes an argument’s value, it does not change the original parameter’s value; so changes are local to the function, not global to the entire program.

As per the Lambda School text:

Once we have our parameters set up in our function, we can now pass data into the function. In order to do this, we will use the parentheses we write when we call the function. We call these pieces of data arguments. arguments can be ANY data type (string, number, Boolean, object, array, even other functions!). Unlike other languages, JavaScript does not require us to set the data type when we write the function, although you should make an effort to understand what type of data will be coming into the function (and if you are using pre-built functions, you should know what data type that function expects).

To use an argument, simply put the argument’s data in the function call parentheses:

function logsName(name){ console.log(name); }

logsName(‘Vish’) // console.logs: Vish
logsName(‘Godzilla’) // console.logs: Godzilla

Multiple parameters require the use of more than one argument:

function logsArtist( artist, descriptor ){
console.log(${artist} ${descriptor}`);
}

logsArtist(‘Chuck Shuldiner’, ‘is the godfather of death metal!’)
// console.logs: Chuck Shuldiner is the godfather of death metal!

Arguments line up with parameters in order, so the first argument gets data from the first parameter, and so on. If an argument is not given for a parameter, the parameter will be equal to undefined.

The recommended way to skip entering data for a given parameter, according to Stack Overflow, is to pass the value “undefined” to the function when calling it. This is essentially the same as the default parameter, as per the Function parameters section of this Mozilla Developer Network article.

2 thoughts on “Lambda School Pre-course 14: Learn to write and call functions using arguments and parameters

Leave a comment