Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

question about return;

Laine Olsen
Registered User
Join date: 6 Jun 2004
Posts: 3
10-30-2004 12:37
Hi,

I am an exterme novice at LSL. Can someone explain to me what 'return;' does. I have a script that only works once. I think its stuck in a global function and I am not sure how to fix it.

Thx
Tiger Crossing
The Prim Maker
Join date: 18 Aug 2003
Posts: 1,560
10-30-2004 12:46
If you put "return;" into a global function, when that step is executed the function will end and return control to whatever code called it. It's equivillent to running to the end of the function, but anything after the return is skipped.
CODE
MyFunction()
{
// do something
if ( end_early == TRUE )
return;
// if end_early was true, anything down here won't run
}
_____________________
~ Tiger Crossing
~ (Nonsanity)
Kurt Zidane
Just Human
Join date: 1 Apr 2004
Posts: 636
10-30-2004 23:38
when a function is called the script begins reading at the top of the function. It read down through the function, until it reaches a return command, or the end of the function character.


// on call of data
functionBat()
{
llSay(0, "cat";);
llSay(0, "dogs";);
}

data()
{
functionBat();
llSay(0, "mice";);
}
//prints to screen:
//cat
//dogs
//mice

----------------
//on call of data

functionBat()
{
llSay(0, "cat";);
llSay(0, "dogs";);
return;
}

data()
{
functionBat();
llSay(0, "mice";);
}
//prints to screen:
//cat
//dogs
//mice


------------------
// on call of data

functionBat()
{
llSay(0, "cat";);
return;
llSay(0, "dogs";);
}

data()
{
functionBat();
llSay(0, "mice";);
}
//prints to screen:
//cat
//mice



------------------
// returning values

string functionBat()
{
return "cat";
}

data()
{
llSay(0, (string)functionBat() );
}
//prints to screen:
//cat


// returning values as integer

integer functionBat()
{
return 5;
}

data()
{
integer total;

total = 1 + functionBat();

llSay(0, (string)total );
}
//prints to screen:
//6


// returning values from a variable

integer functionBat()
{
integer counter = 5;

return integer;
}

data()
{
integer total;

total = 1 + functionBat();

llSay(0, (string)total );
}
//prints to screen:
//6



I was going to try to write out an explanation, but I thought some examples would be more useful.
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
10-31-2004 13:18
code looks really nice if you put it in [ php ] or [ code ] brackets

CODE

// on call of data
functionBat()
{
llSay(0, "cat");
llSay(0, "dogs");
}

data()
{
functionBat();
llSay(0, "mice");
}
//prints to screen:
//cat
//dogs
//mice

----------------
//on call of data

functionBat()
{
llSay(0, "cat");
llSay(0, "dogs");
return;
}

data()
{
functionBat();
llSay(0, "mice");
}
//prints to screen:
//cat
//dogs
//mice


------------------
// on call of data

functionBat()
{
llSay(0, "cat");
return;
llSay(0, "dogs");
}

data()
{
functionBat();
llSay(0, "mice");
}
//prints to screen:
//cat
//mice



------------------
// returning values

string functionBat()
{
return "cat";
}

data()
{
llSay(0, (string)functionBat() );
}
//prints to screen:
//cat


// returning values as integer

integer functionBat()
{
return 5;
}

data()
{
integer total;

total = 1 + functionBat();

llSay(0, (string)total );
}
//prints to screen:
//6


// returning values from a variable

integer functionBat()
{
integer counter = 5;

return integer;
}

data()
{
integer total;

total = 1 + functionBat();

llSay(0, (string)total );
}
//prints to screen:
//6
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Laine Olsen
Registered User
Join date: 6 Jun 2004
Posts: 3
10-31-2004 14:24
Thx guys, I think I follow what you are saying. I am still not sure how to fix my code though. Perhaps I should rephrase the question. Where does the code go next after the return; is called? Also, if a global function did not have a return;, what would happen after the function is completed?

