Thread: Why function used in Return statement

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    Why function used in Return statement

    i have seen in some places of coding where a function is used in return statement. Why is it done? Any advantage?

    Example

    Code:
    int SubFunction1(void)
    {
     return subfunction2();
    }
    
    int Subfunction1(void)
    {
      int local;
      local = subfunction2();
      return local;
    }
    I hope it is only the style of coding. Am I correct?

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Static functions in C

    Static functions jump to mind - You may have a variable restricted to a certain file and need to use it as an argument

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I hope it is only the style of coding. Am I correct?
    Yes you are correct.

    Using a local variable is perhaps convenient if you need to examine a function return value in a debugger.
    Especially when you don't have the source for the called function.

    Any decent compiler will optimise away the local variable anyway.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Return statement is NOT exiting the function!!!
    By Vespasian in forum C++ Programming
    Replies: 43
    Last Post: 01-20-2014, 01:00 PM
  2. function exit with out return statement
    By Martin.Ericsson in forum C Programming
    Replies: 3
    Last Post: 11-09-2012, 06:47 AM
  3. Replies: 6
    Last Post: 07-14-2012, 09:26 AM
  4. Help with using the return statement
    By mkdl750 in forum C Programming
    Replies: 4
    Last Post: 07-23-2008, 10:14 AM
  5. function - return statement
    By chungt004 in forum C Programming
    Replies: 5
    Last Post: 02-27-2008, 08:33 PM

Tags for this Thread