Back to Top

Functions

It's time for the next dose of knowledge. We present the function, or block of code used to perform a specific task. What is important, it should be called. Look at the example to understand what it means. Let's start with the syntax of a function.

Look at the example
function name(parameter) {
// code to perform
}

Discover the features of functions:


• keyword function

• name of the function (can be any name, but it is recommended that it consists of verbs, e.g. checkName),

• one parameter or many parameters, written in brackets (note that a function may also not accept any parameters),

• code to be executed, written in curly brackets.

In the parameter you can pass, among others, any word, e.g. "pineapple", and the function is to perform a specific task, e.g. change the size of characters, reverse the order of letters or check how many times the character "a" is repeated. In our example, we will pass two numeric variables, and the aim of the function will be to multiply them.

function multiply(num1, num2) { // you can specify more than one parameter
return num1*num2;
}

Our function is named multiply. To call it, we enter its name, and then parameters in place of num1 and num2, therefore, we enter the numbers that we want to multiply - in our case 4 and 5. Time to call the function:

multiply(4,5);

Look at the code:
Example of multiply function

What happened? We called the previously declared multiply function and substituted the num1 and num2 parameters for two numbers. The inside of the function during such a call looks like this: return 4 * 5, and the result of this multiplication is 20. Remember to always use round brackets, even if the function does not accept any parameters, which is also possible.


return

Functions must always return something. If they did not do so, they would be working inside themselves, but nothing would be noticeable outside. Imagine a craftsman working in the comfort of his workshop. If he does not bring out the effects of his work outside of the workshop, no one will be able to use them. The return keyword is used to transmit the function's result. The code in the following lines will not be executed, so remember that this keyword should always be the last one.

function multiply(num1, num2) {
num1*num2;
}
multiply(4, 5);

This function will not return anything. Our craftsman will do the job, but he will keep the result for himself.

It is very convenient to assign the result of a function to a variable (as we did in the above example), thanks to which we will be able to use the result of this work many times, e.g.

var result = multiply(4, 5);

The result of the function is now in the result variable, from which we will be able to use it later.

Exercise 1

Before you start: after clicking the following links for exercises and examples, select the index.js file (on the left) and press "Run" (above the code window) to generate the code.

Look at the code in the JavaScript file. You will find there a function that prints the text. Call it.

Go to the first exercise: Exercise 1

Tip: The function is called by giving its name with round brackets, without parameters.

Exercise 2

Before you start: after clicking the following links for exercises and examples, select the index.js file (on the left) and press "Run" (above the code window) to generate the code.

Look at the JS file. You will find there a function and a variable to which its result is assigned. Then the variable itself is displayed below. This function takes two parameters and returns the result of their multiplication. Correct the call of the function so that the equation (5 * 12 = NaN) is correct.

Go to the second exercise: Exercise 2

Tip: In case of problems, compare the parameters at the declaration of the function and its call in the variable and read again the section on the parameters of the function.

Exercise 3

Look at the code in the JavaScript file. Call the function there in such a way that a greeting with your name appears underneath.

Go to the third exercise: Exercise 3

Tip: Next to the function declaration, replace the name parameter with a value.

Exercise 4

Look at the code for exercise 4 in the JavaScript file. There is a function that converts degrees Fahrenheit to Celsius. Add the temperature you want to convert to the fahrenheit variable. Assign the result of this function to the variable result2, and give the variable fahrenheit in parentheses.

Go to the fourth exercise: Exercise 4

Tip: If everything is correct, the result for 50 ° Fahrenheit should be 10 ° Celsius.

Exercise 5

Time to write your own function. In the JS file you have space to write your own function. Give it any name, but remember about good practices. The function is to take a parameter with any name: your age (e.g. 27), and return the year of birth (e.g. 1990). How to write the code?
You should subtract the given parameter from the current year and return the result (e.g. return 2017 - parameter;).

Then call the function and save its result to the already prepared birthYear variable. If everything has been done correctly, the year of your birth should be displayed.

Go to the fifth exercise: Exercise 5

Tip: If you are not sure how to write a function, then look at the example from the theoretical part and then replace its parts.