Void Functions

Write the function void readTest(double & exam, double & tavge) the does the following:
a) print the prompt "enter exam score:" than read the exam score
b) print the prompt "enter all test scores:" then read 7 test scores in a loop, and then computer their average
c)the exam score and the average of the test score are returned to the calling function using the reference parameters exam and tavge respectively.


why wont my program average the test scores?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#define MAXCOUNT 7
using namespace std;


    int studID;  // the students ID number
    int MaxCount;  // the number of exam scores up to 7
    double avgScore;  // the average number of exam scores
int main ()
{
    cout <<  "Enter Student ID:"; 
    cin >> studID;
    /****************definition of the function readTestScores()***************/
    /************************read scores and compute their average*************/
    void readTestScores ();
    {
         double score,  // a student's test score
                totalScore;  // a student's total test score
         int count;  // to hold a student's test score
         totalScore=0;
         count =0;
    cout << "enter exam score:";
    cin  >> score;
    
    /***********************read scores and compute their total****************/
    while (count < MaxCount)
    {
          cin >> score;
          totalScore = totalScore + score;
          count = count +1;
    }
    cout  <<  "Enter all test scores:";
    cin  >>  totalScore; 
    /*******************compute the average score******************************/
    avgScore = totalScore/ MaxCount;
    
    
    double tAvgScore;  // the average of the tests
    cout  << endl  <<  "Test Average is:";
    MaxCount = 7;
    readTestScores();
    tAvgScore = avgScore;
}
system ("PAUSE");
return (0);
}

Last edited on
You have a fundamental misunderstanding of functions. There is no such thing as a "void function". There is, however, such a thing as a function which returns nothing.

As for your problem, you're trying to define the function inside of main - you can't define a function inside another function like that. Separate them, break it up.
Last edited on
isnt a void function when you do:
void computeAreaPeri (void)
{
area= len * width;
peri= 2*(len + width);
}

and than you use void computeAreaPeri() when ever you want to use those problems?
What your tutor is getting at is a function that returns nothing but yet the results of the function are assigned to the variables in the argument list that are passed by reference, hence the calling function can receive the results back.

BTW - you can't declare a function inside another function. See the function:

 
void readTestScores ();


inside your
 
int main()


If I were to defclare a function foo that take an int as its only argument and returns nothing I would declare it as:

 
void foo(int a);


The function source is then written:

1
2
3
4
void foo(int a)
{
   // some code
}


But if I wanted a function to return a value:

1
2
3
4
5
6
int foo()
{
   int val(0);
   // some code, where 'val' is potentially updated...
   return val; // return result
}


When I want to use the code, say in my main function:

1
2
3
4
5
6
7
int main()
{
   int n = foo();
   // code...

   return 0;  // return ok signal back to OS
}


Does that help?
got me more confussed
i did declare my void function into my main function
You are being asked to do somthing like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void readTest(double& exam, double& tavge);

int main()
{
	double exam, tavge;

	// do parts (a) and (b) of question here

	// part (c):
	readTest(exam, tavge);
	
	return 0;
}

void readTest(double& exam, double& tavge)
{
	// calculate average scores, and return result in arg
}
see i didnt think thaat i had to write the double & exam etc inside the () i thought i could leave it blank
what is the calling function?
what is the calling function?


It's the function in which you use the called function. In the above example, it's main() that is calling readTest(...)
baketballcourt wrote:
isnt a void function when you do:
void computeAreaPeri (void)
{
area= len * width;
peri= 2*(len + width);
}

and than you use void computeAreaPeri() when ever you want to use those problems?
No, that is a function that does not return anything. The term "void function" is wrong because it encourages an incorrect way of thinking about functions.
Topic archived. No new replies allowed.