10

I'm pretty new to programming and can't really understand why I can't just declare argument types the same way I do with normal variables and have to declare the type again and again.

I mean, why must I:

Func(int a, int b, float c)

instead of

Func(int a, b, float c)

?

As long as they're the same type, of course.

  • Can I actually do that and just don't know how to?

If it is possible, please tell me how.

Thanks in advance.

@0x499602D2: If parameter declarations were more closely analagous to object declarations, then void f(int a, float c, d) would presumably be equivalent to void f(int a, float c, float d). The language could have made this work correctly and consistently. It just didn't. – Keith Thompson

This answered my question best. but it's a comment...

10
  • Functions can take different types. How can the compiler know what type you intended in this scenario: void f(int a, float c, d)? What if d is meant to be a double or a user-defined class?
    – David G
    Commented Nov 6, 2013 at 22:33
  • Relevant: Go read about the "K&R style" function declarations. C stopped supporting those years ago, C++ never has.
    – Ben Voigt
    Commented Nov 6, 2013 at 22:34
  • 1
    "Why" is a bit of a tricky question. The obvious answer is "because that's how the language is", but does that satisfy? It's a design decision the language creators made.
    – Kerrek SB
    Commented Nov 6, 2013 at 22:35
  • 2
    @0x499602D2: If parameter declarations were more closely analagous to object declarations, then void f(int a, float c, d) would presumably be equivalent to void f(int a, float c, float d). The language could have made this work correctly and consistently. It just didn't. Commented Nov 6, 2013 at 22:39
  • 2
    @KeithThompson imagine this: struct foo {}; void foobar( int a, foo ); what is a second parameter type?
    – Slava
    Commented Nov 7, 2013 at 0:30

5 Answers 5

7

This is why:

Everything has some rules or works on contracts. In theory you could write a C compiler that will instead of:

func(int a, int b)

take this:

func(int a, b)

that would be perfectly fine.

but

Creators of C decided that every single formal argument has to have its type attached to it hence we have it today. It's just a convention which you must follow.

And you must follow it as C/C++ parser is expecting you to do it this way otherwise it will not understand you.

Similarly your question:

Is there a way to declare multiple function arguments with one type?

may theoretically be written this way:

there multiple a way Is to declare function arguments with one type?

If you agree with someone to construct questions this way you must follow this contract - period.

1
  • 1
    I do not think that would be perfectly fine, it will at least conflict with parameter name omission in c++
    – Slava
    Commented Nov 7, 2013 at 19:20
3

That's how the syntax is. I don't know of there is a "reason", but in (old) C arguments without an explicit types would default to int (and there was even a syntax to provide types after the closing parenthesis), so I'm not sure this could be relaxed safely.

1
  • 2
    +1 A lot of the weirdness around function declarations comes from K&R C. Commented Nov 6, 2013 at 22:46
2

If you want b to have the same type as a (in case you later change type of a b should reflect that) in c++11 you can do this:

void Func( int a, decltype(a) b );

If you want to omit type at all, you cannot do that. Simple answer is: syntax does not support that. Why? There could be many answers, one of them:

void func( int, int ); // function declaration

what would we put there now?

void func( int, ); // function declaration? ugly and unreadable

Also it is error prone:

void func( int a, foobar ); // suppose this works
                            // now we add #include where foobar is defined as a struct
                            // function suddenly changes it's signature

I am pretty sure there would be even more bad side effects, so just believe, you do not want that.

2
  • I disagree about this method being prone to errors. You could say the same for variable declaration but you can declare THEM in succession. You will simply have to arrange your parameters so that all parameters of the same type will be where they should. Not very confusing at all. Commented Nov 7, 2013 at 18:53
  • @user2962533 put better example why it is error prone and why it is different than variable declaration
    – Slava
    Commented Nov 7, 2013 at 19:18
1

No, i don't think so.

In your function declarations, however, you can leave out the variable names.

Func(int a, int b);

can be

Func(int, int);

The compiler just needs enough information about the signature in order to be able to determine which function to call at runtime.

0

I realize that this isn't quite answering the question probably asked but it literally answers the asked question: Yes, it is possible to declare a function, say f, which takes N arguments of type T (and returns RC) without repeating T although using a funny notation:

same_type_function<void, 2, int> f;

Something similar can be done for member functions. Note, however, that I don't think that is also possible to define functions like this! Also, you'll obviously need a definition of same_type_function. Below is a quick example how this template can be implemented:

#include <iostream>

template <typename RC, int N, typename T, typename... A>
struct same_type_function_aux;

template <typename RC, typename T, typename... A>
struct same_type_function_aux<RC, 0, T, A...> {
    typedef RC (type)(A...);
};

template <typename RC, int N, typename T, typename... A>
struct same_type_function_aux {
    typedef typename same_type_function_aux<RC, N-1, T, T, A...>::type type;
};

template <typename RC, int N, typename T>
using same_type_function = typename same_type_function_aux<RC, N, T>::type;

same_type_function<void, 2, int> f;

int main()
{
    f(1, 2);
}

void f(int a, int b) {
    std::cout << "called f(" << a << ", " << b << ")\n";
}
2
  • Haha... I even mentioned I'm new to programming... I didn't get a thing of what did there. Looks smart, though. :) Commented Nov 7, 2013 at 18:49
  • 1
    @user2962533: The answer isn't to be taken too serious: I just showed what others claimed can't be done could, indeed, be done - sort of. If there is any take away for a beginner, it would be that you can declare a function or member function using a typedef which is something few people are aware of: typedef void (function_type)(int); function_type f; Normally that isn't too useful either, unless there are many functions which should all have the same type. Commented Nov 7, 2013 at 19:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.