Thread: Compilation error in simple function

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    Compilation error in simple function

    Hi,
    I wrote this small code for a factorial of a number in double format. The compiler complains error at fact declaration saying "conflicting types for fact". Where am i going wrong?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(){
    int i = 0;
    double m = 0;
    printf("Enter a number whose factorial is needed ");
    scanf ("%d", &i);
    m = fact(i);
    printf("%lf\n", m);
    return 0;
    }
    
    double fact(int i){
    
    double j = 1;
    
    if (i < 0){
    	printf("Invalid Number. Program Exiting now!!\n");
    	exit (1);
    	}
    
    else
    	for(; i > 0 ;){
    	j = j * i;
    	i--;
            } 
    		
    return j;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(){
    int i = 0;
    double m = 0;
    printf("Enter a number whose factorial is needed ");
    scanf ("%d", &i);
    m = fact(i);
    printf("%lf\n", m);
    return 0;
    }
    
    double fact(int i){
    You use fact before you define the function, and you don't have a prototype for it. So since it knows nothing about the function, it assumes it returns an int (and I believe in this case, takes an int). Your program only knows what is above the current line. So the first time it sees fact it looks above and says "I have never seen this. I will assume it is X."

    Then it runs into it the second time when you declare the function and says "This isn't what I assumed it to be. You have a problem."


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    Thanks a lot..that was indeed something I didn't know. So it means if u want to define ur function after ur main(), atleast declare a prototype before main(). Else define the function itself before main(). Is what I observed correct?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yep... arrange your functions above main() so that no function is called before it's catalogued... I tend to use prototyping very sparingly.

    In C function prototypes allow you to defeat the "functional heirarchy" of the program and often subtle bugs can be introduced by it. In Pascal (where I learned programming) you couldn't do that and, to this day, I still believe it produces better code.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    In C function prototypes allow you to defeat the "functional heirarchy" of the program and often subtle bugs can be introduced by it.
    What subtle bugs have you encountered that were introduced by the use of this commonly used feature?

    Function prototypes are commonly used because they are what goes in the headers of a library, where functions are concerned.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    What subtle bugs have you encountered that were introduced by the use of this commonly used feature?

    Function prototypes are commonly used because they are what goes in the headers of a library, where functions are concerned.
    We've had this conversation before Lase...
    And I seem to recall that despite offering you explicit examples, it went noplace.

    I guess it comes down to this... Unless you've programmed in a language with rigid structure you won't appreciate the benefits of a functional heirarchy... just like you won't appreciate the benefits of real strings, as opposed to C strings, until you've worked with them. (Sigh... I really miss Pascal...)
    Last edited by CommonTater; 04-17-2011 at 09:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compilation error on function overloading..
    By sanddune008 in forum C++ Programming
    Replies: 2
    Last Post: 07-27-2010, 10:09 AM
  2. error: was not declared in this scope compilation error
    By i_r_tomash in forum C Programming
    Replies: 2
    Last Post: 05-27-2010, 07:44 AM
  3. Simple C++ Compilation Question
    By mercury529 in forum C++ Programming
    Replies: 14
    Last Post: 07-30-2006, 09:40 PM
  4. compilation error using std::map
    By dbeyer in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2006, 04:10 PM
  5. compilation error
    By blue_gene in forum C++ Programming
    Replies: 1
    Last Post: 12-28-2003, 12:49 AM