calling functions, prototypes

Hello all... I am new to c++, and I am trying to figure this out. I have to call on 4 functions in a program, and I can not use any Global variables at all.

The first function is to be used to collectInfo() of string and integer values, and the second is to be used to showInfo() gathered. determineAverage(), DisplayWorstAverage Where I am stuck is,I am being told to call this from the main function. Is using a prototype for these functions different than declaring a global variable before the main function?

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

// prototypes here ?

int main()
{

string bowlerName[10];
int bowlerScore[10];

double totalScore = 0;
double averageScore = 0;
int i = 0;

// or call all 4 functions here?

}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int foo(); // this is a function prototype; it's also a declaration

int bar; // this is a global variable

int main()
{
    bar = foo();  /* function foo() is being called and the return value
                     is being stored in global variable bar */
}

int foo()
{
    // foo's code goes here; this is a function definition
}

The prototype is not needed if the function is defined before being called, because a definition is also a declaration:

1
2
3
4
5
6
7
8
9
10
11
int bar;

int foo()
{
    // foo's code goes here
}

int main()
{
    bar = foo();
}
Thank you very much for taking the time to help me. I am less than a month into this c++ business, and I really do want to make sense of this !!

So just to recap, if I say :


#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

string collectInfo() ;
void showInfo();
double determineAverage();
void DisplayWorstAverage (); // this declares my functions

int main()
{

string bowlerName[10];
int bowlerScore[10];

double totalScore = 0;
double averageScore = 0;
results = collectInfo(); // calls function and stores value in results

}
collectInfo ()
// code goes here

Thanks again : )
Please use [ code ] [ /code ] tags (minus the spaces) to post code. In the above snippet, you forgot to declare results (it must be a variable of type string, global or not). And when you define it, you must write the function signature again:

1
2
3
4
5
6
7
8
9
10
11
12
13
string collectInfo(); // declaration

// some code

int main()
{
    // code
}

string collectInfo()  // definition
{
    // code goes here
}
Declaraing a function prototype is completely different from defining a global variable.
Topic archived. No new replies allowed.