Void Functions

Published on: November 1, 2010

In this tutorial, I introduce to you void functions. I've spoken a little about it in my last tutorial regarding Value Returning Functions but here, I show it to you in application. I reuse my Tip Calculator program that I created several months ago and show you how you can use the calculation in that program as a function for the entire program.

Void functions can be fun and they are very handy. I show you how to use it in several ways including one way where you send values back through the parameter list as references and another way where you simply output the results onto the screen because no return statement is needed in the void. If you have any questions about the program or functions in general, feel free to ask.

I hope you enjoy the video and if you have any requests feel free to let me know.

Remember to checkout the Codesnip below to review the C++ code yourself! Feel free to copy the code, but I ask that you please provide credit if you leave the code unchanged when you use it.

Codesnip:

//Programmer: Nazmus
//Program:  Tip Calculator - Void Function Tutorial
//Website:  www.EasyProgramming.net

#include<iostream>
using namespace std;

void tipcalc(int people, float bill, float tip);

int main()
{
 int people; 
 float bill, tip; 

 cout << "How much is the bill? ";
 cin >> bill;
 cout << "How many people will be splitting the bill? ";
 cin >> people; 
 cout << "What is the percentage of the tip? ";
 cin >> tip;

 tipcalc(people, bill, tip);


 system("pause");  

}

void tipcalc(int people, float bill, float tip)
{
 float totaltip, total;

 totaltip = bill * (tip/100.); 
 total = (totaltip + bill)/people;

 cout << endl;
 cout << "The total tip at " << tip << "% is $" << totaltip << '.' << endl;
 cout << "Each person will pay $" << total << '.' << endl;

}



Comments: