Thread: Calling a function inside a function

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    5

    Calling a function inside a function

    i have the following code and the compiler is throwing error C2371: 'drawgraph' : redefinition; different basic types; on the header line of drawgraph(). I am relatively new to C so are you even allowed to do this? drawgraph isn't anywhere else in my program


    Code:
    void creategraph(int vertnumber){
    	int i = 0, j =0;
    	headedge = Create_Head_Edge();//create the head edge for the LL of edges
    	head = Create_Vertices(vertnumber);//creates the number of verticies based on the arguments
    	while(filevalues[i][j] != 0){//create the LL of edges
    		headedge = Insert_Edge_by_ID(headedge, head, filevalues[i][0], filevalues[i][1]);
    		i++;
    	}
    	drawgraph(head, headedge);	
    }
    
    void drawgraph(struct Vertex* temphead, struct Edge* tempheadedge){ <- the error is thrown here
    	while(temphead != NULL){//traverses the LL
    		DrawCircle(temphead->x, temphead->y, 10, 1, Blue);//draws the vertices to the screen
    		temphead = temphead->next;//increment to the next vertex
    	}
    	
    	while(tempheadedge != NULL){
    		if(tempheadedge->next == NULL)
    			break;
    		DrawLine(tempheadedge->vertex1->x, tempheadedge->vertex1->y, tempheadedge->vertex2->x, tempheadedge->vertex2->y, Black);
    		tempheadedge = tempheadedge->next;
    	}
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't have a prototype for the function drawgraph.

    The compiler has a "default" function prototype it is attempting to use for your drawgraph function, but it's default prototype, and your real function, are not agreeing. You have "redefined" the drawgraph function.

    Usually function prototypes are listed before any function in your program.

    int drawgraph(int, char, float, int*);

    Would be a prototype for a function with parameters of an int, a char, a float, and a pointer to an int. The drawgraph function would have a return type of an integer.

    default for most C compiler is:
    int myfunctionName(int); , with the name of your own function being substituted.

    void myfunctionName(void); would prototype a function with no parameters, and no return type.

    Adding functions before main() avoids the need for prototypes, but is poor programming methodology. It prevents the compiler from having better type checking.

    There is no problem calling a function from inside another function. You can put a function prototype inside the calling function, if you want to (if that's the only function that calls it). That's no problem at all.
    Last edited by Adak; 09-17-2009 at 11:33 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Function calling woes
    By RedZippo in forum C Programming
    Replies: 6
    Last Post: 01-09-2004, 12:39 AM