# Thread: Function Help..........please

1. ## 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. try some of the tutorials on this site

3. 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.

4. ## 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