35

I've seen a few function definitions like this recently while playing with GNU Bison:

static VALUE
ripper_pos(self)
    VALUE self;
{
    //code here
}

Why is the type of self outside of the parenthesis? Is this valid C?

Tom Dalling
  • 23,305
  • 6
  • 62
  • 80

6 Answers6

39

Those are old K&R style function parameter declarations, declaring the types of the parameters separately:

int func(a, b, c)
   int a;
   int b;
   int c;
{
  return a + b + c;
}

This is the same as the more modern way to declare function parameters:

int func(int a, int b, int c)
{
  return a + b + c;
}

The "new style" declarations are basically universally preferred.

pat
  • 12,587
  • 1
  • 23
  • 52
sth
  • 222,467
  • 53
  • 283
  • 367
  • 8
    If you omitted the type definition for any parameter, that parameter would be assumed to be int. Also, if you omitted the return type, it would be assumed to be int. e.g: func(a,b,c) { return a+b+c; } – Ferruccio Jun 10 '10 at 16:23
  • 4
    Just FWIW, K&R style is still sometimes used for code golf... – Jerry Coffin Jun 10 '10 at 16:29
9

This is the so-called "old" variant of declaring function arguments. In ye olden days, you couldn't just write argument types inside the parentheses, but you had to define it for each argument right after the closing parenthesis.

In other words, it is equivalent to ripper_pos( VALUE self )

Fyodor Soikin
  • 78,590
  • 9
  • 125
  • 172
4

Yes, it uses an older style of function definition in which the parameters, sans type, are listed in parentheses, followed by the declaration of those variables with their types before the opening brace of the function body. So self is of type VALUE.

mipadi
  • 398,885
  • 90
  • 523
  • 479
3

This is old c. K&R C used this convention, before ANSI C enforced typed parameters.

static VALUE  // A static function that returns 'VALUE' type.
ripper_pos(self)  // Function 'ripper_pos' takes a parameter named 'self'.
    VALUE self;   // The 'self' parameter is of type 'VALUE'.
Stephen
  • 47,994
  • 7
  • 61
  • 70
2

That's the old-style C function declaration syntax.

David Gladfelter
  • 4,175
  • 2
  • 25
  • 25
2

It is really old C code, where you first specify the argument names, and then their types. See for example here.

axel_c
  • 6,557
  • 2
  • 28
  • 41