Default arguments

This is a discussion on Default arguments within the C Programming forums, part of the General Programming Boards category; Hi, I really cant see what I have done wrong here. I have never used default arguments before and I ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    Default arguments

    Hi, I really cant see what I have done wrong here. I have never used default arguments before and I am wondering what I am doing wrong in this program.

    My C compiler is complaining that my function prototype is wrong. Here is the error:

    ERR1: conflicting types for 'volume'
    ERR2:"an argument type that has a default promotion can't match an empty parameter name list declaration"

    To be fair I have never seen that error message before and I am confused by the meaning of it.

    Here is the code in question:

    Code:
    #include <stdio.h>
    
    /*function prototype*/
    float volume ( float height = 10.12, float width = 10.12, float length = 10.12 );
    
    /*main function - begins program execution -----------------------------------*/
    int main ( void )
    {
       printf("Default volume is %.2f", volume());
       printf("\nAltered height rest default volume is: %.2f", volume ( 12.10 ));
       printf("\nAltered height and width volume is: %.2f", volume ( 12.10, 14.20 ));
       printf("\nAll default values altered volume is: %.2f", volume ( 12.10, 14.20, 16.11));
    
       getchar(); /*freeze console output window*/
    
       return 0; /*return value from int main*/
    }
    
    /*function to calulate volume of cube*/
    float volume ( float h, float w, float l )
    {
       return h * w * l;
    }
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    C allows default arguments?

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Ah, perhaps that is why, is this one those little differences from C and C++? I know they CAN be used in C++, but I wonder if you are correct that C does not allow it?
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    It was a shock for me too.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    Firstly, the code is C++. If your compiler is recognising default arguments, it is a C++ compiler.

    Second, the reason for the error message is that volume() is a function that accepts three float arguments, and returns float. However, the default values you are supplying for those arguments (and, also, the values you pass when you use volume() within main()) are of type double. The easiest fix would be to replace all usages of the keyword "float" with "double" in your code.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    1
    C doesn't allows default arguments

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utilizing another compiled program for a task.
    By kotoroshinoto in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 01:43 PM
  2. default Arguments
    By kishore in forum C++ Programming
    Replies: 2
    Last Post: 01-06-2007, 01:17 AM
  3. Default Arguments Question...
    By tinkerbell20 in forum C++ Programming
    Replies: 4
    Last Post: 07-07-2005, 05:27 PM
  4. Default Arguments
    By pianorain in forum C++ Programming
    Replies: 9
    Last Post: 12-20-2002, 07:40 AM
  5. pointers as default arguments
    By ajm in forum C++ Programming
    Replies: 3
    Last Post: 07-09-2002, 03:26 PM

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