Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. beginner question... recursive function

beginner question... recursive function

Scheduled Pinned Locked Moved C / C++ / MFC
questionlearning
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 10054935
    wrote on last edited by
    #1

    i have this code and it works fine.

    int fatt(int n)
    {
    int fattoriale = 1;

    if(n==0)
        return fattoriale;
    else
        return fattoriale=n\*fatt(n-1);
    

    }

    void main(void)
    {
    printf("%d\n",fatt(10));

    fflush(stdin);
    getchar();
    

    }

    my question is why it works fine also here ?

    int fatt(int n)
    {
    int fattoriale = 1;

    if(n==0)
        return fattoriale;
    else
        fattoriale=n\*fatt(n-1);
    

    }

    void main(void)
    {
    printf("%d\n",fatt(10));

    fflush(stdin);
    getchar();
    

    }

    in this, there aren't the return in the else.... i am using VS2010 and compile in .c . thanks ps: sorry for my english

    S E S 3 Replies Last reply
    0
    • U User 10054935

      i have this code and it works fine.

      int fatt(int n)
      {
      int fattoriale = 1;

      if(n==0)
          return fattoriale;
      else
          return fattoriale=n\*fatt(n-1);
      

      }

      void main(void)
      {
      printf("%d\n",fatt(10));

      fflush(stdin);
      getchar();
      

      }

      my question is why it works fine also here ?

      int fatt(int n)
      {
      int fattoriale = 1;

      if(n==0)
          return fattoriale;
      else
          fattoriale=n\*fatt(n-1);
      

      }

      void main(void)
      {
      printf("%d\n",fatt(10));

      fflush(stdin);
      getchar();
      

      }

      in this, there aren't the return in the else.... i am using VS2010 and compile in .c . thanks ps: sorry for my english

      S Offline
      S Offline
      Santhosh G_
      wrote on last edited by
      #2

      In release mode it will create compilation error. It creates compilation error in VS2008. I got compilation error "error C4716: 'fatt' : must return a value". But in debug mode it works as follows. In both case contents of EAX register is used as the output of fatt() function. In both case EAX holds the output value, therefore the two versions works correctly in debug mode. The result of imul is available in eax register and that is the expected result from the fatt function. Following are the disassembly of two versions of fatt() function. First one with return value and next is without return value.

          return fattoriale=n\*fatt(n-1);
      

      01182ED2 mov eax,dword ptr [n]
      01182ED5 sub eax,1
      01182ED8 push eax
      01182ED9 call fatt (1181889h)
      01182EDE add esp,4
      01182EE1 imul eax,dword ptr [n]
      01182EE5 mov dword ptr [fattoriale],eax
      01182EE8 mov eax,dword ptr [fattoriale]

          fattoriale=n\*fatt(n-1);
      

      01182F92 mov eax,dword ptr [n]
      01182F95 sub eax,1
      01182F98 push eax
      01182F99 call fatt (11815FAh)
      01182F9E add esp,4
      01182FA1 imul eax,dword ptr [n]
      01182FA5 mov dword ptr [fattoriale],eax

      The multiplication in n*fatt(n-1) is done with imul operation. And its output is available in EAX register. In first version( with return statement), last two instructions perform the following things. Contents of eax is moved to fattoriale and after that the contents of fattoriale is moved to eax( EAX holds the return value, or result of operation). In second version( without return statement) the last mov is not found. But the output of iMul is in EAX register and therefore the output will be available to caller function through EAX register.

      A U 2 Replies Last reply
      0
      • S Santhosh G_

        In release mode it will create compilation error. It creates compilation error in VS2008. I got compilation error "error C4716: 'fatt' : must return a value". But in debug mode it works as follows. In both case contents of EAX register is used as the output of fatt() function. In both case EAX holds the output value, therefore the two versions works correctly in debug mode. The result of imul is available in eax register and that is the expected result from the fatt function. Following are the disassembly of two versions of fatt() function. First one with return value and next is without return value.

            return fattoriale=n\*fatt(n-1);
        

        01182ED2 mov eax,dword ptr [n]
        01182ED5 sub eax,1
        01182ED8 push eax
        01182ED9 call fatt (1181889h)
        01182EDE add esp,4
        01182EE1 imul eax,dword ptr [n]
        01182EE5 mov dword ptr [fattoriale],eax
        01182EE8 mov eax,dword ptr [fattoriale]

            fattoriale=n\*fatt(n-1);
        

        01182F92 mov eax,dword ptr [n]
        01182F95 sub eax,1
        01182F98 push eax
        01182F99 call fatt (11815FAh)
        01182F9E add esp,4
        01182FA1 imul eax,dword ptr [n]
        01182FA5 mov dword ptr [fattoriale],eax

        The multiplication in n*fatt(n-1) is done with imul operation. And its output is available in EAX register. In first version( with return statement), last two instructions perform the following things. Contents of eax is moved to fattoriale and after that the contents of fattoriale is moved to eax( EAX holds the return value, or result of operation). In second version( without return statement) the last mov is not found. But the output of iMul is in EAX register and therefore the output will be available to caller function through EAX register.

        A Offline
        A Offline
        AlphaDeltaTheta
        wrote on last edited by
        #3

        Your explanation is nice but do you expect a beginner in c to understand x86 assembly?

        Beauty cannot be defined by abscissas and ordinates; neither are circles and ellipses created by their geometrical formulas.

        ~ Carl von Clausewitz ~

        Source

        J 1 Reply Last reply
        0
        • U User 10054935

          i have this code and it works fine.

          int fatt(int n)
          {
          int fattoriale = 1;

          if(n==0)
              return fattoriale;
          else
              return fattoriale=n\*fatt(n-1);
          

          }

          void main(void)
          {
          printf("%d\n",fatt(10));

          fflush(stdin);
          getchar();
          

          }

          my question is why it works fine also here ?

          int fatt(int n)
          {
          int fattoriale = 1;

          if(n==0)
              return fattoriale;
          else
              fattoriale=n\*fatt(n-1);
          

          }

          void main(void)
          {
          printf("%d\n",fatt(10));

          fflush(stdin);
          getchar();
          

          }

          in this, there aren't the return in the else.... i am using VS2010 and compile in .c . thanks ps: sorry for my english

          E Offline
          E Offline
          Erudite_Eric
          wrote on last edited by
          #4

          Without the 'else' the program flow just goes to the end of the func and returns. However there is no return value, so anything calling fatt() and expecting a return value if the 'else' path is hit is going to be dissapointed.

          1 Reply Last reply
          0
          • A AlphaDeltaTheta

            Your explanation is nice but do you expect a beginner in c to understand x86 assembly?

            Beauty cannot be defined by abscissas and ordinates; neither are circles and ellipses created by their geometrical formulas.

            ~ Carl von Clausewitz ~

            Source

            J Offline
            J Offline
            Joe Woodbury
            wrote on last edited by
            #5

            The point is that values are returned in the eax register. The mathematical operation happens to end up with the result in eax AND eax isn't changed by any other operation. Thus it returns the right value. Problem is that something like an inline optimization could change that.

            1 Reply Last reply
            0
            • U User 10054935

              i have this code and it works fine.

              int fatt(int n)
              {
              int fattoriale = 1;

              if(n==0)
                  return fattoriale;
              else
                  return fattoriale=n\*fatt(n-1);
              

              }

              void main(void)
              {
              printf("%d\n",fatt(10));

              fflush(stdin);
              getchar();
              

              }

              my question is why it works fine also here ?

              int fatt(int n)
              {
              int fattoriale = 1;

              if(n==0)
                  return fattoriale;
              else
                  fattoriale=n\*fatt(n-1);
              

              }

              void main(void)
              {
              printf("%d\n",fatt(10));

              fflush(stdin);
              getchar();
              

              }

              in this, there aren't the return in the else.... i am using VS2010 and compile in .c . thanks ps: sorry for my english

              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #6

              For me, both in Debug and Release mode, the compiler (VS 2010) issues a warning for the second version, indicating the problem. I have set Warning level to 3, and I've found that the majority of Warnings at that level indicate true errors. My advice to beginners is: 1. Set Warning level to at least 3 (but see link below) 2. Make sure you first understand the true meaning of each Warning and what kind(s) of problem(s) it could cause at runtime. Then fix it.* Here's an interesting article on how to deal with warnings, including those that are off by default[^].

              1 Reply Last reply
              0
              • S Santhosh G_

                In release mode it will create compilation error. It creates compilation error in VS2008. I got compilation error "error C4716: 'fatt' : must return a value". But in debug mode it works as follows. In both case contents of EAX register is used as the output of fatt() function. In both case EAX holds the output value, therefore the two versions works correctly in debug mode. The result of imul is available in eax register and that is the expected result from the fatt function. Following are the disassembly of two versions of fatt() function. First one with return value and next is without return value.

                    return fattoriale=n\*fatt(n-1);
                

                01182ED2 mov eax,dword ptr [n]
                01182ED5 sub eax,1
                01182ED8 push eax
                01182ED9 call fatt (1181889h)
                01182EDE add esp,4
                01182EE1 imul eax,dword ptr [n]
                01182EE5 mov dword ptr [fattoriale],eax
                01182EE8 mov eax,dword ptr [fattoriale]

                    fattoriale=n\*fatt(n-1);
                

                01182F92 mov eax,dword ptr [n]
                01182F95 sub eax,1
                01182F98 push eax
                01182F99 call fatt (11815FAh)
                01182F9E add esp,4
                01182FA1 imul eax,dword ptr [n]
                01182FA5 mov dword ptr [fattoriale],eax

                The multiplication in n*fatt(n-1) is done with imul operation. And its output is available in EAX register. In first version( with return statement), last two instructions perform the following things. Contents of eax is moved to fattoriale and after that the contents of fattoriale is moved to eax( EAX holds the return value, or result of operation). In second version( without return statement) the last mov is not found. But the output of iMul is in EAX register and therefore the output will be available to caller function through EAX register.

                U Offline
                U Offline
                User 10054935
                wrote on last edited by
                #7

                Thanks to all! :) I understood!

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups