global functions

This is a discussion on global functions within the C++ Programming forums, part of the General Programming Boards category; I will not say how far I have got so far, because i'll proberbly jsut confuse myself. Could somebody please ...

  1. #1
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44

    global functions

    I will not say how far I have got so far, because i'll proberbly jsut confuse myself. Could somebody please clearly explain on how I could get one function that can be called, from any number of files ?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    // header.h
    #ifndef HEADER_H
    #define HEADER_H
    
    void f(); // Prototype
    
    #endif
    Code:
    //header.cpp
    #include "header.h"
    
    // Definition
    void f()
    {
      ...
    }
    Now just include header.h wherever you want to use f, and compile header.cpp with the rest of your source files.
    My best code is written with the delete key.

  3. #3
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44
    As you can quite obviously see, i am a newbie.

    what does this part of the header file mean ? :

    #ifndef HEADER_H
    #define HEADER_H

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    that says that if HEADER_H is not defined then define it. If it is defined ignore to the #endif.

    Look up preprocessor commands in your favourite text or your help files.
    This is a schema for preventing cyclic includes.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44
    ahh, so am I right in saying that it stops er, processing the header file if its included in multipul c files ?

    also, ( im very, very new to this ), can I include functions like :

    Code:
    void f(void)
    {
    << cout " I called this function";
    }
    or, am I only allowed to prototype functions in a .h file ?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >can I include functions like
    You can, but you shouldn't. Headers should contain only declarations.

    >so am I right in saying that it stops er, processing the header file if its included in multipul c files ?
    Preprocessing, actually. If the header was previously included, HEADER_H will be defined and the #ifndef will fail, thus bypassing everything up to the #endif.
    My best code is written with the delete key.

  7. #7
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44
    Excelent, thank you so much for the help. I so glad I didnt get any ' newbie / rtfm ' comments, and got mature and helpfull responses - thanks.

    Ill come here again for my c++ questions, what an excelent support site.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I so glad I didnt get any ' newbie / rtfm ' comments
    We only give those comments to people who deserve them.

    >and got mature and helpfull responses
    Good questions receive good answers.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global functions and OO
    By l2u in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 11:00 AM
  2. Classes and Global Functions
    By d_heyzie in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2007, 07:57 PM
  3. Static functions.... why?
    By patricio2626 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2007, 08:06 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. Global variables, or...
    By Vber in forum C Programming
    Replies: 4
    Last Post: 01-03-2003, 02:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21