1

We've started looking at functions, and our homework requires us to include some simple ones in our programming. As we've left it a little late in the semester to explore functions, we're well-used to declaring/assigning multiple variables of the same type on a single line, as below:

int a, b, c = 0;

This, however, does not appear to work when integers a, b, and c are the parameters to be passed to a function:

int function(int a, int b, int c);

I was wondering if anybody could advise on the reasoning behind this. Is there something I'm misunderstanding, or is this a known convention?

Many thanks, I'm looking forward to any answers.

4
  • 1
    The first line is a convenience syntax that declares three integer variables. That is its only purpose. You shouldn't use this convenience syntax, by the way; always declare each variable on a separate line. Nov 21, 2019 at 1:58
  • "You shouldn't use this convenience syntax" - well, that's Robert's very personal opinion and debatable even amongst expert programmers. What you should probably not do is to write int a, b, c=0 instead of int a=0, b=0, c=0, since the first expression pretends to initialize a and b as well with zero, which it does not.
    – Doc Brown
    Nov 21, 2019 at 5:05
  • Every style is debatable. Those who would disagree with Robert on this style are welcome to debate it in any shop but mine. Nov 21, 2019 at 5:11
  • @DocBrown: The syntax has more practical implications (and is thus less commonly found) in C++ than in C, because it discourages you from narrowing every variable's scope as much as possible, something which is otherwise much easier to achieve in C++ than in C because you can declare variables everywhere. Nov 21, 2019 at 15:49

3 Answers 3

7

This is historically grown, and has its roots in the first edition of the K&R in 1978. It has to do with a meaninful order of arguments.

In early C, variables had to be declared at the beginning of a block, following the opening { and before any other statement in that block (p.81). Having several variables separated by a comma was then presented as convenient way for saving some space when no comment was needed (p.37). (This worry for space nowadays look like a joke, but on a 25 lines x 80 columns standard screen without mouse, it was very useful to be able to verify the variable declarations at the beginning of a function without scrolling too much. Nowadays, variables can be defined just before their usage, so it does not matter so much anymore.)

Function arguments at that time were declared in the argument-list without type, and the types did only follow the declaration of the function (p.67):

int foo(a,b)
int a;
char *s; 
{
     ...
}

It was possible at that time to regroup several arguments of a same type:

int bar(a,b,c)
int a,b,c;  
{...}

The only order that mattered was the order of the arguments in the argument list.

Forward declaration of functions and declaration of external functions did not provide the argument list but just (). This was very error prone.

So soon after (but not in the original k&R), function prototypes were introduced, to tell the types of the arguments. This looked like:

int foo(int, int, int); 

What mattered the most here was the order of the types and not the name of the argument. Note that nowadays, the name of the argument is still optional in prototypes (but not in the function declaration).

The standard committee did a great job in harmonizing the syntax, to come with the usage we nowadays know. And to be consistent between the declaration of the function and the definition of the function requires to have each argument separately introduced with its type.

So in the end, this evolution was driven by the order of the types for parameter passing.

0

The specific language would be helpful. Though i'm going to presume C.

The first example is a shorthand for int a; int b; int c = 0;.

Its a deliberate choice meant to make C look like some of the earlier languages to make it more familiar to those developers. Much like Java and C# look a lot like C++.

That also explains why it won't work in the function definition:

int function(int a, b, c = 0);          //This would expand to
int function(int a; int b; int c = 0);  //and how do you even parse that?
int function(int a, int b, int c);      //Its nowhere close to meaning this.
                                        // given that ; means end of statement.
3
  • Well, since you did pick C, "The comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type)." Nov 21, 2019 at 1:56
  • @RobertHarvey Except in the context of a variable declaration.
    – Kain0_0
    Nov 21, 2019 at 2:19
  • Yes, of course. Nov 21, 2019 at 2:52
0

Since you reference homework it is not clear the purpose of your assignment but I will take a guess. It looks like you are trying to differentiate how commas are used in two different contexts. First it is in how variables are declared and initialized. In the second case it has to do with how function parameters are declared. The C standard defines both of these cases in language definition. If you want to understand this better I suggest getting your hands on a copy of "The C Programming Language" by Brian Kernighan and Dennis Ritchie. There are several sections in there on this topic that you may find informative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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