/    Sign up×
Community /Pin to ProfileBookmark

Javascript Return Value

Hey everyone ! Im totally new into OOP and and just started studying Javascript after learning HTML and CSS. While studying Functions I had problems of understanding the Return Value. I searched a lot and watched lot vid tuts about it but still didnt get the idea. Any other resources will be appreciated. Thanks to all ! I would like to mention again that im totally new in OOP so please make the examples easy as possible.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@bionoidDec 24.2011 — Return value?

Are you talking about how a function returns a value:

[CODE]function TestFunction()
{
return 123;
}
alert(TestFunction()); //123[/CODE]


... or are you talking about the result of instancing a function ...

[CODE]function TestClass()
{
var value = 123; //PRIVATE
this.go = function() //PUBLIC
{
return value;
};
}
var Test = new TestClass();
alert(Test.go()); //123[/CODE]


... or is it something more complicated, that you probably don't need to know it right now?
Copy linkTweet thisAlerts:
@djqbertauthorDec 25.2011 — Im asking how does a Return value works. Both of your examples works great if you can explain them.Thanks.
Copy linkTweet thisAlerts:
@Christophe27Dec 25.2011 — A function is a piece of code which returns a value. This value can be anything, a string, number, boolean (true/false), ... By default it is 'undefined'. You usually create a function to make your code more synoptic and don't repeat yourself in your code. You can use the return value by assigning it to a variable.

Image a circumstance where you need a simple calculation function to add multiple numbers. You could write a function that does it for you, so you don't have to do this over and over in your code.

Simple function:
[CODE]
function sum(num1, num2) {
var sum = num1 + num2;
return sum;
}
[/CODE]


Then, in your code you can use the functino like follows:
[CODE]var int1 = 4;
var int2 = 6;
var mySum = sum(int1, int2);[/CODE]


In this previous piece of code on line 3 you call the function, and append the return value to the variable mySum. The appending happens behind the scenes, you just have to create a var, and use an equal sign '='.

Now you can do anything you want with mySum.

For simplicity, I have used a simple example. In reality you won't need a functions which adds two numbers, since this would be shorter to do it in the script itself. You can use for example a function, which loops over all inputs to check if a value is given, and if not, prevent the default behavior of the browser, and give an error to the user.

Success!

Christophe
Copy linkTweet thisAlerts:
@djqbertauthorDec 25.2011 — Thank you Christophe ! As far as i understand return is end of the function and gives you the result of a function ? And can you please tell me the difference between parameter and variable. I couldnt find a difference between them.
Copy linkTweet thisAlerts:
@Christophe27Dec 25.2011 — Yes, for now, understand that return is the end of the function which returns its result. You don't have to return anything, if this is not necessary.

During programming you have to assign values (strings, boolean, number or even complete functions) to variables. You ALWAYS have to declare variables with var.

[CODE]var MB = 256;[/CODE]

You can assign a variable without var, but then it becomes a property of the global namespace (window), which you have to avoid in JS. (but this is some material for later)

Then you can pass this variable as a parameter. A parameter is a variable between the parentheses ( ) of a function

[CODE]function (parameter) {
alert(parameter);
}[/CODE]


Always nice to help new JS coders!

Success!

Christophe
×

Success!

Help @djqbert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...