C Programming - Functions - Discussion

1. 

A function cannot be defined inside another function

[A]. True
[B]. False

Answer: Option A

Explanation:

A function cannot be defined inside the another function, but a function can be called inside a another function.


Nitin said: (Aug 2, 2011)  
Explain me with example.

Ahmad Zia said: (Nov 16, 2011)  
If a function can't define in function then what is main() in C.

Pelle said: (Jan 19, 2012)  
Can you explain this?
The usage of functions inside functions works fine with my gcc compiler..


#include <stdio.h>

int main() {

int printFuzzAndOrBuzz(int i) {
int divisable = 0;
if (i % 3 == 0) {
// Divisable by 3, print Fizz
printf("Fizz");
divisable = 1;
}
if (i % 5 == 0) {
// Divisable by 5 (and/or 3)
printf("Buzz");
divisable = 1;
}

return divisable;
}

int N = 30;
int i = 0;
int divisable = 0;
for (i = 0; i<N; i++) {
divisable = printFuzzAndOrBuzz(i);
if (!divisable) {
// Not divisable at all
printf("%d", i);
}
// End of line
printf("\n");
}
}

Sundar said: (Feb 2, 2012)  
In typical C language a function cannot be defined inside an another function.

But, in some modern compilers (GCC in Linux) it is possible, lets see an example.

#include <stdio.h>

int main()
{

int add(int a, int b)
{return a+b;}

printf("Sum = %d", add(4,2) );

return 0;
}

//output: Sum = 6

Kiran said: (Sep 16, 2012)  
In GCC compiler, you can define a function inside a function definition. you can try this.

Bdp said: (Jan 9, 2013)  
Is function declare inside a function ?

Mamatha said: (Jan 10, 2013)  
Yes we can declare function inside another function.

Zilanee said: (Jul 20, 2015)  
What about main()?

Himanshu Gupta said: (Sep 21, 2016)  
We can define the function within another function:
A short code snippet proves that.

#include<stdio.h>
main()
{
int fun()
{
printf("In side Fun sunction ...\n");
}
fun();
}

Hithu said: (Aug 5, 2017)  
How can we declare a function inside a function?

Satakshi said: (Jun 21, 2018)  
int main()
{
int fun();
int i;
i = fun();
printf("%d\n", i);
return 0;
}
int fun()
{
_AX = 1990;
}

fun() is declared inside main(). Can this be done?

Kunj said: (Sep 18, 2019)  
{
int sum(int,int) ;//function declaration
int a=5,b=6,c;
c=sum(a,b); //calling and assigned to c
printf("%d", c) ;
return 0;
}
int sum(int a, int b) //function definition
{
return (a+b) ;
}

Am I right?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.