Selasa, 20 Oktober 2015

Function on C++

Function in C++ For Beginner

Function is a bunch of statement that is grouped into one part of the code to do some specific task. In that way, the code only are defined once, but can be use repeatedly without the need to rewrite the same code again. In other words, function is a subprogram and used to help the program to become more modular that easy to understand and can be reused, both for the program itself or the other program that have the same process.

In the most program language (including C++), function differ into 2, those are "User Defined Function" and  "Built-in Function". User Defined Function are functions which are define by itself, while Built-in Function are 'ready to use' functions which are provided by the compiler.

Function Without Return Value

To do this, we need to create a function type, Void, which mean does not have any return value.

The common form from this function creation without return value is looks like below:

void name_of_function (parameter1, parameter2, ... ){
   Will-do_Statement;
   ....
}

The common form for using the function without return value (that is already defined before) is looks like this:

name_of_function(parameter_value1, parameter_value2,...);

as example of the usage, here we will create a function that able to write text "Procon Daisuki" 10 times. Below here is the program code that is needed to do such thing.

Program Code 1-1

#include <iostream>

using namespace std;

//Create function with a name of Write10times()
void Write10times() {
   for (int C=0; C<10; C++) {
      cout<<"Procon Daisuki"<<endl;
   }
}

//The main function in C++ program
int main() {

   //Use funtion Write10times()
   //For Execution
   Write10times();

   return 0;
}

The result of the above program will be showed like this:

Procon Daisuki
Procon Daisuki
Procon Daisuki
Procon Daisuki
Procon Daisuki
Procon Daisuki
Procon Daisuki
Procon Daisuki
Procon Daisuki
Procon Daisuki

Function with Return Value

Different compared to the one with Type Void, this function is used for doing some process which can return some a value. In making this function we must define the data type from the value which will be returned.

This is the common form of the function creation which has the return value.

data_type name_of_function (parameter1, parameter2,...) {
   will-do_statement;
   ...
   return value_that_will_be_returned;
}

The value that is returned can be stored into a certain variable that have the same data type with connected function.

name_of_variable = name_of_function (parameter1, parameter2, ... );

Other than that, the use of function could be done directly using command cout, if the need is want to show the value that is produced by the function.

cout<<name_of_function(parameter1, parameter2, ...);

To be more understand the creation, take a look at code below:

int Plus(int a, int b) {
   return (a + b);
}

The function usage above can be done using example code below:

int x, y;
// Store the value that is returned by function
// into variable
x = Plus(9, 10);

// Shows directly value that is returned by function\
cout<<Plus(90, 9);


Tidak ada komentar:

Posting Komentar