Thread: Putting functions in a header file

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    28

    Question Putting functions in a header file

    Ok I am learning how to put functions that i write into a header file and then including that file in my source code. But I keep getting and error for undefined reference to rndNumber(), but the other function I have works fine when I call it. Here is the code to my header file:
    Code:
    #include <time.h>
    
    int rndNumber(int range)
    {
      int x,range; 
      srand((unsigned)time(NULL));
      x = rand()%range;
      return x;
    }
    
    void quit(void)
    {
        char x;
        printf("Press q to quit.....");
        lbl1:
        while(kbhit() == 0);
        if(getch() != 'q')
               goto lbl1;
    }
    And here is the code to my .c file:
    Code:
    #include <stdio.h>
    #include <myfunctions.h>
    
    void quit(void);
    int rndNumber(int range);
    
    int main(int argc, char *argv[])
    {
      int x;
    
      printf("Enter the range:\n");
      scanf("%i",&x);
      rndNumber(x);
      printf("The random number is %i\n",x);
      quit(); 	
      return 0;
    }
    Last edited by Vertex34; 09-29-2004 at 09:57 AM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Ok I am learning how to put functions that i write into a header file

    Don't put definitions in a header -- only declarations. I'd recommend something like this.

    myfunctions.h:
    Code:
    #ifndef MYFUNCTIONS_H
    #define MYFUNCTIONS_H
    
    int rndNumber(int range);
    void quit(void);
    
    #endif
    myfunctions.c:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <conio.h>
    #include "myfunctions.h"
    
    int rndNumber(int range)
    {
      int x; 
      srand((unsigned)time(NULL));
      x = rand()%range;
      return x;
    }
    
    void quit(void)
    {
        printf("Press q to quit.....");
        lbl1:
        while(kbhit() == 0);
        if(getch() != 'q')
               goto lbl1;
    }
    main.c:
    Code:
    #include <stdio.h>
    #include "myfunctions.h"
    
    int main(int argc, char *argv[])
    {
      int x = rndNumber(6);
      printf("The random number is %i\n",x);
      quit(); 	
      return 0;
    }
    Last edited by Dave_Sinkula; 09-29-2004 at 10:05 AM. Reason: Bothered to test it.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    Quote Originally Posted by Vertex34
    Code:
    lbl1:
    while(kbhit() == 0);
    if(getch() != 'q')
        goto lbl1;
    Just a side note. C version would be like:
    Code:
    do
    {
      while (kbhit() == 0);
    } while (getch() != 'q');

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
        lbl1:
        while(kbhit() == 0);
        if(getch() != 'q')
               goto lbl1;
    This is such an insane waste of CPU cycles...
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    28
    Ok, thanks for the advice on the CPU cycles, I will think about that a little more when I am coding.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Code:
    void quit(void)
    {
        printf("Press q to quit.....");
        lbl1:
        while(kbhit() == 0);
        if(getch() != 'q')
               goto lbl1;
    }
    //better this way...
    void quit(void)
    {
        while(getch() != 'q');
        // use exit() if you want...
        exit(0);
    }
    About the undefined references:
    if you're using an IDE and add your myfunctions.c and main.c to the default project, it should compile well.
    If you're using a command line compiler, 1st you have to compile each of the *c. files separately and then link them all together.
    Using gcc here's an examle:
    1# gcc myfunctions.c -c
    2# gcc main.c -c
    3# gcc myfunctions.o main.o -o myprog.exe
    or
    4#gcc myfunctions.c main.c -o myprog.exe
    On 1# and 2# you source files are compiled, and on 3# they're linked together to create a final executable.
    That undefined reference happens because in your main.c you include myfunctions.h which declare a function that you use. That's only a declaration, something to mention the compiler that those symbols are funtions that receive x arguments and return something. Then when you link the final file the compiler MUST know here the compiled function is, the binaries.
    4# is just a short hand for 1#,2# and 3#.
    If you're working with a large project, separating your source through several files is a way to organize you code, and to reduce compilation time, because you only need to compile the file that you change, and re-link.
    Last edited by xErath; 09-29-2004 at 07:47 PM.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Its good that everyone was so helpful. goto statements are to C programmers as garlic is to vampires. Also, kbhit() is not a portable function. Those are only me and other being picky about what you are doing, your way works and you don't have to change it if you don't want to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with file pointer in functions
    By willie in forum C Programming
    Replies: 6
    Last Post: 11-23-2008, 01:54 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. knowing what functions are in a header file
    By nextus in forum C++ Programming
    Replies: 4
    Last Post: 04-10-2002, 03:52 PM