Results 1 to 5 of 5

Thread: function returned value

  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
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    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]
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  3. #3

    Re: function returned value

    .

  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
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Re: function returned value

    Glad they helped Sandy.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

Similar Threads

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

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

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