CAN WE DEFINE ANY FUNCTION WITHIN A FUNCTION.
Answers were Sorted based on User's Feedback
Answer / vignesh1988i
no we cant define a function within a function.... but calling of function within a function is possible........
thank u
| Is This Answer Correct ? | 66 Yes | 10 No |
Answer / prof.gagandeep jagdev
No, it is not possible to define any function within a
function. However we can call a function from within
another function.
| Is This Answer Correct ? | 30 Yes | 4 No |
Answer / aparna
no....we cant define a function with in another
fn......Insteadwe can declare a function..
ex:
void main()
{
int fun(int); // this is declaration
fun(3); // this is fn. calling
}
int fun(int a) //this is fn.definition
{
}
| Is This Answer Correct ? | 22 Yes | 2 No |
Answer / shailender
We can do nesting of functions. I tried in GCC.
check this keyword auto does the magic.
int main()
{
func1();
}
func1()
{
int i = 0;
auto func2()
{
i = 10;
printf("Heloo i am func 2\n");
}
printf("Heloo i am func 1\n");
}
However it seems to me that there is no use of doing this
if you want to call func2 you can't do it from out side func1.
| Is This Answer Correct ? | 12 Yes | 2 No |
Answer / chandan patel
absolutely no......
u can't do this....u can only declared and call a function
inside a function....
i m appealing to those people who r saying 'yes' that please
don't misguide other....if u don't know correct stuff.
| Is This Answer Correct ? | 10 Yes | 3 No |
Answer / blaaze
hey guys pls dont misguide......
the correct ans is "no" , we cant define a function within another.
| Is This Answer Correct ? | 8 Yes | 2 No |
Answer / vinod kumari
No, we can not define a function within another function.
we can only declare and call any function in any other
function.
| Is This Answer Correct ? | 8 Yes | 2 No |
Answer / nimish salve
yes it is possible to define a function inside function.but only in optimized compiler like GCC,DEV C and not in turbo c,visual c++.
try this code
#include<stdio.h>
void fun1(void)//function definition 1
{
printf("Inside fun1\n");
void fun2(void)//function definition for 2nd function
{
printf("Inside fun2\n");
}
fun2();//function call
}
int main()
{
printf("Inside Main\n");
fun1();//function call
return 0;
}
try in both GCC and TurboC and get your answer
| Is This Answer Correct ? | 3 Yes | 0 No |
Answer / shiva
No.
in g++ you will be clearly notified that you can't define a
function inside another function
but gcc somehow allows following
#include <stdio.h>
int func2(int (*f)()){
(*f)();
return 0;
}
int main ()
{
int func1 ()
{
printf ("\n Inside func 1 ");
return 0;
}
printf("someting");
func1 (); //you can access this fuction inside
main(the function in which you declared func1) with the name
func1
func2(func1); // to access this function outside use
fucntion pointer as a argument
return 0;
}
o/p:
something
Inside func 1
Inside func 1
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / shivang
No we cannot define any function within any function.Even
though i read it in book by yeshwant kanetkar,i also tested
it, my program generated an error while doing so.
| Is This Answer Correct ? | 2 Yes | 1 No |
while initialization of two dimensional arrays we can initialize like a[][2] but why not a[2][] is there any reason behind this?
which of the following statements is incorrect a.typedef struct new{ int n1; char n2; } DATA; b.typedef struct { int n3; char *n4; }ICE; c.typedef union { int n5; float n6; } UDT; d.#typedef union { int n7; float n8; } TUDAT;
1. What is the output of printf("%d") 2. What will happen if I say delete this 3. Difference between "C structure" and "C++ structure". 4. Diffrence between a "assignment operator" and a "copy constructor" 5. What is the difference between "overloading" and "overridding"? 6. Explain the need for "Virtual Destructor". 7. Can we have "Virtual Constructors"? 8. What are the different types of polymorphism? 9. What are Virtual Functions? How to implement virtual functions in "C" 10. What are the different types of Storage classes? 11. What is Namespace? 12. What are the types of STL containers?. 13. Difference between "vector" and "array"? 14. How to write a program such that it will delete itself after exectution? 15. Can we generate a C++ source code from the binary file? 16. What are inline functions? 17. Talk sometiming about profiling? 18. How many lines of code you have written for a single program? 19. What is "strstream" ? 20. How to write Multithreaded applications using C++? 21. Explain "passing by value", "passing by pointer" and "passing by reference" 22. Write any small program that will compile in "C" but not in "C++" 23. Have you heard of "mutable" keyword? 24. What is a "RTTI"? 25. Is there something that I can do in C and not in C++? 26. Why preincrement operator is faster than postincrement? 27. What is the difference between "calloc" and "malloc"? 28. What will happen if I allocate memory using "new" and free it using "free" or allocate sing "calloc" and free it using "delete"? 29. What is Memory Alignment? 30. Explain working of printf. 31. Difference between "printf" and "sprintf". 32. What is "map" in STL? 33. When shall I use Multiple Inheritance? 34. What are the techniques you use for debugging? 35. How to reduce a final size of executable? 36. Give 2 examples of a code optimization.
What are enumerated types?
what is the difference between const volatile int i & volatile const int j;
What is #define?
How can a string be converted to a number?
How to avoid buffer overflow?
what is the differance between pass by reference and pass by value.
What are the advantages and disadvantages of pointers?
What does & mean in scanf?
4.weight conversion: Write a program that will read weight in pounds and convert it into grams.print both the original weight and the converted value.There are 454 grams in a pound.design and carry out a test plan for this program.