Learning Javascript - Helper Functions lesson

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!
Hello, I’m currently learning about “helper functions” in “introduction to javascript” here and I 'm a bit confused.

And this might be very basic programming thing but I am a newbie, my first time :slight_smile:
[1:09 pm, 15/04/2020] I Get Lucky: function multiplyByNineFifths(number) {
return number * (9/5);
};

function getFahrenheit(celsius) {
return multiplyByNineFifths(celsius) + 32;
};

getFahrenheit(15); // Returns 59
When a return statement is used in a function body, the execution of the function is stopped…so how will this run?
i mean how will the code read…will it read backwards? from down to top?

also if at the end after getFahrenheit(15) , we write return getFahrenheit(15); will that stop the execution?

Just trying to understand how the code is read and use of return- where it aplies or does not.

Thanks a ton… Please advise, if I am missing something basic please let me know what can I read to overcome such things.

Thanks a ton again

Hello @byte1052171993 welcome to the forums!
When it says this:

It means that the function stops at that line. The code will read that line, but not past it. For example, if I wrote:

function returnsStuff(){
  return "Hello World!";
  console.log("Will this get printed?");
} 
returnsStuff();

Nothing would be printed. This is because I am calling the function returnsStuff, but since I am not printing it, and the return comes before the console.log(), nothing happens. That means if I did this:

function returnsStuff(){
  return "Hello World!";
  console.log("Will this get printed?");
} 
console.log(returnsStuff());

Hello World! would be printed, since I am printing the returned value of the function.

Also, when it says that the execution of the function stops at a return, it means only the code inside the curly braces {} of the function. Everything outside of that works fine. If you wanted additional reading material, here are some good links:https://www.w3schools.com/js/js_functions.asp and https://codeburst.io/javascript-functions-understanding-the-basics-207dbf42ed99. These are good, but I’m sure there are other good sites.

Here is some brief code you can play around with (you can use this online compiler: https://paiza.io/projects/imd_FXjlMwSvnLQ4b7kMfw?language=mysql):

function func1(){
  let variable1 = 3;
  let vr2 = 4;
  return variable1 + vr2;
  console.log("This wion't be printed:(");
}


/*random print*/console.log("This gets printed, because it is outside of the curly braces of the function.\n");
/*again*/console.log("The next line will print the return line of the function:\n");
/*demonstrating what happens inside the function.*/console.log(func1());
/*demonstrating what doesn't print*/func1();
/*brief explanation*/console.log("See how the console.log inside the function didn't print?");

I hope this helps!
Happy coding!

1 Like

Thanks a lot…I will check these out

1 Like