This week in class we are looking at functions and how to use them.

This is the example we got during class:

#include <iostream>
#include <string>
using namespace std;
//function prototype
int getTotal(int,string);
void verifySeasons (int&);  
int main()
{
string name; // name of player
int totalGoals = 0; // total goals for all seasons
int totalAssists = 0; // total assists for all seasons
int seasons = 0; // number of seasons played
char next = 'y';
do
{ // start of do-while
// Have the user input the player's name
cout << "Enter the player's name: ";
getline(cin, name);
// Have the user input the number of seasons
cout << "Enter number of seasons: ";
cin >> seasons;
//call the verifySeasons() function
verifySeasons (seasons);
// call the getTotal() function

totalGoals = getTotal(seasons, "goals");
totalAssists = getTotal(seasons, "assists");
// Use a for loop to input goals and assists for each season

 
cout << endl << endl;
cout << "Name: " << name << endl;
cout << "Total Goals: " << totalGoals << endl;
cout << "Total Assists: " << totalAssists << endl;
cout << "Total Points: " << totalGoals+totalAssists << endl;
cout << "Keep going? (y/n): " << endl;
cin >> next;
// reset totals
totalGoals = 0;
totalAssists = 0;
cin.ignore(10, '\n'); // clear out input buffer
} while (next == 'y' || next == 'Y'); // end of do-while
system("pause");
return 0;
}
int getTotal (int numSeasons, string whichone)
{
int data = 0;
int total = 0;
 
for (int count = 1; count <= numSeasons; count++)
{
// Input goals for a season and update goal total
cout << "Enter " << whichone << " for season #" << count << ": ";
cin >> data;
total = total + data;

}
return total;
}
void verifySeasons (int& numSeasons)
{
while (numSeasons <= 0)
{
cout << "Seasons must be positive - Try again" << endl;
cout << "Enter number of seasons: ";
cin >> numSeasons;
}
}

Basically, takes inputs of a player stats and adds them.
I understand the general concept, however what I don't quite get is how to properly call in the function.

#include <string>
using namespace std;
//function prototype
int getTotal(int,string);
void verifySeasons (int&);  
int main()

Why is "getTotal" int, and verifySeasons void?
Another question I have is what's in the parenthesis.
Why "int, string" for getTotal, and "int&" for verifySeasons?
Thanks for your help!!

Edited 5 Years Ago by juangalvez4: n/a

Let's break it down a bit. A function has four parts:

  1. An identifier
  2. A return type
  3. A parameter list
  4. A body

The first three are combined to create the function's signature, and it looks like this:

<return type> <identifier>(<parameter list>)

The parameter list is enclosed in parentheses simply because that makes it easier your the compiler to determine where the list begins and where it ends. The return type may be void, to say that the function returns nothing.

The body of a function is where the work is done, and it's essentially a compound statement (a series of statements wrapped in braces). To return an actual value from the function, you use a return statement.

When you call a function, you specify the identifier and a list of arguments which match the parameter list.

int add(int a, int b)
{
    return a + b;
}

#include <iostream>

int main()
{
    int sum = add(1, 2);

    std::cout<< sum <<'\n';
}

The previous code defines a function called add (the identifier), with a parameter list containing two ints, and a return type of int. The body of the function adds those two parameters together and returns the sum.

When called, 1 is copied into the parameter called a, and 2 is copied into the parameter called b. After this happens, the body of the function is executed, and the return value is then copied into a new int in main called sum. The execution conceptually looks like this:

// Execute the main function
{
    int sum;

    // Execute the add function
    {
        int a = 1;
        int b = 2;

        sum = a + b;
    }

    // Continue with main
    std::cout<< sum <<'\n';
}

Now for your questions.

Why is "getTotal" int, and verifySeasons void?

Because getTotal is defined to return a value while verifySeasons is not. In your example, verifySeasons is actually an example of using output parameters. I can tell this because the parameter to verifySeasons is a non-const reference, which means that the argument must be an object, and any changes to that object will persist back in the caller. Using the add function as an example, this is equivalent using an output parameter instead of a return value:

void add(int a, int b, int& c)
{
    c = a + b;
}

#include <iostream>

int main()
{
    int sum;

    add(1, 2, sum);

    std::cout<< sum <<'\n';
}

A reference is a synonym for an object. When sum is passed to add by reference, c becomes another way to refer to the same object. This is as opposed to passing by value (as with the a and b parameters) where the parameter is a completely different object with a copy of the value.

So getTotal and verifySeasons follow essentially the same pattern, do something and return the result, just in a different manner. Since verifySeasons handles the "return value" through an output parameter, it's not necessary to have a return value, and thus the return type is void. getTotal does not use output parameters, so the only way to get the result to the caller is with a return value.

Another question I have is what's in the parenthesis.

That's the parameter list. If my first description isn't sufficient, let me know and I'll add to it.

Why "int, string" for getTotal, and "int&" for verifySeasons?

Well, we've established that the int& is an output parameter and functions as the return type for verifySeasons. For getTotal, the int and string parameters are information passed in from the caller to help the function do its job. Most of the time a function will not be self-contained; it needs to get input from the caller, and often return an output back to the caller. These are what the parameter list and return value do, respectively.

Edited 5 Years Ago by Narue: n/a

I would also like to add that the input stream is not checked for errors in the example you have been given, so if you type a letter when it expects a number it will probably cough & die or cough & infinite loop.

This article has been dead for over six months. Start a new discussion instead.