Results 1 to 5 of 5

Thread: function returned value

Share/Bookmark
  1. #1

    function returned value

    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

  2. #2
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Clearwater, Florida, USA
    Posts
    3,802
    Rep Power
    391

    Re: function returned value

    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.

    [code=thinbasic]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[/code]

    The above could also have been done this way:
    [code=thinbasic]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[/code]

    Or this way:
    [code=thinbasic]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[/code]

  3. #3
    thinBasic MVPs Michael Hartlef's Avatar
    Join Date
    Sep 2006
    Location
    Leverkusen, Germany
    Age
    51
    Posts
    3,245
    Blog Entries
    2
    Rep Power
    335

    Re: function returned value

    .
    Running Windows 7 Home, 64 bit, 8 GB ram, Athlon II X2 255, ATI Radeon HD 4200.

  4. #4

    Re: function returned value

    Kryton9 - Yes! I've got it now! Your examples make it so easy to understand. I've got it.

    Thank you very much.
    Sandy

  5. #5
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Clearwater, Florida, USA
    Posts
    3,802
    Rep Power
    391

    Re: function returned value

    Glad they helped Sandy.

Similar Threads

  1. Functions as Returned Values
    By danbaron in forum Science
    Replies: 0
    Last Post: 27-05-2011, 08:47

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •