02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Function Return

We know how to create functions that is great if all we wanted to do is output to the browser things in an alert message but in reality most functions we will use will return a value that we can then use or send to a new function. in this video we cover the basics of how to get one value from the function back into a variable.

Video Transcript

00:00So we're making the steps ahead in our application.
00:03The only problem is that we actually really
00:06want to take that value and put it into another variable.
00:09And maybe it's because we need to use it.
00:11We don't want to just output it onto the screen.
00:15So so far with our operations, all we could have done
00:17is basically output it onto the screen.
00:20So the next step for us is actually
00:22to fetch that information when we complete a task.
00:25So let's do that.
00:26So let me just comment out this function.
00:31And we're going to grab our function again.
00:36All right.
00:36So the next step with this one is basically
00:38we want to get rid of that alert.
00:40And we actually want to send that information.
00:42In our ideal scenario, we want to create
00:44a variable that's going to be a clean variable.
00:47So let's say we'll call it NC.
00:51So NC is going to accept the value that was in NA
00:55because NA was the negative one.
00:58And let's make this work.
00:59So to make this work, it's actually really, really simple.
01:02There's only one rule.
01:04A function could return a value.
01:05Basically, by calling a function, it could return something.
01:08Right now, it's not returning anything.
01:09That's why we have to output it.
01:11To make the function return something,
01:13we could only return one value.
01:15And that's it.
01:16Not one value at a time, but only one variable
01:19could be sent back.
01:20To send back that one variable, instead of using an alert,
01:23what we're going to do is say return.
01:26As soon as we give the command return,
01:28it's going to terminate the function.
01:30Now, it doesn't really matter if there's
01:33more code underneath here.
01:35It will never trigger if we did our return.
01:39Traditionally, you only put your return
01:41on the bottom of your function as the last thing
01:43because you don't want to add confusion into the code itself.
01:46So basically, we're just going to return that new variable
01:49that we just cleaned up from our function, abs.
01:53So by doing that, our variable is going to come back
01:56and go inside of our NC.
01:57Now, if I refresh the code, I'm not going to see anything.
02:00There's not going to be any alerts.
02:01And the reason is because we're not using alerts.
02:03But we could test out and see if our code is working
02:05by just putting here a little alert and writing there
02:08NC, refreshing our screen again.
02:10And then we could see that we're getting the value back.
02:13And it's 20 instead of minus 20.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!