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:
Discover the features of functions:
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.
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:
Look at the code:
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.
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.
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.
The result of the function is now in the result variable, from which we will be able to use it later.
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.
Tip: The function is called by giving its name with round brackets, without parameters.
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.
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.
Look at the code in the JavaScript file. Call the function there in such a way that a greeting with your name appears underneath.
Tip: Next to the function declaration, replace the name parameter with a value.
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.
Tip: If everything is correct, the result for 50 ° Fahrenheit should be 10 ° Celsius.
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.
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. </div>