Thx
Cross Lament
Loose-brained Vixen
Join date: 20 Mar 2004
Posts: 1,115
10-31-2004 15:15
As I understand it, return will exit the current function and return code execution to the point in the script where that function was called from.
_____________________
- Making everyone's day just a little more surreal -

Teeple Linden: "OK, where did the tentacled thing go while I was playing with my face?"
Water Rogers
Registered User
Join date: 1 May 2003
Posts: 286
10-31-2004 15:34
perhaps you could post a little code so we can see the exact problem to help you out?

here is a quick idea of how scripting kinda works:

CODE
// Anything with double slashes will turn orange and the script does not read that line.  Commony known as a Comment

//First you declare your global variables. It's a matter of scripting preference and your style... but i tend to put a "g" in front of all my global variables


string gHelloMessage = "Hello Second Life";

// Now after we declared our global variables, we can declare functions

func_message(){
llSay(gHelloMessage);
}

// After we declare our functions, now we can go into the default state of the script

default{
state_entry(){
func_message();
}
on_rez(integer t){
func_message();
}
touch_start(integer t){
func_message();
}
}


As you can see here, when the script is saved, it will first enter the default state. Scripts can have multiple states, however-it will always enter the default state first. After it enters the default state, it will check for a "state_entry" event handle. We have one here - so basically, after you save the script, it will fire what's in the state_entry event handler first. Events are raised depending on how you want your object to react to certain things (like touching for instance). So under the state entry, we tell it to fire the func_message() function.

The purpose of functions, is to save time on having to rewrite code over and over that you may want to reuse... and to save on the memory consumption of your script. This is a very basic way of implementing a function, but as you can see - we want the same code to fire in multiple events (we want the object to say "Hello Second Life" when 1) The script is saved, 2)When it is Rezzed out of inventory, and 3)If it is touched.

"Return" can be used mainly for 2 purposes. To either Return data to a function, or to say stop a function and return back to code.

Here is an example of the two:
CODE
// This is a function that returns data... in this case a string message.
string func_helloMessage(){
return "Hello Second Life";
}

//Here is a function that would return a boolean value TRUE or FALSE if the creator of the object is the same as the owner

integer func_IsAlsoCreator(){
if (llGetCreator() == llGetOwner()){
return TRUE;
}else{
return FALSE;
}
}

// And here is a function that would return a boolean value depending on a loop and has arguments

list gList = ["Water Rogers"];

integer func_isInList(string name){
integer i;
integer listLength = llGetListLength(gList);
for(i = 0 ; i < listLength ; i++){
if(llList2String(gList, i) == name){
return TRUE;
}
}
return FALSE;
}


Also, there are different ways to declare a function - the above posts describe how to do that pretty accurately... hope this helps!

--Water
_____________________
From: Philip Linden

For the more technically minded - the problem is actually NOT the asset server (or 'asshat' as you prefer to affectionately call it herein).
Wednesday Grimm
Ex Libris
Join date: 9 Jan 2003
Posts: 934
11-01-2004 09:04
This is one key difference between LSL and other programming languages.

It's best if you think of your script doing *nothing* most of the time, just sitting there on the server waiting for somehing to happen. When "something" happens, one of the current state's event handlers is called, it does it's thing, then the script goes back to doing nothing.
_____________________
Sarcasm meter:
0 |-----------------------*-| 10
Rating: Awww Jeeze!
Laine Olsen
Registered User
Join date: 6 Jun 2004
Posts: 3
11-01-2004 19:02
Thank you, everyone. You have all been a big help. I finally got my script working. Plus I have a good understand of return; and how I was using it incorrectly. :D
Kurt Zidane
Just Human
Join date: 1 Apr 2004
Posts: 636
11-01-2004 20:56
about functions...

CODE

integer number = 4;
llSay( 0, (string)function( number) );

integer function( integer input)
{
integer outPut;

//processing

return outPut;
}


