HTML Academy
Function, I’m calling you!
Functions6/24
Back to the list of tasks
  • 1. Fasten your seat belts
  • 2. Long-distance flight
  • 3. Going the second circle
  • 4. Helper function
  • 5. Function, I’m calling you!
  • 6. I have a parameter for you
  • 7. Order of parameters
  • 8. Return from function
  • 9. Summary of “Functions”. Part 1
  • 10. Seventh program: “From salary to salary”
  • 11. Business trip
  • 12. Write, simplify
  • 13. Get rid of unnecessary
  • 14. Make me beautiful
  • 15. Just add percentage
  • 16. Call me quietly by name
  • 17. I see the target
  • 18. How many flights?
  • 19. Let’s clean up a little
  • 20. Who is faster?
  • 21. Saving up for a trip around the world
  • 22. Summary of “Functions”. Part 2
  • 23. Eighth program: “Money makes money”
  • 24. Ninth program: “The Eternal Question”
Order of parameters
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

I have a parameter for you

We wrote a function, called it and counted the miles accrued for the flight to Irkutsk. And what about the flight to Kamchatka? How do you calculate the result of the second trip?

However many times we call the calculateMiles() function, the result will be the same, because inside the function, there are variables with fixed values ​​suitable only for the flight to Irkutsk, that is, only for one case.

Inside the function in the variable distance we store the distance to Irkutsk. To find miles for another flight, you need to change the value of this variable. But we will not rewrite the function with a different distance value. We will make the function universal so that it can be written once, and then used for different cases. To make the function really universal, you must use parameters.

Parameters are values ​​we pass to the function. With their help, we can calculate the result of its operation for specific cases.

Let’s see how it works.

var showTime = function (hours, minutes) {
  console.log('Current time: ' + hours + ':' + minutes);
};

showTime(3, 15);    // Displays: Current time: 3:15
showTime(16, 20);   // Displays: Current time: 16:20

For the function to work with parameters, they must be somehow expressed and given names. Let’s figure out how we can do it.

1. Set the parameters.

var showTime = function (hours, minutes) {
  …
};

When the function is declared, in the parentheses of the function itself, the parameter is given a name. This parameter starts behaving like a variable and can be used in the function code. If there are several of them, the parameters are separated with a comma.

2. Use the parameters.

var showTime = function (hours, minutes) {
  console.log('Current time: ' + hours + ':' + minutes);
};

Parameters work just like variables. We substitute them for fixed values in the operation inside the function. When the code is executed, each parameter is substituted with its value.

3. The function receives parameter values

showTime(3, 15);    // Displays: Current time: 3:15
showTime(16, 20);   // Displays: Current time: 16:20

In the previous steps, we examined how the function works with the received parameters. But we have not yet discussed how to make the function receive them. At the time of the function call, we indicate in parentheses those values that appear in the parameters. We write showTime (16, 20) and instead of hours in the body of the function 16 is substituted, and instead of minutes the number 20.

Let’s improve our code, finally make the calculateMiles function a universal one and calculate the miles accrued for the second flight.

By the way, we say “the function takes parameters”, but at the same time we “pass arguments to the function”. Why the arguments? It was about the parameters! The fact of the matter is that parameters are values ​​that we set at the time a function is declared. We also use them in the function body. And arguments are values ​​that we transfer to a function when it is called.

Comments

  • script.js
JavaScript
var calculateMiles = function () { var distance = 4125; var percent = 0.25; if (distance > 10500) { percent = 0.35; } var miles = distance * percent; console.log('We’ll get ' + miles + ' miles for the flight to Irkutsk'); }; calculateMiles();

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

Console

The code has changed, click “Run” or turn autorun on.

Result

Goalscompleted
  1. Change the message We’ll get miles for the flight to Irkutsk to a more universal: We’ll get miles for the flight.
  2. Add parameter distance to the function.
  3. In the function call, specify argument 4125.
  4. Inside the function body, delete variable distance.
  5. At the end of the program, add another calculateMiles call with argument 11000.

Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2025

VISAMastercard

Log in

or

Forgot your password?

Sign up

Sign up

or
Log in

Restore access

Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

Forgot to connect your email to the profile? Email us and we’ll help.