Home » » Declaring Functions

Declaring Functions

Written By Unknown on Minggu, 22 Januari 2012 | 00.36

Lab RPL .
In a similar fashion to variables, functions need to be declared in order to be used. There are a few years of doing this.

Method 1 : Declare First , Define Later

#include <iostream>
using namespace std;
void triple (int a, int b);
int main ()
{
         //Some Code

         return 0 ;

}
Remember "Revirwing C++" Has many more example  and topics .
void triple (int a , int b)
{
          a  = a * 3;
          b = b * 3;
}

As you will observe , the trip (...) function was declare between the main ( ) function and the heading of the program.
It was then define in full after the main cuntion. These are called Header Lines for a fui
function as they occur in the head of the program. This is the preferred was of declaring functions but there is another .

Method 2 : Define In Full
#include <iostream.h>
void triple (int a, int b)
{
a = a *  3 ;
b = b * 3 ;
}
int main ( )
{
        // some code
return 0;
}

In this method, the function is declared fully BEFORE the main ( ) function .
This Is Dangerous primarily because of other functions. As an Example, say that you have another function call inside the triple (...) function and you did not declare it ABOVE the triple (...) function,you are in trouble. C++ will not know where the other function is in memory ! try to use methode number 1 when declaring functions.

0 komentar:

Posting Komentar