Jump to content

Passing a list as a function argument


SimplifyKidd
 Share

You are about to reply to a thread that has been inactive for 1268 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Yes, but remember that the_list is local to function_a .  If you want to use the result from function_a anywhere else, you will have to add return the_list; .  As written, of course, your example looks like an awkward way to just add "a new value" to my_list, so I'm assuming that you have something more in mind.

  • Thanks 1
Link to comment
Share on other sites

Thanks a lot! Yes, my example was just an example to know if the argument would be passed as a pointer, so that my_list from function_b would actually have a value after calling function_a. But it seems that I will have to return the list to get the value back to the calling function_a.

Link to comment
Share on other sites

15 minutes ago, SimplifyKidd said:

But it seems that I will have to return the list to get the value back to the calling function_a.

Ah, but if you return it to function_a, you could be in an infinite loop if you aren't careful.  (You didn't ask whether this would be a smart thing to do; just whether you could do it.  ;) )  That's why I was asking whether there's more to the example you presented.  You can prevent the infinite loop by adding, for example, a flag that says "if the answer has just come from function_b, don't send another request right away."  That way, function_a can send a list to function_b, get the answer back, and use it for a different purpose instead of sending it back to function_b again.  As I said before, your example -- as written -- seems like an awkward way to just add "a new value" to the_list.

Edited by Rolig Loon
  • Like 1
Link to comment
Share on other sites

21 minutes ago, SimplifyKidd said:

Thanks a lot! Yes, my example was just an example to know if the argument would be passed as a pointer, so that my_list from function_b would actually have a value after calling function_a. But it seems that I will have to return the list to get the value back to the calling function_a.

All data in LSL is pass-by-value, never by reference.

If you have a list of 100 elements and you pass it to a function, the entire list is copied and you have two lists of 100 elements.

Edited by Wulfie Reanimator
  • Like 3
  • Thanks 1
Link to comment
Share on other sites

5 hours ago, SimplifyKidd said:

so that my_list from function_b would actually have a value after calling function_a. But it seems that I will have to return the list to get the value back to the calling function_a.

yes

depending on what our goal is then the design can be ok. Ok meaning that we want to separate out what is happening, rather than inline all the code

 
function_a(list the_list)
{
  return the_list += ["a new value"]
}

function_b()
{
  list my_list;
  return function_a(my_list);
}

list this = function_b();

 

 

  • Like 1
Link to comment
Share on other sites

5 hours ago, Wulfie Reanimator said:

All data in LSL is pass-by-value, never by reference.

If you have a list of 100 elements and you pass it to a function, the entire list is copied and you have two lists of 100 elements.

Yes. Which is unnecessary, since lists are immutable. It discourages programming where lists are passed through several functions.

  • Like 1
Link to comment
Share on other sites

Yes, it would have been much nicer if the list was passed by reference. What I need to do is convert a list that comes through a http call (so it is just a string with comma separated values) and convert it into an SLS list. I need to do this for up to 10 different lists (maybe more), so I wanted to be able to code the conversion of any string into any list in function_a and just call that function with the different lists and comma separated strings from function_b:

list function_a(string Values, list The_list)

{

  ... convert Values into The_list ...

  return (The_list);

}

function_b

{

  List1 = function_a(Values1, List1); 

  List2 = function_a(Values2, List2);

  ... etc

}

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1268 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...