Thread: function calling function

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    92

    function calling function

    Hi, I have a function that I need to call from another function, I am not passing anything to them and there no return. I am getting an error
    Code:
    error C2371: 'get_two_candidates' : redefinition; different basic types
    Can anyone please explain why I am getting this error.
    Thank you.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    You should always post your code or we cant tell you where the error maight be coming from.

    From the error message, I'm guessing you have two functions with the same name...

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    sorry, here is 2 functions, I'm checking if there is a one winner in a runoff, and if there is not I need to find two candidates who still will run for.... for it I am calling another function
    Code:
    void get_winner(void)
    {
    	
    	if(percent_A>=50)
    		printf("\nCandidate A the winner!\n\n");
    	else if(percent_B>=50)
    			printf("\nCandidate B the winner!\n\n");
    	else if(percent_C>=50)
    			printf("\nCandidate C the winner!\n\n");
    	else if(percent_D>=50)
    			printf("\nCandidate D the winner!\n\n");
    	else 
    		printf("Two condidates will run:\n\n");
    		get_two_candidates();
    		
    
    }
    
    void get_two_candidates(void)
    {
    	int n, m, temp;
    
    	for(n=0;n<3;++n)
    			for(m=n+1;m<4;++m)
    				if(total[n]<total[m])
    				{
    					temp=total[m];
    					total[m]=total[n];
    					total[n]=temp;
    				}
    	
    
    		if(percent_A==total[0])
    			printf("A will run");
    		else if(percent_B==total[0])
    			printf("B will run");
    		else if(percent_C==total[0])
    			printf("C will run");
    		else if(percent_D==total[0])
    			printf("D will run");
    		else if(percent_A==total[1])
    			printf("A will run");
    		else if(percent_B==total[1])
    			printf("B will run");
    		else if(percent_C==total[1])
    			printf("C will run");
    		else 
    			(percent_D==total[1]);
    			printf("D will run");
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to declare (or define) the function before you call it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    I am using function befoure the main, One day I was told that if you use a function befour the main you do not need to declare it, is that right?

    Here is my program:
    Code:
    #include<stdio.h>
    
    int A[5]={192,147,186,114,267};
    int B[5]={48,90,12,21,13};
    int C[5]={206,312,121,408,382};
    int D[5]={37,21,38,39,29};
    int total[4]={0};
    int percent_A, percent_B, percent_C, percent_D;
    
    
    void display_table(void)
    {
    	int i;
    	
    	printf("         Candidate  Candidate  Candidate  Candidate\n");
    	printf("Precinct    A           B          C          D\n") ;
    	printf("___________________________________________________\n");
    
    	for(i=0;i<5;++i)
    		printf("%3i\t   %i          %i        %i         %i\n",i+1,A[i],B[i],C[i],D[i]);
    }
    
    int get_num_of_votes(int X[5])
    {
    	int total=0;
    	int i;
    
    	for(i=0;i<5;++i)
    	{
    		total+=X[i];
    	}
    	
    return total;
    }
    
    int get_percent_of_total_votes(int a, int b)
    {
    	return((a*100)/b);
    }
    void get_winner(void)
    {
    	
    	if(percent_A>=50)
    		printf("\nCandidate A the winner!\n\n");
    	else if(percent_B>=50)
    			printf("\nCandidate B the winner!\n\n");
    	else if(percent_C>=50)
    			printf("\nCandidate C the winner!\n\n");
    	else if(percent_D>=50)
    			printf("\nCandidate D the winner!\n\n");
    	else 
    		printf("Two condidates will run:\n\n");
    		get_two_candidates();
    		
    
    }
    
    void get_two_candidates(void)
    {
    	int n, m, temp;
    
    	for(n=0;n<3;++n)
    			for(m=n+1;m<4;++m)
    				if(total[n]<total[m])
    				{
    					temp=total[m];
    					total[m]=total[n];
    					total[n]=temp;
    				}
    	
    
    		if(percent_A==total[0])
    			printf("A will run");
    		else if(percent_B==total[0])
    			printf("B will run");
    		else if(percent_C==total[0])
    			printf("C will run");
    		else if(percent_D==total[0])
    			printf("D will run");
    		else if(percent_A==total[1])
    			printf("A will run");
    		else if(percent_B==total[1])
    			printf("B will run");
    		else if(percent_C==total[1])
    			printf("C will run");
    		else 
    			(percent_D==total[1]);
    			printf("D will run");
    }
    
    main();
    {
    	int total_A, total_B, total_C, total_D;
    	int votes_total;
    	
    	display_table();
    
    	total_A=get_num_of_votes(A);
    	total_B=get_num_of_votes(B);
    	total_C=get_num_of_votes(C);
    	total_D=get_num_of_votes(D);
    
    	votes_total=total_A+total_B+total_C+total_D;
    
    	percent_A=get_percent_of_total_votes(total_A, votes_total);
    		total[0]=percent_A;
    	percent_B=get_percent_of_total_votes(total_B, votes_total);
    		total[1]=percent_B;
    	percent_C=get_percent_of_total_votes(total_C, votes_total);
    		total[2]=percent_C;
    	percent_D=get_percent_of_total_votes(total_D, votes_total);
    		total[3]=percent_D;
    
    
    	printf("\n");
    	printf("Candidate: precent\n");
    	printf("    A%10i\n    B%10i\n    C%10i\n    D%10i\n",percent_A, percent_B, percent_C, percent_D);
    
    	get_winner();
    
    
    }

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    But get_winner() is calling get_two_candidates() and get_winner() is above get_two_candidates(), so it doesn't know what you're talking about unless you either move get_two_candidates() up or put a declaration for it at the top.

    BTW, are those 2nd lines below the else clauses indented because you think they belong to the else clause or just because you pressed tab too many times? If they belong in the else, you need braces around the code.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Thank you , I switched functions and it works now. and thanks for pointing to mistake in my else statement.
    BTW, I love this forum, I think its a best one between those programming forums what I know.
    You always helped (if you deserve), and very easy to search and novigate through.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would still recommend you declare your functions, for two reasons:
    - It allows functions to call each other without you needing to reorder them.
    - It allows tools such as IntelliSense to provide you with information when calling functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by nynicue View Post
    sorry, here is 2 functions, I'm checking if there is a one winner in a runoff, and if there is not I need to find two candidates who still will run for.... for it I am calling another function
    Code:
    void get_winner(void)
    {
    	
    	if(percent_A>=50)
    		printf("\nCandidate A the winner!\n\n");
    	else if(percent_B>=50)
    			printf("\nCandidate B the winner!\n\n");
    	else if(percent_C>=50)
    			printf("\nCandidate C the winner!\n\n");
    	else if(percent_D>=50)
    			printf("\nCandidate D the winner!\n\n");
    	else 
    		printf("Two condidates will run:\n\n");
    		get_two_candidates();
    		
    
    }
    
    void get_two_candidates(void)
    {
    	int n, m, temp;
    
    	for(n=0;n<3;++n)
    			for(m=n+1;m<4;++m)
    				if(total[n]<total[m])
    				{
    					temp=total[m];
    					total[m]=total[n];
    					total[n]=temp;
    				}
    	
    
    		if(percent_A==total[0])
    			printf("A will run");
    		else if(percent_B==total[0])
    			printf("B will run");
    		else if(percent_C==total[0])
    			printf("C will run");
    		else if(percent_D==total[0])
    			printf("D will run");
    		else if(percent_A==total[1])
    			printf("A will run");
    		else if(percent_B==total[1])
    			printf("B will run");
    		else if(percent_C==total[1])
    			printf("C will run");
    		else 
    			(percent_D==total[1]);
    			printf("D will run");
    }
    just place the get_to_candidates() function above the get_winner function.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just make a prototype!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    From the error message, I'm guessing you have two functions with the same name...
    In this case it's one function with different return types. The first call of get_two_candidates() is not preceded by a prototype, so the compiler assumes that the function returns an int. A few lines later, the definition of get_two_candidates() makes it return void, so the compiler is confused. Hence the babble about different basic types.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM
  5. Calling a user-defined function in C++
    By brianptodd in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2002, 12:09 PM