By Tom Dalling


2010-06-10 16:10:05 8 Comments

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?

6 comments

@sth 2010-06-10 16:15:16

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.

@Ferruccio 2010-06-10 16:23:12

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; }

@Jerry Coffin 2010-06-10 16:29:03

Just FWIW, K&R style is still sometimes used for code golf...

@Stephen 2010-06-10 16:16:18

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'.

@axel_c 2010-06-10 16:14:30

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

@David Gladfelter 2010-06-10 16:14:25

@Fyodor Soikin 2010-06-10 16:13:59

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 )

@mipadi 2010-06-10 16:13:32

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.

Related Questions

Sponsored Content

22 Answered Questions

[SOLVED] Set a default parameter value for a JavaScript function

37 Answered Questions

[SOLVED] var functionName = function() {} vs function functionName() {}

32 Answered Questions

[SOLVED] What exactly is RESTful programming?

  • 2009-03-22 14:45:39
  • hasen
  • 1599654 View
  • 3839 Score
  • 32 Answer
  • Tags:   http rest definition

24 Answered Questions

[SOLVED] What is the scope of variables in JavaScript?

10 Answered Questions

[SOLVED] What does the exclamation mark do before the function?

  • 2010-09-20 21:21:51
  • Sebastian Otto
  • 144826 View
  • 1140 Score
  • 10 Answer
  • Tags:   javascript function

18 Answered Questions

[SOLVED] What should main() return in C and C++?

18 Answered Questions

[SOLVED] What does "static" mean in C?

  • 2009-02-21 06:47:52
  • David
  • 807039 View
  • 978 Score
  • 18 Answer
  • Tags:   c syntax static

20 Answered Questions

[SOLVED] What is the difference between call and apply?

22 Answered Questions

Sponsored Content