Thread: how do I function?

  1. #1
    Registered User mik's Avatar
    Join Date
    Oct 2001
    Posts
    15

    Question how do I function?

    how do I seperate my program into functions to simplify it for other programmers?

    e.g. is this right?

    #include....

    int....
    float....

    void main()
    {

    printname();

    }//end main

    void printname()
    {
    cout<<"my name is ...";
    }//end



    if so I have a larger program which doesnt work, why, what could I be doing wrong?

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    You've got the right idea, but you have to declare a function prototype before you call the function. There are two ways to do this. You can put all your called functions before main (programs always start with main, so it doesn't matter if you put it at the end). The other way is to declare just the prototype first.
    Code:
    #include....
    
    void printname();   // Add this line
     
    int..
    float....
     
    void main()
    {
       printname();
    }                //end main 
    
    void printname() 
    { 
       cout<<"my name is ..."; 
    }                //end
    If you passed any values to printname, include the types in the protoptype:
    void printname(int, int, char);
    Then make sure you pass the appropriate types when you call printname:
    printname(a, b, c); // call printname
    .
    .
    .
    void printname(int x, int y, char z) // printname function
    {
    cout << x << y << z;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM