Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function declaration inside of function — why?

Tags:

I am reading the book "Programming in C" and found in Chapter 10 an example like this:

#include <stdio.h>

void test (int  *int_pointer)
{
     *int_pointer = 100;
}

int main (void)
{
     void test (int  *int_pointer);
     int  i = 50, *p = &i;

     printf ("Before the call to test i = %i\n", i);

     test (p);
     printf ("After the call to test i = %i\n", i);

     return 0;
}

I understand the example, but I don't understand the line void test (int *int_pointer); inside of main. Why do I define the signature of test again? Is that idiomatic C?

like image 643
Max Avatar asked Apr 20 '15 11:04

Max


People also ask

Can you declare a function inside a function?

We can declare a function inside a function, but it's not a nested function. Because nested functions definitions can not access local variables of the surrounding blocks, they can access only global variables of the containing module.

What are declared inside a function?

A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.

Why function declaration is placed prior to function definition?

The reason modern compilers give warnings on an attempt to call a function before seeing a declaration is that a declaration allows the compiler to check if arguments are of the expected type.

Why function declaration is necessary?

Function declarations are important because a calling convention (part of the ABI of a platform) can define different places for arguments and return values based on the types a function returns and accepts as arguments.

What is function declaration in C programming?

Declaration: the function's name, return type, and parameters (if any) For code optimization, it is recommended to separate the declaration and the definition of the function. You will often see C programs that have function declaration above main (), and function definition below main ().

Is it necessary to declare a function above the main function?

If a function is defined above the main function, there is no need of a separate declaration of function. However, if the function is defined below the main function, it is a good programming practice to declare the functions being used above the main.

What is the difference between function declarations and function expressions?

Function declarations are hoisted but function expressions are not. It’s easy to understand with an example: The above does not throw an error, but this would: It might seem like function declarations, with their powerful hoisting properties, are going to edge out function expressions for usefulness.

What should be included in the declaration of a function?

However, note that each declaration must be consistent with its definition with regard to the number of parameters, their types and the return type. A function declaration provides valuable information (function name, number and type of parameters and return type) to the compiler.


2 Answers

It's definitely not idiomatic C, despite being fully valid (multiple declarations are okay, multiple definitions are not). It's unnecessary, so the code will still work perfectly without it.

If at all, perhaps the author meant to do

void test (int *int_pointer);  int main (void) {      ...  } 

in case the function definition was put after main ().

like image 94
haneefmubarak Avatar answered Oct 31 '22 11:10

haneefmubarak


void test (int *int_pointer); is just a declaration (or prototype) of function test. No need of this declaration in main because you already have function definition before main.

If the definition of test were after main then it would be worth of putting its declaration there to let the compiler know about the return type, number of arguments and arguments types of test before calling it.

like image 45
haccks Avatar answered Oct 31 '22 10:10

haccks