What does the return do in a function?

D.V.D

Make a wish
Reaction score
73
Maybe the title isn't so good but the question is: When you have a function that takers lets say real. What does the return do? Do you return the struct you used for the variables? Also, what are methods and what are static methods? I think that they are the action/condition/event you are creating but what's the difference?
 

Xorifelse

I'd love to elaborate about discussions...........
Reaction score
87
Code:
function sum takes integer [COLOR="SeaGreen"]a[/COLOR], integer [COLOR="SeaGreen"]b[/COLOR] returns [COLOR="Red"]integer[/COLOR]
    return a + b
endfunction

function someFunc takes noting returns nothing
    local [COLOR="Red"]integer[/COLOR] i = sum( [COLOR="SeaGreen"]4[/COLOR], [COLOR="SeaGreen"]5[/COLOR] ) // i holds the value 9.
endfunction

In vJass, a static is a normal global, or a non object orientated function.
Methods are object orientated functions, it parses an extra integer with it called "this", which holds the current struct ID.
But I suggest learning some proper Jass before you switch over to vJass, get a hold of the syntax then someday use structs.
I hope that explains it..
 

D.V.D

Make a wish
Reaction score
73
Explained a bit. I still don't get what exactly does the return mean? So what you return a integer, what does it do if it does?
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
Explained a bit. I still don't get what exactly does the return mean? So what you return a integer, what does it do if it does?

It stops the function completely, and 'returns' the value to the function that called it, so that function can then use the returned value. For example, you can set a variable to the function like this:

JASS:
function GimmeUnit takes argument <name>, argument <name>, argument <name> returns unit
    return udg_ThatOneGuy
endfunction

local unit u = GimmeUnit(argument,argument,argument)


or you can simply call the function, like this:

JASS:
call GimmeUnit(argument,argument,argument)


The declaration line,

JASS:
function NAME takes ARG name, ARG name, ARG name returns VARIABLE TYPE


Specifies how the function can be used: The function's name, How many arguments it has passed to it when it is called, the type and names of those arguments (to be used inside the function), and a variable type that the function will return.

if a function "returns nothing" that means that the function can only be called, not used. For these types of functions, you can use the "return" line to "Skip Remaining Actions"

JASS:
call DoStuff()
 

Ryuu

I am back with Chocolate (:
Reaction score
64
Basically, a return is a return.
What am I talking about, let me show you an example.

Let's say you made a function called Something.

JASS:
function Something takes string s returns integer
    return 12
endfunction


Once you had this function, the following function:

JASS:
set Integer = Something("abcd")


becomes

JASS:
set Integer = 12


Because that function returns '12'.
 

GooS

Azrael
Reaction score
154
The function called (The returning function) stops and the function in which it has been called has a variable based on the conditions and/actions made in the returning function.
(Explained above me)

I'm a newb in jass so i don't really know what it's usefull for beyond the intitial condition in a trigger:

JASS:
call TriggerAddCondition( gg_trg_IRFunc, Condition( function Trig_IRCond ) )

As the only time I go beyond the initial action function is when I use callbacks such as timers.
 

cr4xzZz

Also known as azwraith_ftL.
Reaction score
51
Basically, to sum it up, the return part makes the function return a value - be it integer, boolean, real, whatever. The value can be accessed later if you saved it in a variable (as the examples above show).
 

SerraAvenger

Cuz I can
Reaction score
234
Code:
function sum takes integer [COLOR="SeaGreen"]a[/COLOR], integer [COLOR="SeaGreen"]b[/COLOR] returns [COLOR="Red"]integer[/COLOR]
    return a + b
endfunction

function someFunc takes noting returns nothing
    local [COLOR="Red"]integer[/COLOR] i = sum( [COLOR="SeaGreen"]4[/COLOR], [COLOR="SeaGreen"]5[/COLOR] ) // i holds the value 9.
endfunction

In vJass, a static is a normal global, or a non object orientated function.
Methods are object orientated functions, it parses an extra integer with it called "this", which holds the current struct ID.
But I suggest learning some proper Jass before you switch over to vJass, get a hold of the syntax then someday use structs.
I hope that explains it..

Well... I wouldn't say it is not OOP.
A static method in vJASS, to be precise, is a method that takes no struct object as parameter. It just takes the class.

For example:
JASS:
struct A
  static method test takes nothing returns integer
    return 0
  endmethod
endstruct

struct B
  static method test takes nothing returns integer
    return .allocate() // This is a static method, so it can use private methods and variables. allocate is a static method too, hence you don't need the 'B' infront of it.
  endmethod
endstruct

now
JASS:

local integer testA = A.test() // The 'return' statement makes this method return 0, so testA is 0 now
local B testB = B.test() // testB now holds a newly created instance of the 'B' class.


Return statements jump back to the last address of the Virtual Machine and hand out their value.

In a pseudocode imperative language, you could express it like that:
JASS:

109  // 109 Lines of other code before this...
110  // This is a 'function' that returns the sum of two arguments. As this is not a procedural language, there are no functions actually.
111  RETURN_STATEMENTS := RETURN_STATEMENTS + 1
112  
113  RESULT := ARGUMENT[ 0 ] + ARGUMENT[ 1 ]
114  
115  RETURN_STATEMENTS := RETURN_STATEMENTS - 1
116  GOTO LAST_LINE[ RETURN_STATEMENTS ]
117
118 // A few lines in between...
.
.
.
573 ARGUMENT[ 0 ] := 1
574 ARGUMENT[ 1 ] := 4
575 LAST_LINE[ RETURN_STATEMENTS ] := 577 // The current line + 2!
576 GOTO 111
577 ? "%d" % RESULT  // Prints 5
 
General chit-chat
Help Users

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top