Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by wlad_s · Feb 11, 2011 at 01:31 PM · javascriptreturncallstatement

Javascript return statement

Hi,

I've only recently started learning javascript and it would be helpful if someone could explain the return statement in plain english.

For an instance, let's say I have this code:

class Return {
public static void main(String args[]) {
boolean t = true;
System.out.println("Before the return.");
if(t) return; // return to caller
System.out.println("This won't execute.");
}
}

In the javascript tutorial/explanation it says: "At any time in a method the return statement can be used to cause execution to branch back to the caller of the method."

But what/who is a caller of this method? :)

Thanx.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JChilton · Jun 28, 2013 at 07:18 PM 0
Share

a (simple) way of thinking of it is that calling return; "cancels" a method where it was at ins$$anonymous$$d of continuing on with code

1 Reply

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by Jessy · Feb 11, 2011 at 02:27 PM

First, that code is not JavaScript; where did you get it? Let's try to turn it into something that would work in Unity:

import UnityEngine.Debug; // Lets us type "Log" instead of "Debug.Log"

function Start () { var t = true; Log("Before the return."); if (t) return;

 // No further code can execute after the return statement.
 Log("Useless code here.");  

}

Now, in that case, we used Start; which is automatically called by Unity. Let's call another function, from Start, and see how return can actually be useful:

import UnityEngine.Debug;

var kittyIsHappy = false;

function Start () { MakeSureKittyIsHappy(); MakeSureKittyIsHappy(); // No feeding or petting happens this time. Log("We double-checked, so the kitty is surely happy now!"); }

function MakeSureKittyIsHappy () { if (kittyIsHappy) return;

 FeedKittyAHamster();
 PetKitty(); // You will be petting the kitty on a belly full of hamster! :-D
 kittyIsHappy = true;

}

function FeedKittyAHamster () { Log("You feed an extremely cute hamster to the kitty."); }

function PetKitty () { Log("You pet the kitty on his belly."); }

In that case, MakeSureKittyIsHappy was called, and all of the code in it was run. The execution then branched back to Start. MakeSureKittyIsHappy was called again, in Start, at that point, but only the first line was run, and execution then branched back to Start the return statement functioned as a way to avoid running unecessary code.

Alternative versions of that function, that do the exact same thing, are:

function MakeSureKittyIsHappy () { if (kittyIsHappy) return; else { FeedKittyAHamster(); PetKitty(); kittyIsHappy = true; } }

function MakeSureKittyIsHappy () { if (!kittyIsHappy) {
FeedKittyAHamster(); PetKitty();
kittyIsHappy = true; } }

Often, return statements can be used to eliminate else statements and brackets. Do whatever is easiest to read, to you. I prefer the first of the three examples (with no brackets), in this case. However, as displayed, I tend to put a blank line after my return statement, to signify that it represents an important ending. You'll figure out helpful coding style as you go along.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessy · Feb 11, 2011 at 02:37 PM 0
Share

I'm sorry, the formatting got screwed up. I'm working on it.
:-(

avatar image e-bonneville · Feb 11, 2011 at 02:38 PM 0
Share

Your link and code formatting don't seem to be working. ;)

avatar image e-bonneville · Feb 11, 2011 at 02:38 PM 0
Share

How'd you know what I was going to say 26 seconds before I said it!?

avatar image Jessy · Feb 11, 2011 at 04:57 PM 0
Share

I'm in yer braynz, steelin yer obzurvayshunz! >(o.O)< There are a couple little edits I still want to make, when I get to better internets, but it should hopefully be readable now.

avatar image wlad_s · Feb 13, 2011 at 06:28 PM 0
Share

Thanx Jessy! Poor hamster though :D I got that code from a site that I got to when I googled "java return explanation" or something like that. I should probably have entered "javascript" ins$$anonymous$$d of "java". But anyway, I didn't understand what they mean by "caller" but I get it now.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Unity communication to JavaScript in browser with return 2 Answers

How can I Javascript a phone call from within Unity? 1 Answer

Call SoundManager with JS? 0 Answers

Calling Functions from another Script 3 Answers

Script Performance Questions 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges