Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we declare functions inside functions?

Tags:

c

function

#include <stdio.h>

int main()
{
   void foo();
   printf("1 ");
   foo();
}

void foo()
{
    printf("2 ");
}

Output:

1 2

How declaring functions inside functions work? Does it mean that foo() function can only be called by main()?

like image 706
Pankaj Mahato Avatar asked Jan 30 '14 17:01

Pankaj Mahato


People also ask

Can you put functions inside of functions in C?

No you can't have a nested function in C . The closest you can come is to declare a function inside the definition of another function. The definition of that function has to appear outside of any other function body, though.

Can a function have another function inside?

A function defined inside another function is called a nested function. Nested functions can access variables of the enclosing scope. In Python, these non-local variables are read-only by default and we must declare them explicitly as non-local (using nonlocal keyword) in order to modify them.

Can you declare a function inside a function in JavaScript?

A function is called “nested” when it is created inside another function. It is easily possible to do this with JavaScript.


1 Answers

Yes, you can declare, but you cannot define. Also, you can declare function as many times you want, but define only once.

like image 151
Pranit Kothari Avatar answered Oct 07 '22 21:10

Pranit Kothari