Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does this function definition work?

I generated a hash function with gperf couple of days ago. What I saw for the hash function was alien to me. It was something like this (I don't remember the exact syntax) :

unsigned int
hash(str, size)
   register char* str;
   register unsigned int size;
{
   //Definition
}

Now, when I tried to compile with a C++ compiler (g++) it threw errors at me for not having str and size declared. But this compiled on the C compiler (gcc). So, questions:

  1. I thought C++ was a superset of C. If its so, this should compile with a C++ compiler as well right?
  2. How does the C compiler understand the definition? str and size are undeclared when they first appear.
  3. What is the purpose of declaring str and size after function signature but before function body rather than following the normal approach of doing it in either of the two places?
  4. How do I get this function to compile on g++ so I can use it in my C++ code? Or should I try generating C++ code from gperf? Is that possible?
like image 479
nakiya Avatar asked Nov 21 '10 04:11

nakiya


People also ask

What defines how function works?

A function is defined as a relation between a set of inputs having one output each. In simple words, a function is a relationship between inputs where each input is related to exactly one output. Every function has a domain and codomain or range.

What is the definition to function?

the kind of action or activity proper to a person, thing, or institution; the purpose for which something is designed or exists; role.

How does the Def function work in Python?

Python def keyword is used to define a function, it is placed before a function name that is provided by the user to create a user-defined function. In python, a function is a logical unit of code containing a sequence of statements indented under a name given using the “def” keyword.

How does a function definition begin and how does it end?

Function definitions are the first of several compound statements we will see, all of which have the same pattern: A header, which begins with a keyword and ends with a colon.


2 Answers

1.  C++ is not a superset, although this is not standard C either.

2/3. This is a K&R function declaration. See What are the major differences between ANSI C and K&R C? .

4. gperf does in fact have an option, -L, to specify the language. You can just use -L C++ to use C++.

like image 148
Matthew Flaschen Avatar answered Oct 13 '22 00:10

Matthew Flaschen


The Old C syntax for the declaration of a function's formal arguments is still supported by some compilers.

For example

int func (x) 
int x 
{

}

is old style (K&R style) syntax for defining a function.

I thought C++ was a superset of C. If its so, this should compile with a C++ compiler as well right?

Nopes! C++ is not a superset of C. This style(syntax) of function declaration/definition was once a part of C but has never been a part of C++. So it shouldn't compile with a C++ compiler.

like image 39
Prasoon Saurav Avatar answered Oct 13 '22 02:10

Prasoon Saurav