JavaScript fundamentals
Theory: Function parameters
Functions can not only return values, but also take them as parameters. We have already seen the parameters of functions many times before:
In this lesson, we will learn how to create functions that take parameters as an input. Imagine we need to implement a function getLastChar() that returns the last character of a string passed as a parameter to it. The function will look like this:
From the description and code examples, we can draw the following conclusions:
- We need to define a function
getLastChar() - The function should take a single string parameter as an input
- The function must return a string type value
Definition of the function:
Let's analyze it. The name of the parameter (str) is given in brackets. We do not know exactly what value we are working with inside the function, so the parameters are always described as variables.
The parameter name can be anything; it has nothing to do with how the function is called. The main thing is that the name should reflect the meaning of the value contained inside. The specific value of the parameter will depend on the call to that function.
Two, three or more parameters can be specified in exactly the same way. Each parameter is separated from the other by a comma.
Recommended programs
Completed
0 / 39

