ID:140141
 
Code:
mob/verb
testproc()
set category = "TEST"
var/super = 0
usr << "BEFORE: [super]"
changesuper(super)
usr << "AFTER: [super]"
proc
changesuper(super)
super = 20


Problem description:
The problem is that testproc() is supposed to modify super=0 to super=20. I have no idea why it's completely ignoring the changesuper() proc.
When passing strings and numbers to procs you pass a value and not a reference. So you're never actually changing the variable you defined in testproc() but actually a new local variable within changesuper(). This is where return values come in handy.

mob
proc
One()
var/test = 0
src << "Test 1: [test]"
test = Two(test)
src << "Test 2: [test]" // Outputs the new value.

Two(test)
return (test + 5) // Sends the value of test plus 5 back to the caller.


Now keep in mind that when passing things like mobs and other reference-based item you won't have this limitation.
Primitive types are passed by value, not reference. What this means is that any variables modified within a procedure - unless they are objects (excluding strings) - will not have any effect on the variable passed into the procedure. If you want to communicate with the calling procedure, use a return statement, like so:

var/counter = 0
proc
counting()
counter = counter + 1
return counter

mob/verb/count()
var/N = counting()
usr << "This verb has been clicked [N] times"
In response to Garthor
So only num and text have this limitation? I tried the same example on lists and it seemed to have the expected results.

proc
changesuper2(list/superlist)
superlist.Add(34)
superlist[1] = 12
mob/verb
testproc2()
set category = "TEST"
var/list/superlist = list(3)

usr << "AFTER:"
for(var/X in superlist)
usr << X

changesuper2(superlist)

usr << "BEFORE:"
for(var/X in superlist)
usr << X


The reason why using return might be restrictive because I can have multiple arguments in a proc where the final values of each is dependent on the others.
In response to Sandlight
thats something completely different thats not what garthor meant. In that proc your changing the variable directly.

To do that with your supper proc it wouldn't look like that but like this

mob/verb
testproc()
set category = "TEST"
var/super = 0
usr << "BEFORE: [super]"
usr.changesuper()
usr << "AFTER: [super]"
proc
changesuper()
super = 20


but if you want this proc to work with any number you would have to do this like what garthor said.

return is basically this
d = change(d)
change(d) = d+5

proc
change(a)
a+5
return a

that proc can be translated into : change(a) = a+5

im not sure how old you are but if you've ever seen algebra you would understand
In response to Masschaos100
Masschaos100 wrote:
return is basically this
> d = change(d)
> change(d) = d+5
>
> proc
> change(a)
> a+5
> return a
>

that proc can be translated into : change(a) = a+5

The first line there is proper syntax and is fine.
The second line there is not proper syntax, though I'm not sure if you were intending that to be descriptive or whatever (it isn't).
How you wrote that proc is proper syntax but does nothing as the result of the operation a+5 is not stored anywhere.


im not sure how old you are but if you've ever seen algebra you would understand

Irony.