Functions and Returning Values

This is a discussion on Functions and Returning Values within the C++ Programming forums, part of the General Programming Boards category; When a function returns a value, how does the function expression take on the value returned since it's not a ...

  1. #1
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256

    Functions and Returning Values

    When a function returns a value, how does the function expression take on the value returned since it's not a variable? For example, say you have the function sqrt() that finds the square root of a number and you implement it like so:

    answer = sqrt(number);

    how does sqrt() hold the returned value?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    implentation specific.
    A common way is to put the value into a specified location (be it a register or memory location whatever) which the caller function looks at once the callee function is completed.

  3. #3
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    ?
    So does the function return the memory or register address to the the calling function?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by Thantos
    implentation specific
    Now maybe if you told us what os/compiler you were using we could give more details

  5. #5
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    I just wanted to know generally how it was accomplished, and you explained that. thank you. I"m not looking for detailed step by step operation, but thank's for offering.
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok assuming an x86 environment:
    The return value is placed in register EAX, whatever function called it looks at that register for the return value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unkown Hang
    By Bladactania in forum C Programming
    Replies: 31
    Last Post: 04-22-2009, 10:33 AM
  2. Returning values from functions
    By g001 in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2007, 04:42 AM
  3. returning multiple values from functions?
    By jamesn56 in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2005, 02:10 PM
  4. Replies: 5
    Last Post: 07-11-2004, 12:42 PM
  5. Functions returning char arrays
    By turmoil in forum C Programming
    Replies: 3
    Last Post: 05-27-2003, 02:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21