Thread: returning values from a function...

  1. #1
    Registered User
    Join Date
    Jun 2009
    US of A

    returning values from a function...

    I am unable to understand what does the author mean by saying :

    The “C” language approach: values are returned through additional reference arguments in the method. This is with reference to error handling.

    Does he imply that we can return values by the arguments that we pass to a function. How does this work out in practice ?
    Last edited by roaan; 08-03-2009 at 06:36 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    functions can only "return" 1 value (i.e. 1 int, or 1 pointer, or 1 struct, etc, but only one of them). if you pass arguments by reference, then the function can modify those arguments, and they are actually modifying the variable in the calling function.

    for example, say you have a function that reads a file and counts the number of vowels in the file. the function may be written to return a boolean/int "0" or "1". "0" if some something IO failed, "1" otherwise. the function may accept an argument "int" by reference, which is where it stores the number of vowels read from the file. the caller calls this function, and checks the return value. if 0, print IO error and exit. if 1, check the 'number of vowels' variable that was passed by reference (and this variable was defined in the calling function). since the function modified this variable, because of pass by reference, we can then inspect this value.

    basically its used if you want to "return more than one" value, or if you want to allow the function to modify your variable for whatever reason.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Returning Multiple Values from a function
    By Eddie K in forum C Programming
    Replies: 6
    Last Post: 03-12-2006, 01:18 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Replies: 1
    Last Post: 02-03-2005, 03:33 AM