Thread: valid function prototypes?

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    5

    valid function prototypes?

    I was asked by a friend about validity of following function prototypes,

    void func1(int = 0, int*);
    void func2(int = 1, int& = 2);
    void func3(int*, int& = 3);
    void func4(int& = 4, int* = 0);
    void func5(int& = 0, int = 1);
    void func6(int = 5, int& = 6, int* = 0);

    I think the only prototype that is invalid is func1 because it does not have default parameter on the far right.
    I am not sure if I am right, though.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Default parameters are places one the right far..
    Next time post your code in [code]/*your code*.[/code]
    Welcome to the forum
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    5
    Quote Originally Posted by std10093 View Post
    Default parameters are places one the right far..
    Next time post your code in [code]/*your code*.[/code]
    Welcome to the forum
    thank you for the feedback, I'll definitely post code using that next time.
    So I am right about func1 being invalid, but how about the rest?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    You could have just pasted them into a compiler, and found the answer a few seconds later.
    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.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    5
    The compiler shows following error,
    error: default argument for ‘int& <anonymous>’ has type ‘int’|

    Could you explain to me what does int& <anonymous> means?

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I think that <anonymous> means the identifier for the parameter is missing.
    This is a guess from a C programmer.

    Code:
    void func2(int = 1, int& = 2);
    With identifier for the parameters

    Code:
    void func2(int param1 = 1, int& param2 = 2);
    Tim S.
    Last edited by stahta01; 03-11-2013 at 04:53 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    5
    @ stahta01
    thanks I understand what <anonymous> means.

    error: default argument for ‘int& a’ has type ‘int’|, this is the error I get when I change the parameter name to "a".

    but I still am confused about why int & is not the same as int, for which I cannot assign an interal literal to a type int& variable.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    References must be constructed from existing objects.

    You could do something like:

    Code:
    int globalparam = 3;
    
    void foo (int& param = globalparam)
    {
      std::cout << param << "\n";
    }
    
    int main()
    {
      int x = 4;
      foo(x);
      foo();
    }

    Globalparam is guaranteed to exist before the function is invoked, so this is OK... on the other hand, globalparam is a global variable. It's scope has to be considered when you think about how reliable this function is.

    Only if the reference were const would you be allowed to do:

    void foo(const int& param = 3);

    Since integer literals are immutable.
    Last edited by whiteflags; 03-11-2013 at 08:36 PM.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Came C++11, the follow is also valid:

    void foo (int&& param = 1337)

    Since an rvalue reference allows you to bind temporaries (such as number literals) to references.
    && is called an rvalue reference if you want to look it up.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Function Prototypes
    By Valandu in forum C Programming
    Replies: 16
    Last Post: 10-14-2011, 10:11 PM
  2. function prototypes
    By kawaikx15 in forum C Programming
    Replies: 1
    Last Post: 06-28-2011, 09:20 PM
  3. Function Prototypes
    By askinne2 in forum C Programming
    Replies: 6
    Last Post: 10-01-2010, 01:01 PM
  4. Function prototypes
    By XxPKMNxX in forum C Programming
    Replies: 4
    Last Post: 10-28-2009, 02:33 AM
  5. 2 function prototypes
    By newbie101 in forum C++ Programming
    Replies: 3
    Last Post: 05-09-2006, 05:49 AM