Thread: Function issues

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    21

    Function issues

    So I guess you can't be redundant in your function and variable names? I'm getting an error that says "Call of non-function in function main." Here's a sample code I wrote just to test the name. I changed the name of the function from counter to something else, but kept the variable names the same, and it worked. Go figure:

    #include <stdio.h>
    #include <stdlib.h>
    int counter();

    main()
    {
    int counter = 0;
    counter = counter();
    printf("%d", counter);
    getch();
    }

    int counter()
    {
    int counter = 0;
    for (counter = 0; counter <10; counter++)
    {
    }
    return counter;
    }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    This is where it goes wrong:

    counter = counter();

    Change the name of the variable or the name of the function.

    BTW, function main needs to return something.

    Code:
    int main ()
    {
        int return_value;
        ....
        return return_value;
    }

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    21
    But I thought that C could determine the difference between a function and a variable. Does that mean that I can't have a function with the same name as a variable anywhere in the program?

  4. #4
    aurė entuluva! mithrandir's Avatar
    Join Date
    Aug 2001
    Posts
    1,209
    >>Does that mean that I can't have a function with the same name as a variable anywhere in the program?<<

    Well look at your program - you've got three things called "counter"...a function, a variable within the function, and a variable in main. Now this is just about breaks every accepted rule of naming conventions in C.

    Even when corrected:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int count_func();
    
    int main()
    {
    	int counter = 0;
    	counter = count_func();
    	printf("%d", counter);
    	fgetc(stdin);
    	return 0;
    }
    
    int count_func()
    {
    	int counter = 0;
    
    	for (counter = 0; counter < 10; counter++)
    
    	return counter;
    }
    That still returns 0. I assume what you want to do is pass the value of counter in count_func to main, and print that value. Well to do this, you could use a pointer. Otherwise, you could just call the function in main, therefore outputting the value of counter. In any case, my advice is to think about what it is you want your program to do before you code it.

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. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM