emmeowzing
emmeowzing

Reputation: 2161

Why use function-declarations in C?

Why does C have function declarations?

Example from C in a Nutshell:

//circle.c: Calculate and print the areas of circles

#include <stdio.h>

double circularArea( double r );

int main()
{
    double radius = 1.0, area = 0.0;
    printf( "     Areas of Circle\n\n" );
    printf( "      radius         Area\n"
            "-------------------------\n" );

    area = circularArea( radius );
    printf( "%10.1f     %10.2f\n", radius, area );

    radius = 5.0;
    area = circularArea( radius );
    printf( "%10.1f     %10.2f\n", radius, area );

    return 0;
}

double circularArea( double r )
{
    const double pi = 3.14159265;
    return pi * r * r;
}

What is the purpose of typing "double circularArea( double r );" on the 5th line?

Upvotes: 2

Views: 193

Answers (4)

Barmar
Barmar

Reputation: 780889

The C compiler processes a source file from top to bottom. When it encounters a use of a function, it needs to know the arguments and return types of the function. You either have to declare or define the function before you use it, so it knows these types.

So you can either move the definition of curcularArea to before main(), or you can put a declaration before main().

A common style is to put declarations of all functions at the beginning of the file. Then you can define all your functions in any order, rather than having to worry about which calls which so you put all the dependencies first. Also, if you have mutually-recursive functions, no ordering of the definitions will work, you'll need a declaration of one of them.

Upvotes: 4

Deduplicator
Deduplicator

Reputation: 45654

C is inherently single-pass, which means you cannot use symbols (variables, macros, functions) before they are made known to the compiler.

Declarations make a symbol known to the compiler and usable in the translation unit (file) thence onward.

Those declarations are collected in header-files to avoid errors on re-writing them and to facilitate re-use and separate compilation.

Local symbols are introduced with declarations before the first point of use to allow recursive referencing between multiple symbols.
Those declarations are known as forward-declarations.

While one can use forward-declarations without need, an easier solution avoiding duplication is just moving the function-definition before the first point of use, because a definition is also a declaration.

Upvotes: 0

Matt Jacobi
Matt Jacobi

Reputation: 864

The compiler goes line by line. Without the declaration, when the compiler encountered the use of circularArea within the main() function, it would not know what that was an error. The declaration is basically a promise to the compiler that you will define a function with that signature later.

Upvotes: 0

Rizier123
Rizier123

Reputation: 59681

The Compiler goes from the top to the bottom! So if your calling a function in main which is at this time not declared it's undefined! So that this error don't appear you make a declaration at the top to say the compiler hey the function comes after the main, but it's there!

Upvotes: 2

Related Questions