HTML Academy
Order of parameters
Functions8/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”
Summary of “Functions”. Part 1
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Return from function

And what if we need to get a value from a function and somehow use it later?

Functions are able to return the result of their work. We already talked about what “return” means in “Introduction to JavaScript” chapter. Take a look there if you forgot what “return” is and how it works.

Briefly, the function can execute the code and return the result of its actions for further use. This result is placed into the position where we called the function.

var increaseByTwo = function (number) {
  var sum = 2 + number;
  return sum;
};

increaseByTwo(1); // Function will return 3
increaseByTwo(2); // Function will return 4

How does this code works?

To return a value, we use the return statement. Then we specify a return value after the statement. In our case, it is a value of the variable sum. When the function reaches the line return, it returns the result and completes the code execution, in other words, we will exit the function.

A few things you need to know:

  • Code written on a new line after return is not executed.
  • A function cannot return many values ​​at once, it returns only one result.
  • If there is no return inside the function or if after return the return value is not specified, the function will return undefined, in other words, nothing.

As we mentioned above, the result of function actions can be used for further operations.

console.log('Sum of numbers: ' + increaseByTwo(1));
// Displays: Sum of numbers: 3

console.log('Sum of numbers: ' + increaseByTwo(2));
// Displays: Sum of numbers: 4

JavaScript has built-in language functions that return the result of their work. There are a lot of such functions, and here are just a few of them:

  • Math.ceil(number) accepts the input number and rounds it up to a whole number.
  • Math.floor(number) does the same, only rounds it down.
  • Math.round (number) rounds the number to the nearest integer value.

Round the number of miles in our calculateMiles function. Where did you see a fractional number of miles being accrued?

Comments

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

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. Edit the value of the variable miles rounding the result of calculations Math.floor(distance * percent);.
  2. Inside the function, replace console log message with return miles;.
  3. Replace the first function call with console log message 'We’ll get ' + calculateMiles(4125) + ' for the flight to Irkutsk'.
  4. Replace the second function call with console log message 'We will get ' + calculateMiles(11000) + ' miles for the flight to Kamchatka'.

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.