Function Help..........please

This is a discussion on Function Help..........please within the C++ Programming forums, part of the General Programming Boards category; anyone know of a good tutorial or webpage to help me out with functions? i got the basics down.........its just ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    25

    Cool Function Help..........please

    anyone know of a good tutorial or webpage to help me out with functions? i got the basics down.........its just when it comes to making a program with like 2 or more somehow i get messed up an either it dont work or outputs werid stuff.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    try some of the tutorials on this site
    I am a C++ newb
    using: Visual C++ 6.0
    thanx for any Help

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's a basic rundown:

    Functions require the following:

    1) a return type
    2) a name
    3) arguments
    4) a function body, enclosed in { }

    Thus:

    void myFunc( void ) { cout << "Yo. This is a valid function." << endl; }
    int myFun( int x ) { cout << "X is " << x << endl; return x; }

    Etc...

    Keep in mind, a function has to know of another function before it can use it. Do do this, you must either:

    a) declare the function before a function calls it
    b) define the function before a function calls it
    c) declare the function in the body of the function that wishes to call it

    Consider:

    void fun1( void ) { cout << "Hi!"; }
    void fun2( void ) { fun1( ); }

    This works, because 'fun1' is defined before 'fun2' calls it.
    If we had 'fun2' before 'fun1', then 'fun2' would not know of 'fun1'.

    We could simply reference the function (declare it) first:

    void fun3( void );
    void fun4( void );
    void fun5( void );

    ... then later on we actually 'define' the functions:

    void fun3( void )
    {
    fun5( ); //valid because we've defined them above
    }

    void fun4( void )
    {
    fun3( ); //valid for the same reason
    }

    void fun5( void )
    {
    cout << "Whee...." << endl;
    }

    Now then, let's say we haven't declared or defined 'fun6' yet, but we wanted to have 'fun8' use it. We could do:
    Code:
    void fun8( void )
    {
        void fun7( void ); //declare it locally so fun8 can call it
    
        fun7( ); //now we can use it assuming it exists some place
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    remember

    use int func(); instead of void func(); if you want to return a value. If you put the function definition at the top of the program, you dont need to declare any prototype.

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, 04:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM

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