Thread: Functions and Returning Values

  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, 09:33 AM
  2. Returning values from functions
    By g001 in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2007, 03:42 AM
  3. returning multiple values from functions?
    By jamesn56 in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2005, 01:10 PM
  4. Replies: 5
    Last Post: 07-11-2004, 11:42 AM
  5. Functions returning char arrays
    By turmoil in forum C Programming
    Replies: 3
    Last Post: 05-27-2003, 01:43 AM