PDA

View Full Version : function returned value



sandyrepope
07-12-2007, 03:54
I'm trying to understand about function parameters and keep getting confused when I read the help.

What's giving me the most trouble right now is having the function return a value and then how my script can use the value. I have an idea that I should use something like 'function = value' but what I don't understand is where does the value go and how does my script use the returned value.

I don't understand this enough to work up an example script right now.

Thanks
Sandy

kryton9
07-12-2007, 08:22
Sandy hope this helps:
Basically the answer coming back from the function can be assigned to a variable of the same type. In this case a quad.
The functions returned value can also be used directly without assigning it to a variable as in the second and third example.


uses "console"

Dim Num as Long ' create a number variable
Dim Answer as Quad ' create another number variable
Num = 5
Answer = GetSquare(Num) 'this is where we use and get the answer back from our function

Console_WriteLine ( "The answer of 5 squared is: "+Str$(answer) ) 'this displays the answer on the console screen
Console_WriteLine ( "") 'just prints a blank line
Console_WriteLine ( "Press any key to exit" ) 'tells us how to exit from the program
Console_Read() 'waits for input so the user has time to read the screen

'our function is here.
'this function gets a number value of long type and returns back an answer of quad type
Function GetSquare(N as Long) as quad
Function = N * N 'this line does what the function is supposed to do and also returns the answer back in one line
End Function

The above could also have been done this way:

uses "console"

Console_WriteLine ( "The answer of 5 squared is: "+Str$(GetSquare(5)) )
Console_WriteLine ( "")
Console_WriteLine ( "Press any key to exit" )
Console_Read()

Function GetSquare(N as Long) as quad
local answer as quad
answer = N * N
Function = answer
End Function

Or this way:

uses "console"

Console_WriteLine ( "The answer of 5 squared is: "+Str$(GetSquare(5)) )
Console_WriteLine ( "")
Console_WriteLine ( "Press any key to exit" )
Console_Read()

Function GetSquare(N as Long) as quad
Function = N * N
End Function

Michael Hartlef
07-12-2007, 08:44
.

sandyrepope
07-12-2007, 16:35
Kryton9 - Yes! I've got it now! Your examples make it so easy to understand. I've got it.

Thank you very much.
Sandy

kryton9
07-12-2007, 22:54
Glad they helped Sandy.