ID:937477
 
(See the best response by Kaiochao.)
I didn't begin coding yesterday. I have only 6-9 months of coding experience. But I never learned this. I'm trying to learn some simple procs but I don't fully understand this.

mob/proc/test(a,b,c)
a+=b
b+=c
a+=c
world << "a=[a]"
mob/verb/testverb(a as num,b as num,c as num)
world << test(a,b,c)


How does passing the arguments here work? Does the proc test define new variables a,b,c? Does testverb define the new variables and then pass them onto world << test(a,b,c)? How does world << test(a,b,c) pick up the test proc arguments? Do the letters have of testverb have to match test?

What is this kind of coding being called?

By the way what does return 1 and return 0 mean? How do they work?
return 1 and return 0 are used as return TRUE and return FALSE; they're un-needed unless you're trying to check something with if() You can also use return [variable] to set a variable to something inside the proc.
proc/test(a,b,c)
a+=b+c
return a
var/d=test(1,2,3)


In the coding you provided, testverb calls test(a,b,c) then outputs the results, which is nothing. You'd have to return a value inside the proc to have it output anything, otherwise it's null.
Can you give me an example of when to use return TRUE/1 and return FALSE/0? How does it work with an if statement? What do you mean set a variable when you "return variable"?

In my coding I have to add return a in order for it to work?
In response to TheDarkChakra (#2)
When you call a proc, it is replaced by its return value. For example, when you set a variable to a proc that returns 4, that variable is now equal to 4. When you use switch() on input(), the switch() doesn't care that there's an input inside. It only sees the return value of input, which is the value the player responded with.
In response to TheDarkChakra (#2)
proc/example(a)//Useless, you could simply use if([value]) or if(![value])
if(a) return TRUE//checks if a exists, returns TRUE if so
else return FALSE//otherwise, return FALSE

if(example(1))//if 1 exists(will always be TRUE)
//action
else//if 1 returns FALSE
//action

and then

proc/AddNums(a as num, b as num)//This is just an example and is completely useless.
a+=b
return a//returns the new value of a, which is a and b added together.
gold=AddNums(gold, gain)//sets total gold to current gold plus gold gained.

Might I suggest looking in the DM guide some more? Perhaps some more tutorials?
In response to TheDarkChakra (#2)
TheDarkChakra wrote:
Can you give me an example of when to use return TRUE/1 and return FALSE/0? How does it work with an if statement? What do you mean set a variable when you "return variable"?

In my coding I have to add return a in order for it to work?

Example :D

proc/test(...)
for(var/i in args)
.+=i
return .

mob/verb/Test()
world<<test(5,5,5,5)//20
world<<test(1,2,3,4,5,6,7,8)//36


You can have as many vars as you'd like ^^.
For some reason this is hard for me to understand
In response to TheDarkChakra (#6)
TheDarkChakra wrote:
For some reason this is hard for me to understand

proc/Test(...)
for(var/i in args)//checks inside of args[] (...)
.+=i// . is the DEFAULT RETURN VALUE , so just add to it.
return . //returns the value.

mob/verb/Test()//name of verb/action
world<<test(5,5)//world = outputs msg to world. 5 & 5 are both Args in the proc.
Is ... An operator or a name of a variable and can be anything?
In response to TheDarkChakra (#8)
TheDarkChakra wrote:
Is ... An operator or a name of a variable and can be anything?

It's an operator[?].

world<<test(5,5)
//This will output a NUMBER of 10.

world<<test("Hello, ","How are you?")
//This will output "Hello, How are you?"

world<<test("Hello",5)
//This will cause an error, you can't mix numbers and text.

world<<test(length("Hello"),5)
//Unless you turn the string of text into a number field.
You don't need to do ... to use the args list. args is a list defined in every proc that contains every value sent to that proc.
In response to Albro1 (#10)
Albro1 wrote:
You don't need to do ... to use the args list. args is a list defined in every proc that contains every value sent to that proc.

Really? I didn't know.
Ok i get return true but still am trying to figure out the procs
In response to TheDarkChakra (#12)
TheDarkChakra wrote:
Ok i get return true but still am trying to figure out the procs

Trial & error my friend. :)
Procs are very simple, Dark. They are processes, and every one has a return value. The return value is stored in ., like so:

proc/test()
. = "return value"
// Does the same thing as
proc/test()
return "return value"


If the proc reaches the end without hitting a return statement, it automatically returns what is stored in ..

You can use them to assign values to variables, like so:
var
a = 1
b = 2
c = add(a, b) // will be 3
proc/add(a = 0, b = 0)
return a + b


Or you can just use them to do a task, effectively splitting up your code into more manageable parts.
client/proc/start()
task_one()
task_two()
client/proc/task_one()
winset(src, "default", "is-maximized = true")
client/proc/task_two()
world << "done"


Remember that since they always have a return value, you can use them in conditional statements too!
mob/proc/do_something()
if(src.lazy)
return 0
else
return 1
mob/proc/execute()
if(do_something())
src << "You are not lazy."
else
src << "You are lazy."


I think that's a long enough post for now. I hope you learned something!
Best response
I think the thing to keep in mind is, DM only works with numbers, text, files, paths, and objects. Variables and procedures are only ever read as the most basic value they're associated to. DM doesn't care what you call things. 1 is 1, "blah" is "blah". If you set a variable "poop" to 1, DM only ever sees it as 1, not "poop". Similarly, when a proc returns 1, DM only sees it as 1 in the code before using it.
I think I'm beginning to understand this.

But can someone help me interpret this:

proc/CheckJump(obj/piece/jumper, obj/piece/jumped) 
for(var/obj/piece/p in get_step(jumped, get_dir(jumper,jumped)))
return 0
return 1

proc/CheckReqMoves(obj/piece/selected)
for(var/obj/piece/P in oview(selected, 1))
if(selected.type==P.type) continue
if(CheckJump(selected, P)) return 1


How do the arguments like (obj/piece/selected) and (obj/piece/jumper, obj/piece/jumped) act like what they are named(selected,jumper,jumped etc...)?
In response to TheDarkChakra (#16)
Again, the names of variables and procedures that you make have absolutely no effect on what value they refer to. When you call those procs, you're expected to give them pieces in their arguments, which are then referred to inside the procedure.

Arguments are just variables that you can set when you call the proc. Of course, you can also set them differently inside the proc, but it's more important to know the first part.