Recursive function won't compile

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bc1891@googlemail.com

    Recursive function won't compile

    #include <stdio.h>
    #include <stdlib.h>

    def RecursiveFact(n ):
    if(n>1):
    return n*RecursiveFact (n-1)
    else:
    return 1

    fact = RecursiveFact(3 1)
    print fact

    fact = "End of program"
    print fact


    .......but yet it still gives the right answer. How is this possible?
  • ajaksu

    #2
    Re: Recursive function won't compile

    On Apr 2, 5:23 pm, bc1...@googlema il.com wrote:
    #include <stdio.h>
    #include <stdlib.h>
    >
    def RecursiveFact(n ):
        if(n>1):
            return n*RecursiveFact (n-1)
        else:
            return 1
    >
    fact = RecursiveFact(3 1)
    print fact
    The output is 822283865417792 281772556288000 0000 and is correct. But
    the "#include"s tell me you're a bit confused. Have you tried running
    "python yourscript.py" (where "yourscript .py" is the filename you
    saved the above program to)?

    HTH,
    Daniel

    Comment

    • dj3vande@csclub.uwaterloo.ca.invalid

      #3
      Re: Recursive function won't compile

      In article <be71dc98-63c0-4a56-80e8-7d83b2d3e882@u3 6g2000prf.googl egroups.com>,
      <bc1891@googlem ail.comwrote:

      (Subject: Recursive function won't compile)
      >#include <stdio.h>
      >#include <stdlib.h>
      >
      >def RecursiveFact(n ):
      >......but yet it still gives the right answer. How is this possible?
      Possibly because it gives the right answer in a different language than
      it fails to compile in?


      dave

      --
      Dave Vandervies dj3vande at eskimo dot com
      A violent rewrite could produce something worthwhile; as it is you need enough
      experience to not need the book to be able to read it. Well, that makes sense
      to me, anyhow. --CBFalconer in comp.lang.c

      Comment

      • Diez B. Roggisch

        #4
        Re: Recursive function won't compile

        bc1891@googlema il.com schrieb:
        #include <stdio.h>
        #include <stdlib.h>
        >
        def RecursiveFact(n ):
        if(n>1):
        return n*RecursiveFact (n-1)
        else:
        return 1
        >
        fact = RecursiveFact(3 1)
        print fact
        >
        fact = "End of program"
        print fact
        >
        >
        ......but yet it still gives the right answer. How is this possible?
        Given that you obviously don't use python, but some weird cross-breed
        beteween python and C - who are we to judge the semantics of that chimera?

        Diez

        Comment

        • George Sakkis

          #5
          Re: Recursive function won't compile

          On Apr 2, 5:00 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
          bc1...@googlema il.com schrieb:
          >
          >
          >
          #include <stdio.h>
          #include <stdlib.h>
          >
          def RecursiveFact(n ):
          if(n>1):
          return n*RecursiveFact (n-1)
          else:
          return 1
          >
          fact = RecursiveFact(3 1)
          print fact
          >
          fact = "End of program"
          print fact
          >
          ......but yet it still gives the right answer. How is this possible?
          >
          Given that you obviously don't use python, but some weird cross-breed
          beteween python and C - who are we to judge the semantics of that chimera?
          >
          Diez
          Seems like a bad belated April Fool's day joke to me.

          George

          Comment

          • Robbie Hatley

            #6
            Re: Recursive function won't compile


            "bc1891" wrote:
            #include <stdio.h>
            #include <stdlib.h>
            >
            def RecursiveFact(n ):
            if(n>1):
            return n*RecursiveFact (n-1)
            else:
            return 1
            >
            fact = RecursiveFact(3 1)
            print fact
            >
            fact = "End of program"
            print fact
            >
            ......but yet it still gives the right answer. How is this possible?

            No, no, no. Write that in Haskell instead, like this:


            #Switch<C++RTTI >(ON)
            #include <stdio.hAND <stdlib.h>
            #use FORTRAN_RUNTIME _MODULE but compile_as(Cobo l)
            #Incorporate{Pa scalInterpreter } but run_as(Haskell)
            use strict;
            use warnings;
            sub RecursiveFact
            {
            my $n=shift;
            if ($n 1)
            {
            return $n * RecursiveFact($ n-1);
            }
            else
            {
            return 1;
            }
            }
            printf("%d\n",R ecursiveFact($A RGV[0]));


            Yep, that there Haskell program should solve these C++
            problems you've been having with that Oberon program of yours,
            by injecting a bit of Perlescence.


            Or just write it in Fortran and be done with it:


            /* This is a really lovely Fortran program. */
            #include <stdio.h>
            #include <stdlib.h>
            int RecursiveFact (int n)
            {
            return n>1?n*Recursive Fact(n-1):1;
            }
            int main (int argc, char ** argv)
            {
            printf("%d", RecursiveFact(a toi(argv[1])));
            return 0;
            }


            --
            Cheers,
            Robbie Hatley
            lonewolf aatt well dott com
            www dott well dott com slant user slant lonewolf slant


            Comment

            • Keith Thompson

              #7
              Re: Recursive function won't compile

              "Diez B. Roggisch" <deets@nospam.w eb.dewrites:
              >#include <stdio.h>
              >#include <stdlib.h>
              >>
              >def RecursiveFact(n ):
              > if(n>1):
              > return n*RecursiveFact (n-1)
              > else:
              > return 1
              >>
              >fact = RecursiveFact(3 1)
              >print fact
              >>
              >fact = "End of program"
              >print fact
              >>
              >>
              >......but yet it still gives the right answer. How is this possible?
              >
              Given that you obviously don't use python, but some weird cross-breed
              beteween python and C - who are we to judge the semantics of that
              chimera?
              What do you mean? Aren't these:

              #include <stdio.h.
              #include <stdlib.h>

              perfectly valid comments in Python?

              Followups redirected.

              --
              Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
              Nokia
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              Working...