Practical Application for C++ Programming: Functions

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

In this practical lesson, you will write a C++ program that creates a function; this function will pass values both by value and by reference. You will write, run, and test your code.

Lesson Overview & Knowledge Required

To complete this lesson, you should be able to write, compile, and execute a C++ program. You should be able to declare a function and create a basic function that accepts and returns parameters. An understanding of pass-by-reference, pass-by-value, and overloaded function is also important.

Program Code

The following code asks the user for five scores, each time passing that entry to a function called set_scores, which in turn uses that score to add to the running total. Notice how the variable total is set as static. This means that its value will NOT be reset each time the function is called. There are two variables at the end of the main function: You'll be using those for the code application.

#include <iostream>
using namespace std;
int set_scores(int s);
int main() {
 int menu = 1;
 do {
  cout << "Enter score number " << menu << ": ";
  int score;
  cin >> score;
  cout << endl;
 } while(menu <= 5);
 int batter_a = 275;
 int batter_b = 397;
 return 0; 
int set_scores(int s) {
 static int total;
 total += s;
 cout << "Total so far = " << total;

Recursive Function

Consider that our two batters are now playing in the outfield. They've grown bored and want to play a little game of Tower of Hanoi: They set up disks in the field and start playing. In order to help them with their game, write a recursive function to play with 3 disks.

First, add the function declaration before the main function:

void towerOfHanoi(int disks, string s1, string s2, string s3);

Next, add the function itself.

void towerOfHanoi(int disks, string begin, string temp, string end) {
 if(disks == 1) {
  cout << begin << " ---->" << end << endl;
 } else {
  towerOfHanoi(disks - 1, begin, end, temp);
  cout << begin << " ---->" << end << endl;
  towerOfHanoi(disks - 1, temp, begin, end);

Code Application

There are two parts to the code application, listed as follows:

New Function

For the code application, you will be creating another function called switch_batters. In this function you will need to complete the following:

  • You will pass the batter_a and batter_b variables, by reference, to switch_batters.
  • In the function, switch the values for batter a and batter b.
  • Ensure the program compiles
  • When run, batter A should have a value of 397 and batter B's value should be 275

Call Recursive Function

Finally, before the end of the main function, add a call to the recursive function.

Follow-Up Questions

Now that you have created a function to pass by reference, let's do a little more with C++ functions. Perform the following tasks:

  • Overload the set_scores function to accept a modifier as well as the score.
  • The set_scores function is pass-by-value: What does this mean for the parameter(s) that was passed?
  • Can you add a third parameter to the swap function, but only pass this parameter by value?
  • In a recursive function, what happens if you do not provide an exit path of the function?

Answer Key

Below are the answers to the Code application section and the follow-up questions.

Code Application

Before you can use a new function, you will need to declare it before the main function. The following code is added before main.

void switch_batters(int &a, int &b);

In the main function, after the two batter variables, add the code to call the function and display the values.

swap(batter_a, batter_b);
cout << "Swapping: A is now " << batter_a << endl;
cout << "Swapping: B is now " << batter_b << endl;

Finally, here is the code for the function itself.

void switch_batters(int &a, int &b) {
  int temp;
  temp = a;
  a = b;
  b = temp;

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account