1
Posted November 20, 2012 by Victor in C/C++ Programming
 
 

How to use functions in C++ programming

function-fx-x2
function-fx-x2

C++ is an Object Oriented Programming (OOP) and functions, helps in ensuring that proper and easy access to all the C++ programming structures are enabled. What is a function? In C++, a function refers to a group of statements which are executed upon program calls. The meaning of functions will be understood more deeply as we look into how to use C++ functions.

Parts of a C++ Function

Any function in C++  is written using the format below:

Type name (parameters)

{

Statements;

}

Where:

Type: – This refers to the data type of the data which will be returned by the given function. The data type can either be a character (initialized as char), Integer (initialized as int) or a float variable (initialized as float).

Name: This is the special identifier which you will use to call your function. The name of any C++ function will vary depending on your preference. E.g, I can have an interger type function with my name int victor (parameters).

Parameters:- This are the data type specifiers and identifiers and are similar to the normal C++ variable declarations. E.g int victor (int x, int y) meaning the integer function called victor has two variables of integer types (x & Y).

Statements: This are the bodies of any C++ functions. These block of statements covered by {} braces determine the action of the function.

Writing Your First Function

Now that you know the parts of a function and how they are written, let us use our above example to write your first function. This function will be able to add two numbers when called and will be named victor. Here is how to do it (note that // is used for comments):

//my first function for adding numbers

#include <iostream>

using namespace std;

//performing function declarations

int victor (int x, int y)

{

  //writing function statements

  int a;

  a=x+y;

  return a;

}

From the above example, the function named victor is of integer type and has two integer variables. These two variables (x,y) are added in the function body and stored in another variable named (a). i.e a=x+y. Finally, once the addition has taken place, the function is told to return (a) meaning to give an output of what is stored in variable a.

Performing Function calls

Now that you know how to write C++ functions, you have to learn how to use them in your C++ program. Functions cannot be used without calling them. Remember that the main reason as to why functions are often used is to avoid bulky repetition of codes.

For instance, every time you need two numbers to be added, instead of writing a whole line of codes to add the two numbers, you will simply call our function above that has the ability to add two numbers. Here is now you perform the function call:

int main ()

{

  int b;

  b = victor (2,8);

  cout << "The result is " << b;

  return 0;

}

In the above example, once you have declared an integer variable b and you want to add two numbers, you will call on the function we had written known as victor to perform the calculation. Instead of writing victor (int x, int y), you will write numbers 2 and 8 to be placed in the variables x and y and be calculated.

Writing a complex function

The example above is a simple function for addition and how it is called. Even when functions are complex, they follow a similar pattern. For instance, here is an example of a more complex function. Note the way it is written and called similar to our example above:

// In this example, there are two functions created named yourage and goodbye. The program begins executing from the main function (void main()). You will be asked if you wish to know your age after which the function yourage() which had been earlier created is called if you say yes. If you do not wish to know your age, another function named goodbye is called to usher you out of the system. However, if you do not wish to logout, the function your age is called to help you calculate your age.

#include <iostream>

using namespace std;

void yourage ()

{

  int x;

  int y;

  int c;

  cout << "When were you born?";

  cin >> x;

  cout << "What is the current year?";

  cin >> y;

  c = (y - x);

  cout << "Your age is:"<< c;

}

void goodbye()

{

  int b;

  cout << "Do you want to logout?\n 1. YES \n 2. NO \n__";

  cin >> b;

  if (b == 1)

    cout << "Thank you for using our system. Goodbye\n";

  else

    yourage ();
   
}

int main ()

{

  int a;

  cout << "Do you want to know your age?\n 1. YES \n 2. NO \n__";

  cin >> a;

  if (a == 1)

  {

    yourage();

    return 0;

  }

  else

  {

    goodbye();

    return 0;

  }

}

Next time you are writing your C++ programs, why not use functions as they will make coding a lot easier for you.


Victor