Thread: is this required in a function body?

  1. #1
    Unregistered
    Guest

    is this required in a function body?

    I know this is legal in the body of int main()

    if( test == 1){
    ++count;
    }

    my question is the {,} required in a function body of the if statement, because in my book's example all I see is this:

    double test(double a, double b)
    {

    double test, sum;

    //is {,} required for this one?
    if(test ==2)

    a = 2.0;
    b = 2.0;
    sum = a + b;

    return(sum);
    }

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    You need { and } only if your statements are more than one line.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    154

    Re: is this required in a function body?

    Originally posted by Unregistered
    I know this is legal in the body of int main()

    if( test == 1){
    ++count;
    }

    my question is the {,} required in a function body of the if statement, because in my book's example all I see is this:

    double test(double a, double b)
    {

    double test, sum;

    //is {,} required for this one?
    if(test ==2)

    a = 2.0;
    b = 2.0;
    sum = a + b;

    return(sum);
    }
    Assuming a, b, and sum are what you want to do if test equals 2, yes. Also, you don't set test to any value. sum should also be set prior to the if test in case test != 2. You're returning sum in either case. You could put that return in the if block {}, but would still need to return something if the if block wasn't executed. Also, the syntax is: return sum; not return (sum);
    Last edited by Brown Drake; 09-26-2001 at 03:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM