top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Can we define function with in function.

+7 votes
700 views

If yes, why we don't define.

posted Oct 13, 2013 by Mona Sharma

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+8 votes

Yes, we can define function within function.
But the scope of inner function will be limited.
We can not call the inner function directly. We need to call the outer function,
this outer function will return the address of the inner function and
we can use this address to call inner function but the stack of outer function will be finished
after returning the address of inner function. So address of inner function will be garbage address.

You can do One thing :
1) Written a outer_functtion with return type is void pointer;
2) declare a inner in this outer function;

Now see the code below

#include <stdio.h>
#include <pthread.h>

void (*ptr_fun) (void);

void *f2()
{
  while(1)
  {
    sleep(1);
    ptr_fun();
  }
  pthread_exit(0);
}

void *outer_fun()
{
        printf("\n outer function is called \n");
        void inner_fun()
        {
                printf("\n inner function is called \n");
        }
        ptr_fun = inner_fun;
        while(1){}
        pthread_exit(0);
}

int main()
{
  pthread_t f2_thread, f1_thread;

  pthread_create(&f1_thread, NULL, outer_fun, NULL);
  pthread_create(&f2_thread, NULL, f2, NULL);

  pthread_join(f1_thread,NULL);
  pthread_join(f2_thread,NULL);

  return ;
}
answer Oct 13, 2013 by Vikas Upadhyay
+3 votes

I agree with Vikas that the scope of inner function stays within the outer function.

But , as the code of the inner function resides in the code segment of the program, accessing the inner function with a globally defined function pointer ,even after the return of the outer function is valid.

Please refer to the following code snnipet and its output:

 
Code Snippet

void (*funcPtr)(void);
void outerFunction()
{
   void innerFunction()
   {
      printf("innerFunction!!\n");
   }
   funcPtr = innerFunction;
   printf("outerFunction!!\n");
}
int main ()
{
  outerFunction();
  funcPtr();
  return 0;
}

Output

outerFunction!!
innerFunction!!

answer Oct 14, 2013 by Sony Mohanty
You are lucky that It is working but it will not work every time because at time of calling funcPtr()   the stack of outerFunction is finished so this is garbage address because in global pointer you are storing the address of local variable of stack.
Please correct if I am wrong.
void fun1(){
    int i = 5;
}
void fun2(){
    int i;
    printf("\n\t%d\n",i);
}
int main(){
    fun1();
    fun2();
}
TRY THIS :)
It will print 5;
Any function definition ( inside another function or global) will have its binary code in the CODE SEGMENT at a particular address. In my example "funcPtr" is assigned that address and since it is a global variable that address is retained in the global variable irrespective of  "outerFunction" being returned.

The Example you have given  is different from the concerned problem. Rewriting your example to site this will be something like the following
-----------------------------------------------------
void  (*globalPtr)();
void fun1()
{
    void fun2() //suppose the address of this function is 0x1234
    {
    //body
    }
    globalPtr = fun2; //i.e globalPtr = 0x1234;
}
int main()
{
    fun1();
    globalPtr(); //call the function at 0x1234 i.e fun2();
}

I don't see any relation between the return of th outer function and the loss of the address of inner function.
Please let me know if i am missing something.
I have given the example because I want to clarify that if your function is overwritten in memory then funcPtr will be corrupted.
 As in my example stack of fun1 is overwritten at same address space by fun2. The value of i is 5 because of fun1.

In your case when you will call the function "outerFunction" So stack will be created (from heap) and the memory for "innerFunction" will be in stack of "outerFunction".  
I think the address of innerFunction is in stack of outerFunction which is not in CODE SEGMENT.
Some how the stack of outerFunction  is not overwritten in your case.

I tried it on cygwin and it was not working.

Please correct if I am wrong.
In the following code it can be seen that the address space of the local variables and the functions are different.Hence all the functions reside in CODE SEGMENT.
moreover in the output the address of the local variables (a,b) are the same for outerFunction() and outerFunction1() but the address of the innerFunction() and innerFunction1() are different.Had inner function be defined in the stack segment itself both the addresses would be same


#include <stdio.h>
void (*funcPtr)(void);
void (*funcPtr1)(void);
 void outerFunction1()
{
   int a;
   void innerFunction1()
   {
      printf("innerFunction!!\n");
   }
   int b;
   funcPtr1 = innerFunction1;
   printf("outerFunction1!!:outerFunction1=%x\n, a=%x\n, innerFunction1=%x\n, b=%x\n",outerFunction1,&a,innerFunction1,&b);

}
void outerFunction()
{
   int a;
   void innerFunction()
   {
      printf("innerFunction!!\n");
   }
   int b;
   funcPtr = innerFunction;
   printf("outerFunction!!:outerFunction=%x\n, a=%x\n, innerFunction=%x\n, b=%x\n",outerFunction,&a,innerFunction,&b);

}
int main ()
{
  outerFunction();
  outerFunction1();
  funcPtr();
  return 0;
}


output ::

outerFunction!!:outerFunction=8048424
, a=bfd83274
, innerFunction=8048460
, b=bfd83270
outerFunction1!!:outerFunction1=80483d4
, a=bfd83274
, innerFunction1=8048410
, b=bfd83270
innerFunction!!
Yes, You was right,
Thanks  Sony.
Similar Questions
+7 votes
#include<stdio.h>

int &fun()
{
   static int x;
   return x;
}   

int main()
{
   fun() = 10;
   printf(" %d ", fun());

   return 0;
}

It is fine with c++ compiler while giving error with c compiler.

+2 votes

How many parameters can we pass as argument in function? Is there any limit ?
If yes , Please explain the reason.

+1 vote

We have two types of main declaration i.e.

void main()
and
int main()

I want to know the difference between these two and when to use what?

...