I'm using the sudo code above for my question.. A function like the one above, when the function is called it creates a variable called input, and then copys the values in variable number into variable input. At leas that's what I think it's doing, is that Right?
Dose sls support veriable linking? ie instead of creating a new variable input and copying number into input, input would be a pointer to number.
Or is there a way to make the variable input static?
And what dose it mean when people put # infront of a variable?
Water Rogers
Registered User
Join date: 1 May 2003
Posts: 286
11-02-2004 02:23
From: Kurt Zidane
about functions...

CODE

integer number = 4;
llSay( 0, (string)function( number) );

integer function( integer input)
{
integer outPut;

//processing

return outPut;
}


I'm using the sudo code above for my question.. A function like the one above, when the function is called it creates a variable called input, and then copys the values in variable number into variable input. At leas that's what I think it's doing, is that Right?
Dose sls support veriable linking? ie instead of creating a new variable input and copying number into input, input would be a pointer to number.
Or is there a way to make the variable input static?
And what dose it mean when people put # infront of a variable?

functions are just code snippets in your scripts. They can be declared with as many arguments as you want/need and can return data or not return data.

All of these functions will do the exact same thing.
CODE
/////////////// Concept 1: Say "Hi"
function(){
llSay(0, "Hi");
}

state_entry(){
function();
}


/////////////// Concept 2: Say "Hi"
function( string alternate_way ){
llSay(0, alternate_way);
}

state_entry(){
function( "Hi" );
}


/////////////// Concept 3: Say "Hi"
string function( integer check ){
if (check == TRUE){
return "Hi";
}else{
return "Bye";
}
}

state_entry(){
llSay(0, function( TRUE )); // If True - then then the output would be "Hi", if False - then the output would be "Bye"
}

There's still a number of other different ways you can do this... as i'm sure you will figure out.

it's not really 'copying' the variable - which would suggest multiple instances of the same data - rather more like Referencing the variable into a required argument list needed by the function. The arguments that you create are required variables needed for the function to operate correctly. But i think you have the general jist of it down.

The return command is not a requirement in functions btw unless you NEED to return data back to your function. Return simply means to "Give your function the resulted data". If you have the word integer in front of your function, then you are stating that the result of your function needs to return an integer as it's main purpose. If you aren't declaring the function as a datatype... then return simply means "Stop the function, and return to code right where the function was last called".

Pointers (like in C++) are not supported in LSL.

Hope that helps.

--Water
_____________________
From: Philip Linden

For the more technically minded - the problem is actually NOT the asset server (or 'asshat' as you prefer to affectionately call it herein).
Wednesday Grimm
Ex Libris
Join date: 9 Jan 2003
Posts: 934
11-02-2004 10:13
From: Kurt Zidane

I'm using the sudo code above for my question.. A function like the one above, when the function is called it creates a variable called input, and then copys the values in variable number into variable input. At leas that's what I think it's doing, is that Right?


Correct.
From: Kurt Zidane

Dose sls support veriable linking? ie instead of creating a new variable input and copying number into input, input would be a pointer to number.
Or is there a way to make the variable input static?
And what dose it mean when people put # infront of a variable?

You are talking about "call by name [or in some circles, reference]" verses "call by value". LSL is "call by value". This means that the value of the variable is sent to the function, and any changes to that value inside the function stay inside the function, there's no way to change this. In call by name languages, the function argument becomes a kind of alias for the variable passed in, and changes made inside the function effect the value outside the function. (for those who weren't paying attention earlier, LSL is *not* like this)


Aside: The ancient and venerable computer scientist, Niklaus Wirth once noted that Eurpoeans pronounced his name correctly, (Ni-klows Virt) whereas Americans usually mangled it in to "Nickles worth". He decided that Europeans were calling him by his name, whereas the Americans were calling by value.
_____________________
Sarcasm meter:
0 |-----------------------*-| 10
Rating: Awww Jeeze!