C Programming - Functions - Discussion

Discussion Forum : Functions - True / False Questions (Q.No. 1)
1.
A function cannot be defined inside another function
True
False
Answer: Option
Explanation:

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

Discussion:
12 comments Page 1 of 2.

Kunj said:   5 years ago
{
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?

Satakshi said:   6 years ago
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?

Hithu said:   7 years ago
How can we declare a function inside a function?

Himanshu Gupta said:   8 years ago
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();
}

Zilanee said:   9 years ago
What about main()?

Mamatha said:   1 decade ago
Yes we can declare function inside another function.

BDP said:   1 decade ago
Is function declare inside a function ?

Kiran said:   1 decade ago
In GCC compiler, you can define a function inside a function definition. you can try this.

Sundar said:   1 decade ago
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

Pelle said:   1 decade ago
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");
}
}


Post your comments here:

Your comments will be displayed after verification.