------------------------------------------------------------------------------
MC logo
Functional Average II
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Functional Average] [Functional Average II] [Functional Average III] [Default Parameters] [Function Overloading] [Reference Parameters] [Constant References] [Namespaces] [Namespaces I] [Using Namespaces]
average3.cpp
#include <iostream>
using namespace std;

double av(int n1, int n2, int n3)
{
        double sum;

        sum = n1 + n2 + n3;
        return sum / 3.0;
}

void print_it(int a1, int a2, int a3, double av)
{
        cout << "The average of " << a1 << " " << a2 << " " << a3 
             << " is " << av << endl;
}
int main(void)
{
        int n1, n2, n3;

        cout << "Enter: ";
        cin >> n1 >> n2 >> n3;

        print_it(n1, n2, n3, av(n1, n2, n3));
}

As in Java, the return type of void means there is nothing to return.

Some languages distinguish functions (with return values) and procedures (without). A much more rational arrangement. We have C to thank for void.

This code both C and C++.