1

I'm an absolutely newbie to both this site and programming so please be patient with me. I decided to start learning programming by learning C. Now, I still know nothing outside of stdio.h . Anyway, I've now come across a new topic, which is function and I would like to try using it to do something.

However, I've never got the chance to know whether my algorithm is right or not due to the errors while compiling and I really don't understand what the compiler's error message is trying to tell me. I don't know what caused the errors. Therefore, I need someone to have a look at my code and guide/explain/teach me, anything.

I'm trying to print out X and O in chessboard manner by using a function and here's my code.


int function_X;                                                         
int function_O;                                                               
int size_controller;                                                          

main()                                                            
{                                                                             
    int i;                                                                     
    int j;                                                                     
    int width;                                                                
    int height;

    clrscr();                                                                  

    printf("Width: \n");                                                       
    scanf("%d", &width);

    printf("Height: \n");                                                      
    scanf("%d", &height);

    for(j=0; j<height; j++)                                                   
    {                                                                          
        for(i=0; i<width; i++)                                                  
        {                                                                      
            if(size_controller(i) )                                              
            {                                                                 
                printf("\n");                                                     
            }                                  

            if(function_X(i, j) )                                              
            {           
                printf("X");                                                    
            } 

            if(function_O(i, j) )                                                      
            {                                                                
                printf("O");                                     
            }                                                                
        }                                                                  
    } 

    return 0;
}


int size_controller(int i)                                                    
{                                                                           
    if(i%width == 0)                                                           
    {                                                                          
        return 1;                                                               
    }                                                                          
    else                                                                       
    {                                                                          
        return 0;                                                              
    }                                                                         
}                                                                             


int function_X(int i, int j)                                                 
{                                                                             
    if((j%2 != 0) && (i%2 != 0) || (j%2 == 0) && (i%2 == 0))                   
    {                                                                          
        return 1;                                                               
    }                                                                          
    else                                                                      
    {                                                                          
        return 0;
    }   
}

int function_O(int i, int j)                                                  
{                                                                             
    if((j%2 != 0) && (i%2 == 0) || (j%2 == 0) && (i%2 != 0))                  
    {                                                                          
        return 1;                                                               
    }                                                                        
    else                                                                     
    {                                                                          
        return 0;                                                             
    }                                                                      
}                                                                          

When I tried to run the program, the compiler declared 7 errors and 1 warning:

Error 01.C 27: Call of nonfunction
Error 01.C 32: Call of nonfunction
Error 01.C 37: Call of nonfunction
Error 01.C 48: Type mismatch in redeclaration of 'size_controller' Error 01.C 49: Undefined symbol 'width'
Warning 01.C 57: Parameter 'i' is never used
Error 01.C 60: Type mismatch in redeclaration of 'function_X'
Error 01.C 72: Type mismatch in redeclaration of 'function_O'

Please help me!

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
Autumn K.
  • 33
  • 1
  • 1
  • 7
  • LATEST UPDATE: I've made a few changes and the errors eventually eliminated down to 1 error, which is 01.C 28: Too few parameters in call to 'size_controller' *What does it mean?* – Autumn K. Jan 29 '12 at 08:41
  • LATEST UPDATE OF THE PREVIOUS LATEST UPDATE: HURRAY~!!! I went back to see my program and looked at it real carefully again. I detected a silly mistake that I haven't made any changes to it, fixed it, and I finally made it~!!! Thank you so much, everyone~!!! I'm now really happy!!! You guys have just made my day :D – Autumn K. Jan 29 '12 at 08:45

4 Answers4

3

Change these (which look like ordinary variables):

int function_X;                                                         
int function_O;                                                               
int size_controller;

To these (which look like function declarations):

int size_controller(int i, int width); /* Needs width parameter. */
int function_X(int i, int j);
int function_O(int i, int j);

But the only truly sensible advice is to get hold of a decent book and study, these really are problems you could have avoided.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • Thank you for your advice. I noticed that different books have different kinds of example coding for each topic. I guess the book I'm reading doesn't provide enough examples. – Autumn K. Jan 29 '12 at 08:14
  • Also, K&R C Programming Language - IMO The best C book out there. Though you might not like it that much if this is your first programming experience. – Fingolfin Jan 29 '12 at 08:44
  • Oh, why do you think I might not like it that much? It contains complicated explanations? – Autumn K. Jan 29 '12 at 08:48
  • @AutumnK. I don't know about Adel C Kods opinion but I think it's a terse book, not always suited for beginners. – cnicutar Jan 29 '12 at 08:49
  • Any good books to recommend, anyone? I don't know why but I've tried reading "C for dummies, 2nd edition" and didn't like it much. I mean, I understand the purpose of writing the book that way: to make it enjoyable and "friendly" enough for those who get scared easily of sth too academic. However, I find it rather tedious and never gets to the point. I need a book that gives more real, lucid, and straight-forward explanations than C for dummies. – Autumn K. Jan 29 '12 at 09:13
  • @AutumnK. Start [here](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – cnicutar Jan 29 '12 at 09:14
  • @cnicutar Wow, extremely fantastic! Thank you :D – Autumn K. Jan 29 '12 at 09:24
3

The following is declaring a variable:

int function_X;                                                         

You probably want to declare a prototype for the function:

int function_X(int, int);

Alternately, you can place the whole main() function at the bottom of your source file, so that the functions you call are declared above it. Then when the compiler gets to your main() function, it will have already seen the functions declared and knows how to call them. Doing this also avoids having to repeat the function arguments twice, once in the prototype declaration and once in the function definition.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • +1 For explaining the problem and not just providing code which removes the errors, *guide/explain/teach me, anything* An explanation is what OP was expecting not just an answer. – Alok Save Jan 29 '12 at 07:58
  • Thank you :) Well, since I'm trying to learn here, i think it wouldn't help me much by getting an answer without understanding the actual problems behind it. – Autumn K. Jan 29 '12 at 08:11
0

Your forward declarations are wrong.

int function_X;                                                         
int function_O;                                                               
int size_controller;

are actually declarations of functions (with parameters), so they should be

int size_controller(int i);     
int function_X(int i, int j);  
int function_O(int i, int j);

As you declared them, they are variables and the compiler gets confused when you try to call them.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
0

You have to change two things:

1)AS above suggested by,change function Declaration as above

2)Declare the width variable as global means declare it above main().Not in main().

By doing these two changes it will definitely work..

krushnakant
  • 431
  • 9
  • 21
  • Oh, I should declare width variable as global variable!!! I didn't even think of it!!! Thank you! *Just to be sure, I have to declare any variables used by functions as global variable, right?* – Autumn K. Jan 29 '12 at 08:33
  • Oh, and if I declare my width variable as global variable, do I still need to declare it again inside main()? – Autumn K. Jan 29 '12 at 08:35
  • @AutumnK. No, if you declare a variable global you don't need to declare it in main. – cnicutar Jan 29 '12 at 08:45