Thread: .h and functions question

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Australia
    Posts
    5

    .h and functions question

    what exactly does the .h at the end of curtain #include librarys mean? is it just apart of the name of the library?

    also, at the very start of the code after includes etc people define a "shell" of their functions

    Code:
    void example();
    when the function is called later, i noticed it still works even if the function is defined after the code for example

    Code:
    void example();
    example();
    
    void example() {
    cout<<"Example\n";
    }
    when a function is called, does the code automatically keep looking down the code until it finds the first defined version of the function (if not defined already)? unlike PHP where you have to pre define the function contents before calling for it.

    thanks for any help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > what exactly does the .h at the end of curtain #include librarys mean?
    Header file.

    This is different from a library (.lib)

    Header files contain function prototypes, which enable the compiler to generate the correct code to call the function.

    Libraries contain the actual compiled code, which is used by the linker to produce an executable program.

    > i noticed it still works even if the function is defined after the code for example
    The compiler only needs the prototype to be able to call the function. Whether that function is later on in the same module, in another module or in another library really doesn't matter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> what exactly does the .h at the end of curtain #include librarys mean?

    For standard C++ library headers like iostream, fstream, new, etc., the .h means that the header is out-dated and non-standard. For those headers you want to include the ones without the .h: #include <iostream>, #include <fstream>, #include <string>, etc.

    In C++, for standard C library headers like stdio.h, stdlib.h, math.h, etc., the .h means that the header is deprecated in favor of the C++ version. For those headers you want to add a c to the front and remove the .h: #include <cstdio>, #include <cstdlib>, #include <cmath>, etc.

    While the .h does generally stand for header file, it is just a convention and is not mandatory.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    also, at the very start of the code after includes etc people define a "shell" of their functions
    It's like when you use a variable in your program. In C++, this code will generate an error:
    Code:
    int main()
    {
         num = 10;
         return 0;
    }
    The compiler has to encounter num's declaration before num can be used in the program:

    int num;
    num = 10;

    It works similarly with functions. This will generate an error:
    Code:
    int main()
    {
         myFunc();
    
         return 0;
    }
    so you have to do this:
    Code:
    #include <iostream>
    using namespace std;
    
    void myFunc()
    {
    	cout<<"hello"<<endl;
    }
    
    int main()
    {
    	myFunc();
    
    	return 0;
    }
    However, you can do this as well:
    Code:
    #include <iostream>
    using namespace std;
    
    void myFunc();
    
    
    int main()
    {
    	myFunc();
    
    	return 0;
    }
    The line:

    void myFunc();

    is a promise to an old, grumpy fellow named Mr. Compiler that you will define the function elsewhere. You can define it in the same file or in another file. After Mr. Compiler gets done examining your program, since Mr. Compiler doesn't get around very well anymore, he hands the program over to his friend Mr. Linker. Mr. Linker's job is to make sure you keep the promises you made to Mr. Compiler. So, Mr. Linker goes hunting around all the files in your program looking for a definition of the function myFunc(). If you didn't keep your promise and define the function somewhere in your files, you will get an error.

    when a function is called, does the code automatically keep looking down the code until it finds the first defined version of the function (if not defined already)?
    In C++, there is no "first defined version". In all your files, there can only be one function that matches. If there are two functions that match the function call, then you will get an error. The same is true for variables: you can't define them more than once. This will produce an error:

    int num = 10;
    ...
    ...
    int num = 20;
    Last edited by 7stud; 10-20-2005 at 12:38 PM.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Just for future reference - The "shell" is called a function prototype

    Function Prototype:
    Code:
    void example();
    Function Definition:
    Code:
    example();
    
    void example() {
    cout<<"Example\n";
    }
    The function prototype needs the variable types (the return type, and the arguments passed-in). It does not need the variable names.

    The function definition needs the variable types and the variable names used inside the function.

    Note that the variable names inside the function may be different than the variable names used in the function call(s). The function call passes-in a copy of numerical value, not the actual variable or name.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Location
    Australia
    Posts
    5
    cool thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's question about functions.
    By Crocodile23 in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 07:00 AM
  2. Functions Question
    By audinue in forum C Programming
    Replies: 2
    Last Post: 01-09-2009, 09:39 AM
  3. Replies: 3
    Last Post: 11-04-2006, 01:09 PM
  4. Question regarding recursive functions
    By knj316 in forum C++ Programming
    Replies: 7
    Last Post: 10-01-2006, 11:41 PM
  5. How to Create .H files and use Assembler functions?
    By luisvalencia in forum C Programming
    Replies: 7
    Last Post: 04-10-2005, 11:47 PM