C++ – Functions

C++ – Functions

Functions allow to structure programs in segments of code to perform individual tasks.

Functions with type

Working samples:


// function example
#include <iostream>
using namespace std;

// integer - name of the function - data
int addition (int a, int b)
{
  int r;
  r=a+b;
  return r;
}

int main ()
{
  int z;
  z = addition (5,3);
  cout << "The result is " << z; // the result is 8
}


// function example
#include <iostream>
using namespace std;

int subtraction (int a, int b)
{
  int r;
  r=a-b;
  return r;
}

int main ()
{
  int x=5, y=3, z;
  z = subtraction (7,2);
  cout << "The first result is " << z << '\n';
  cout << "The second result is " << subtraction (7,2) << '\n';
  cout << "The third result is " << subtraction (x,y) << '\n';
  z= 4 + subtraction (x,y);
  cout << "The fourth result is " << z << '\n';
}

Functions with no type – void


// void function example
#include <iostream>
using namespace std;

void printmessage ()
{
  cout << "I'm a function!"; // it prints 'I'm a function!'
}

int main ()
{
  printmessage ();
}

Arguments passed by reference

// passing parameters by reference
#include <iostream>
using namespace std;

void duplicate (int& a, int& b, int& c)
{
  a*=2;
  b*=2;
  c*=2;
}

int main ()
{
  int x=1, y=3, z=7;
  duplicate (x, y, z);
  cout << "x=" << x << ", y=" << y << ", z=" << z;
  return 0;
}

The result:
x=2, y=6, z=14

By |C++, Video Games Development|Commenti disabilitati su C++ – Functions

C++ – For Statement

C++ – For Statement

Working samples:


// countdown using a for loop
#include <iostream>
using namespace std;

int main ()
{
  for (int n=10; n>0; n--) {
    cout << n << ", ";
  }
  cout << "liftoff!\n";
}

The result:

10, 9, 8, 7, 6, 5, 4, 3, 2, 1, liftoff!


// break loop example
#include <iostream>
using namespace std;

int main ()
{
  for (int n=10; n>0; n--)
  {
    cout << n << ", ";
    if (n==3)
    {
      cout << "countdown aborted!";
      break;
    }
  }
}

The result:
10, 9, 8, 7, 6, 5, 4, 3, countdown aborted!


// continue loop example
#include <iostream>
using namespace std;

int main ()
{
  for (int n=10; n>0; n--) {
    if (n==5) continue;
    cout << n << ", ";
  }
  cout << "liftoff!\n";
}

The result:
10, 9, 8, 7, 6, 4, 3, 2, 1, liftoff!

By |C++, Video Games Development|Commenti disabilitati su C++ – For Statement

C++ – Do While Statement

C++ – Do While Statement

Working sample:


// custom countdown using while
#include <iostream>
using namespace std;

int main ()
{
  int n = 10;

  while (n>0) {
    cout << n << ", ";
    --n;
  }

  cout << "liftoff!\n";
}

The result:

10, 9, 8, 7, 6, 5, 4, 3, 2, 1, liftoff!


// echo machine
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str;
  do {
    cout << "Enter text: ";
    getline (cin,str);
    cout << "You entered: " << str << '\n';
  } while (str != "goodbye");
}

The result:

Enter text: hello
You entered: hello
Enter text: who’s there?
You entered: who’s there?
Enter text: goodbye
You entered: goodbye
Process returned 0

By |C++, Video Games Development|Commenti disabilitati su C++ – Do While Statement

C++ – Switch Statement

C++ – Switch Statement

Working samples:

...
switch (expression)
{
  case constant1:
     group-of-statements-1;
     break;
  case constant2:
     group-of-statements-2;
     break;
  .
  .
  .
  default:
     default-group-of-statements
}
...
By |C++, Video Games Development|Commenti disabilitati su C++ – Switch Statement

C++ – If Statement

C++ – If Statement

if (x == 100)
  cout << "x is 100";
...
if (x == 100) { cout << "x is "; cout << x; }
...
if (x == 100)
  cout << "x is 100";
else
  cout << "x is not 100";
...
if (x > 0)
  cout << "x is positive";
else if (x < 0)
  cout << "x is negative";
else
  cout << "x is 0";
...

Working sample:


#include <iostream>
using namespace std;

int main ()
{
int x = 3;

if (x > 0)
  cout << "x is positive";
else if (x < 0)
  cout << "x is negative";
else
  cout << "x is 0";
return 0;
}

By |C++, Video Games Development|Commenti disabilitati su C++ – If Statement