Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a function declaration within another function compile and what does it do?

Tags:

I intended to call a private class member function, but by a copy&paste mistake pasted the line as this function is declared in the header file:

void DebugView::on_cbYAxisEnabled_stateChanged(int) {     void updateAxisEnabled(); } 

instead of

void DebugView::on_cbYAxisEnabled_stateChanged(int) {     updateAxisEnabled(); } 

Surprisingly, the code was compiled and executed. However the method updateAxisEnabled() was not executed.

So, why does it compile? Was here a local function declared within a method body or has void instructed the compiler to ignore whatever comes afterwards?

The compiler is Visual Studio 2008.

P.S.: I'm aware of class declaration/definition within functions, but not functions within functions in C++.

like image 254
Valentin H Avatar asked Oct 13 '15 12:10

Valentin H


People also ask

What is the purpose of a function declaration?

A function declaration introduces an identifier that designates a function and, optionally, specifies the types of the function parameters (the prototype). Function declarations (unlike definitions) may appear at block scope as well as file scope.

Can a function be declared within 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.

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.

Can a function definition appear inside the body of another function definition?

Yes we can define a function in other function. I have compiled below written lines in gcc and it ran successfully without showing an error. #include<stdio.h>; void main() { int sum() { int a=30, b=10, c=20, sum=0; sum=a+b+c; return sum; } int a; a=sum(); printf("Sum = %d", a); }


2 Answers

void updateAxisEnabled(); is a function declaration.

Sample:

#include <cstdio>  void a(); void b();  int main(void) {     a();     b();     return 0; }  void a() {     void c(); // Declaration     c(); // Call it }  void b() {     c(); // Error: not declared }  void c() {     puts("Hello, world!"); } 
like image 127
MikeCAT Avatar answered Sep 22 '22 22:09

MikeCAT


It is perfectly allowed to declare a function inside a function scope: a function may be declared in any scope.

A common mistake among C++ programmers is indeed to:

void foo() {     MyObject bar(); // 1     bar.someMethod(); // 2 } 

this will miserably fail to compile because line 1 is not declaring a MyObject named bar and calling its constructor explicitly; rather, it is declaring a function named bar that returns a MyObject. Thefore, there is really no object to call someMethod on.

like image 44
edmz Avatar answered Sep 18 '22 22:09

edmz