You are here

Introduction of Functions within C++

5 February, 2015 - 12:14

We are going to consider a simple program that might be used for testing a compiler to make sure that it is installed correctly.

Example: Compiler Test.cpp source code

//****************************************************** // Filename: Compiler Test.cpp // Purpose: Average the ages of two people 
// Author: Ken Busbee; © Kenneth Leroy Busbee 
// Date: Jan 5, 2009 
// Comment: Main idea is to be able to 
// debug and run a program on your compiler. //****************************************************** 
// Headers and Other Technical Items 
#include <iostream> 
using namespace std; 
// Function Prototypes 
void pause(void); 
// Variables 
int agel; 
int age2; 
double answer; 
//****************************************************** 
// main 
//****************************************************** 
int main(void) 
{ 
// Input 
cout « "\nEnter the age of the first person --->: 
"; cin » agel; 
cout « "\nEnter the age of the second person -->: 
"; cin » age2; 
// Process 
answer = (agel + age2) 1 2.0; 
// Output 
cout « "\nThe average of their ages is -------->: "; 
cout « answer; 
pause(); 
return 0; 
} 
//****************************************************** 
// pause 
//****************************************************** 
void pause(void) 
{ cout « "\n\n"; system("PAUSE"); cout « "\n\n"; return; } 
//****************************************************** // End of Program //****************************************************** 

This program has two functions, one from each of our categories. The technical layout of functions are the same, it is our distinction that creates the two categories based on how a function is being implemented.