Calculates the average of three input values using function. - C++ Function

C++ examples for Function:Function Creation

Description

Calculates the average of three input values using function.

Demo Code

#include <iostream>
using namespace std;
int calc_av(int num1, int num2, int num3);   //Prototype
int main()/*  ww w . j av  a  2 s.  c  o m*/
{
   int num1, num2, num3;
   int avg;                 // Holds the return value.
   cout << "Please type three numbers (such as 23 54 85) ";
   cin >> num1 >> num2 >> num3;

   avg = calc_av(num1, num2, num3);
   cout << "\n\nThe average is " << avg;     // Print the return value.
   return 0;
}
int calc_av(int num1, int num2, int num3)
{
   int local_avg;  // Holds the average for these numbers.
   local_avg = (num1+num2+num3) / 3;
   return (local_avg);
}

Result


Related Tutorials