Menu

need help with functions...calling

exhibit
2008-08-13
2012-09-26
  • exhibit

    exhibit - 2008-08-13

    this is the source prog i ve written ...m practisin as m new to c...every time im using functions...error displays " function not defined" ...m using DEV C++ version 4.9.9.2

    include <stdio.h>

    main( )
    {
    int a, fact ;
    printf ( "\nEnter any number " ) ;
    scanf ( "%d", &a ) ;
    fact = factorial ( a ) ;
    printf ( "Factorial value = %d", fact ) ;
    }
    factorial ( int x )
    {
    int f = 1, i ;
    for ( i = x ; i >= 1 ; i-- )
    f = f * i ;
    return ( f ) ;
    }

     
    • Wayne Keen

      Wayne Keen - 2008-08-13

      Could you please post your full compile log, its on the tab labeled "Compile Log", and the right mouse button brings up the copy menu. Excerpting the error message throws away a lot of useful information.

      Wayne

       
    • Wayne Keen

      Wayne Keen - 2008-08-13

      The following does not change my request for your compile log. You probably should read the thread titled "Please Read Before Posting a Question" - in particular the section on the Basic 3.

      Now:

      While I actually get your code to compile and run succesfully, I am using a different version of the GCC compiler. You might want to ponder the following modifications to your code. (The fflush statements are for me, they insure that the printf's actually print at the right time in MinGW/MSYS).

      include <stdio.h>

      int factorial ( int x );
      main( )
      {
      int a, fact ;
      printf ( "\nEnter any number " ) ;
      fflush(stdout);
      scanf ( "%d", &a ) ;
      fact = factorial ( a ) ;
      printf ( "Factorial value = %d", fact ) ;
      fflush(stdout);
      }
      int factorial ( int x )
      {
      int f = 1, i ;
      for ( i = x ; i >= 1 ; i-- )
      f = f * i ;
      return ( f ) ;
      }

       
    • cpns

      cpns - 2008-08-13

      In C++ a function declaration or definition must be provided before a function is used. In C you should do the same, but if you don't a function returning an int is assumed, but it is not good practice to rely on that, and the compiler should issue a warning.

      Are you using C or C++ compilation? In C you should have got a warning not an error, but your code is not valid C++ either for other reasons, so that would not have been the only error.

      You should always post the complete Compile log text so that we can see how you build the code, whether you use C or C++ compilation, and exactly what errors/warnings are reported and for which lines.

      To fix your problem, and make the code C++ compilable (since good C is generally also valid C++) (modified lines marked):

      include <stdio.h>

      static int factorial ( int x ) ; ////////////// (required in C++, recommended in C)

      int main( ) ////////////// (required by C++)
      {
      int a, fact ;
      printf ( "\nEnter any number " ) ;
      scanf ( "%d", &a ) ;
      fact = factorial ( a ) ;
      printf ( "Factorial value = %d", fact ) ;

      return 0 ; ////////////// (not strictly necessary in C++, required by C )
      

      }

      int factorial ( int x ) ////////////// (required by C++)
      {
      int f = 1, i ;
      for ( i = x ; i >= 1 ; i-- )
      f = f * i ;
      return ( f ) ;
      }

      Note that your factorial() function is unnecessarily inefficient. The loop does a final multiply by 1 unnecessarily. But since a 32bit int cannot handle results beyond 12! it is feasible to use a look-up table while at the same time being both faster and deterministic (i.e. will take the same length of time regardless of the value of x):

      int factorial ( int x )
      {
      int f = -1 ;
      static int lookup[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600 } ;
      if( x <= 12 && x >= 0)
      {
      f = lookup[x] ;
      }
      return ( f ) ;
      }

      Note my version returns -1 if the value cannot be computed, yours would simply return an incorrect value. Using a long long int (64bit) will support 20! (still best done with a look-up table). Using double precision floating point will get you approximations of higher factorials - but only approximations. For some pointers on algorithms and techniques for calculating larger factorials, see http://en.wikipedia.org/wiki/Factorial#Computation

      Finally, if you run your code from the IDE, you will want to add code to wait before closing the console window, which will happen as soon as your program terminates. Otherwise you will not see the result.

      Clifford

       
    • exhibit

      exhibit - 2008-08-14

      here s the compile log.....

      Compiler: Default compiler
      Executing g++.exe...
      g++.exe "C:\Documents and Settings\abhishek\Desktop\ex3.cpp" -o "C:\Documents and Settings\abhishek\Desktop\ex3.exe" -ansi -traditional-cpp -g3 -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
      C:\Documents and Settings\abhishek\Desktop\ex3.cpp: In function int main()': C:\Documents and Settings\abhishek\Desktop\ex3.cpp:7: error:factorial' undeclared (first use this function)
      C:\Documents and Settings\abhishek\Desktop\ex3.cpp:7: error: (Each undeclared identifier is reported only once for each function it appears in.)

      C:\Documents and Settings\abhishek\Desktop\ex3.cpp: At global scope:
      C:\Documents and Settings\abhishek\Desktop\ex3.cpp:11: error: ISO C++ forbids declaration of factorial' with no type C:\Documents and Settings\abhishek\Desktop\ex3.cpp: In functionint factorial(int)':
      C:\Documents and Settings\abhishek\Desktop\ex3.cpp:11: error: `int factorial(int)' used prior to declaration

      Execution terminated@

      @ DR WAYNE ...ur suggested code worked with the included lines

       
      • Wayne Keen

        Wayne Keen - 2008-08-14

        OK, go back and read Clifford's comments in detail. It turns out that you are in fact compiling your code as C++ code (the 'g++' in the log tells me that, as well as the error message), and C++ will not let you use a function before it knows what it is.

        If you look at the code I posted, you will see a line I added near the top. This line tells the compiler to expect a function with a certain name, that takes a certain number of arguments of a certain type, and spits out an argument of a type. It needs to see this BEFORE it runs into a line of regular code that uses the function.

        In learning to program, when you ask a question like this, you need to think very logically. My code works, yours does not - the first questions should be:

        (1) What did I do differently?

        (2) Why did I do it?

        This is not hard, and I am NO wizard. If you have a question of the form

        "Why did you add this line:"

        feel free to ask. I don't want you to think of these as magic words.

        Wayne

        p.s. Always read what Clifford says, he is very good, and takes the time to do a thorough post.

         
    • cpns

      cpns - 2008-08-14

      > @ DR WAYNE ...ur suggested code worked with the included lines

      I doubt that, because you used C++ compilation, so his code would still have produced the "ISO C++ forbids declaration of `factorial' with no type" error. My code contained the same corrections as Wayne's, plus those necessary to make it C++ compilable. As Wayne pointed out, the stdout flushing although harmless is not necessary in your case - just adding a '\n' at the end of the output statements will flush the buffer in any case.

      Of course nothing in your code requires C++ compilation, you could have used C compilation, but C++ provides stronger error checking (which is why you had this error in the first place).

      I would suggest that you don't use the -traditional-cpp option unless you are building legacy code that needs it. With both -traditional-cpp and -ansi you seem to want it both ways! According to the GNU GCC manual the compiler now ignores -traditional-cpp in any case, it only affects the behaviour of the C-Preprocessor. Nothing in your code needs this option.

      I do recommend however that you add -Wall -Werror as compiler options.

      You also appear to have specified -g3 twice. This is not a problem, but if you decide to create a release build, you will have to switch off the debug generation in two places.

      There is an issue with Dev-C++ and paths containing spaces under some circumstances. For that reason putting your project at "C:\Documents and Settings\abhishek\Desktop" is not a good idea. Had you read the "PLEASE READ BEFORE POSTING A QUESTION" thread you will have found that advice. Besides the build process creates a number of intermediate files, you probably don't want them dumped on your desktop.

      Clifford

       
      • Wayne Keen

        Wayne Keen - 2008-08-14

        I put an "int" in front of the function (definition and implementation) in what I posted for the OP, so it does compile under g++.

        Wayne

         
    • Wayne Keen

      Wayne Keen - 2008-08-13

      Interestingly, the original code compiled as C code without warning on MinGW/MSYS (GCC-4.2.1).

      Now I know what Zero used to refer to as "thwarting".

      Just kidding Clifford...

      Wayne

       
      • cpns

        cpns - 2008-08-13

        Ha! You didn't post your log! ;)

        Perhaps because I habitually use -Wall or \W4 in VC++ I would expect to see a warning. Because it is valid C you may not get a warning if you don't ask for them. You should ask for warnings, and for them to be regarded as errors (-Wall -Werror in GCC). The resultant code will be better for it.

        However does not explain how he got an error, it implies C++ compilation, but if he'd used that he'd have had other errors as well - you cannot have an implicit return type in C++, and you should not use then in C either.

        Clifford

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.