Rabu, 21 Oktober 2015

Recursion - C++

Recursion on C++??

I'm lacking the information on this matter so let's see what other site saying about Recursion:

"What is recursion? The simple answer is, it’s when a function calls itself."

And you might want to read this article about Array first:


Due to my very limited understanding about Recursion, it might be better if we just take a look at this:

Now with recursion, we won’t need to use a ‘for loop’ because we will set it up so that our function calls itself. Let’s recreate this same program one more time, only this time we will do it without a ‘for loop’. We will use a recursion loop instead, like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

void numberFunction(int i) {
  cout << "The number is: " << i << endl;
  i++;
  if(i<10) {
    numberFunction(i);
  }
}

int main() {

int i = 0;
numberFunction(i);

return 0;
}


We did it! We used recursion! You can see the call to ‘numberFunction’ is made only once in the main part of the program but it keeps getting called again and again from within the function itself, for as long as ‘i’ is less than 10.

Array C++

What is Array?? (C++ style)

Array is a variable that store a bunch of data that have the same type. Each data to occupy the different memory locations or addresses and further called with array element. These array element  can be accessed through index from inside it. But, It is very important to take a look that in array index always start from 0, not 1. Below here is a picture how array works.

1st  Value
2nd Value
….
Nth Value
   Array Element Value
1st Address
2nd Address
Nth Address
   Array Element Address
0
1
N
   Array Element Index

To declare an array in C++ , we must use bracket [ ]. The common form of the declaration is below here:

data_type array_name[element_amount];

As example, if we want to declare an array (for example, with the name ARRAY) which have 25 elements with data type int, therefore the declaration form is looks like the one below:

int ARRAY[25];

Memory room that's required to declare this array is about 100 byte, that's comes from 25 x 4 (4 is size from data type int). The way which is used to access the element is with write the index. For example we want to take a value that is in the 10th element and store the value into a variable with type int too (example, X), then we need to write the code like the below:

X=ARRAY[9]

Why 9, not 10? Remember, index array always start from 0 so that to access the 10th element, then the index we need is 10 - 1, that is 9. 

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);