In order to use a c++ function, we need these three steps…

1) Declare the function at the top of the c++ file before it appears in any other functions that call it.

int sum(int first, int second);

2) Write the function it selves within the c++ file.

int sum(int number1, int number2) { return (number1 + number2); }

3) Use it!

cout << "The total is " << sum(number1, number2) << endl;

Below program will call the sum function and outputs the total of the two numbers.

#include <iostream> using namespace std; int sum(int first, int second); int main() { int number1, number2; cout << "enter two number : " << endl; cin >> number1; cin >> number2; cout << "The total is " << sum(number1, number2) << endl; return 0; } int sum(int number1, int number2) { return (number1 + number2); }

Below is another program where the function will repeatedly calling it selves to produce a factorial of a number.

#include <iostream> using namespace std; int factorial(int number); int main() { int number; cout << "enter a number : " << endl; cin >> number; cout << "The factorial of that number is " << factorial(number) << endl; return 0; } int factorial(int number) { if (number <= 1) { return 1; } else { return number * factorial(number - 1); } }

This tutorial is a simple one but that is basically what you need to create a function which can operate by it’s own!