c++ function examples

#include <cstdlib>
#include <iostream>

using namespace std;

//Function prototypes
int addition(int a, int b);
int subtraction(int a, int b);
int multiplication(int a, int b);
int division(int a, int b);

int main(int argc, char** argv) {

    int a=10, b=2;
        
   /*
    * Each of the following 4 statements, calls
    * a function and prints the value returned.
    */
    cout << a << "+" << b << "=" << addition(a,b) << "\n";
    cout << a << "-" << b << "=" << subtraction(a,b) << "\n";
    cout << a << "*" << b << "=" << multiplication(a,b) << "\n";
    cout << a << "/" << b << "=" << division(a,b) << "\n";
    return 0;
}

//A function returns the sum of two integers.
int addition(int a, int b)
{
    return a+b;
}

//A function returns the difference of two integers.
int subtraction(int a, int b)
{
    return a-b;
}

//A function returns the multiplication of two integers.
int multiplication(int a, int b)
{
    return a*b;
}

//A function returns the division of two integers.
int division(int a, int b)
{
    return a/b;
}

Output

10+2=12
10-2=8
10*2=20
10/2=5

Search within Codexpedia

Custom Search

Search the entire web

Custom Search