5. + Void Functions – Example 1
#include <iostream>
using namespace std;
function prototype
void printStars(int blanks, int starsInline);
int main (int argc, const char * argv[]) :
{ void printStars(int blanks, int starsInLine) {
int noOfLines; int count;
int counter;
int noOfBlanks; for (count=1; count <= blanks; count++)
cout << ' ';
cout << "Enter the number of star lines to be for (count=1; count <=starsInLine; count++)
printed: "; cout << "* ";
cin >> noOfLines; cout << endl;
cout << endl; }
noOfBlanks = noOfLines;
for (counter=1; counter <=noOfLines;
counter++) {
printStars(noOfBlanks, counter); function call
noOfBlanks--;
} function definition
return 0;
}
:
6. + Void Functions
#include <iostream>
using namespace std;
formal parameters (value)
void printStars(int blanks, int starsInline);
int main (int argc, const char * argv[]) :
{ void printStars(int blanks, int starsInLine) {
int noOfLines; int count;
int counter;
int noOfBlanks; for (count=1; count <= blanks; count++)
cout << ' ';
cout << "Enter the number of star lines to be for (count=1; count <=starsInLine; count++)
printed: "; cout << "* ";
cin >> noOfLines; cout << endl;
cout << endl; }
noOfBlanks = noOfLines;
for (counter=1; counter <=noOfLines;
counter++) {
printStars(noOfBlanks, counter); actual parameters
noOfBlanks--;
}
return 0;
}
:
7. +
Void Functions– Example 1
#include <iostream>
using namespace std;
void printStars(int blanks, int starsInline);
int main (int argc, const char * argv[])
{ noOfLines noOfBlanks
int noOfLines;
int counter;
int noOfBlanks;
cout << "Enter the number of star lines to be printed: ";
cin >> noOfLines; counter
cout << endl;
noOfBlanks = noOfLines;
for (counter=1; counter <=noOfLines; counter++) {
printStars(noOfBlanks, counter);
noOfBlanks--;
}
return 0;
}
:
8. +
Void Functions– Example 1
#include <iostream>
using namespace std;
void printStars(int blanks, int starsInline);
int main (int argc, const char * argv[])
{ noOfLines noOfBlanks
int noOfLines;
int counter; 10
int noOfBlanks;
cout << "Enter the number of star lines to be printed: ";
cin >> noOfLines; counter
cout << endl;
noOfBlanks = noOfLines;
for (counter=1; counter <=noOfLines; counter++) {
printStars(noOfBlanks, counter);
noOfBlanks--;
}
return 0;
}
:
9. +
Void Functions – Example 1
#include <iostream>
using namespace std;
void printStars(int blanks, int starsInline);
int main (int argc, const char * argv[])
{ noOfLines noOfBlanks
int noOfLines;
int counter; 10 10
int noOfBlanks;
cout << "Enter the number of star lines to be printed: ";
cin >> noOfLines; counter
cout << endl;
noOfBlanks = noOfLines;
for (counter=1; counter <=noOfLines; counter++) {
printStars(noOfBlanks, counter);
noOfBlanks--;
}
return 0;
}
:
10. +
Void Functions – Example 1
#include <iostream>
using namespace std;
void printStars(int blanks, int starsInline);
int main (int argc, const char * argv[])
{ noOfLines noOfBlanks
int noOfLines;
int counter; 10 10
int noOfBlanks;
cout << "Enter the number of star lines to be printed: ";
cin >> noOfLines; counter
cout << endl;
noOfBlanks = noOfLines;
1
for (counter=1; counter <=noOfLines; counter++) {
printStars(noOfBlanks, counter);
noOfBlanks--;
}
return 0;
}
:
11. +
Void Functions – Example 1
:
void printStars(int blanks, int starsInLine) {
int count; blanks starsInLine
10 1
for (count=1; count <= blanks; count++)
cout << ' ';
for (count=1; count <=starsInLine; count++)
cout << "* "; count
cout << endl;
}
12. +
Void Functions – Example 1
:
void printStars(int blanks, int starsInLine) {
int count; blanks starsInLine
10 1
for (count=1; count <= blanks; count++)
cout << ' ';
for (count=1; count <=starsInLine; count++)
cout << "* "; count
cout << endl;
} 11
Output:
13. +
Void Functions – Example 1
:
void printStars(int blanks, int starsInLine) {
int count; blanks starsInLine
10 1
for (count=1; count <= blanks; count++)
cout << ' ';
for (count=1; count <=starsInLine; count++)
cout << "* "; count
cout << endl;
} 2
Output:
*
14. +
Void Functions – Example 1
#include <iostream>
using namespace std;
void printStars(int blanks, int starsInline);
int main (int argc, const char * argv[])
{ noOfLines noOfBlanks
int noOfLines;
int counter; 10 9
int noOfBlanks;
cout << "Enter the number of star lines to be printed: ";
cin >> noOfLines; counter
cout << endl;
noOfBlanks = noOfLines;
1
for (counter=1; counter <=noOfLines; counter++) {
printStars(noOfBlanks, counter);
noOfBlanks--;
}
return 0;
}
:
15. +
Void Functions – Example 1
#include <iostream>
using namespace std;
void printStars(int blanks, int starsInline);
int main (int argc, const char * argv[])
{ noOfLines noOfBlanks
int noOfLines;
int counter; 10 9
int noOfBlanks;
cout << "Enter the number of star lines to be printed: ";
cin >> noOfLines; counter
cout << endl;
noOfBlanks = noOfLines;
2
for (counter=1; counter <=noOfLines; counter++) {
printStars(noOfBlanks, counter);
noOfBlanks--;
}
return 0;
}
:
16. +
Void Functions – Example 1
:
void printStars(int blanks, int starsInLine) {
int count; blanks starsInLine
9 2
for (count=1; count <= blanks; count++)
cout << ' ';
for (count=1; count <=starsInLine; count++)
cout << "* "; count
cout << endl;
}
17. +
Void Functions – Example 1
:
void printStars(int blanks, int starsInLine) {
int count; blanks starsInLine
9 2
for (count=1; count <= blanks; count++)
cout << ' ';
for (count=1; count <=starsInLine; count++)
cout << "* "; count
cout << endl;
} 10
Output:
*
18. +
Void Functions – Example 1
:
void printStars(int blanks, int starsInLine) {
int count; blanks starsInLine
10 1
for (count=1; count <= blanks; count++)
cout << ' ';
for (count=1; count <=starsInLine; count++)
cout << "* "; count
cout << endl;
} 3
Output:
*
**
19. +
Void Functions – Example 1
… and so on until….
20. +
Void Functions – Example 1
printStars(0, 10);
:
void printStars(int blanks, int starsInLine) {
int count; blanks starsInLine
0 10
for (count=1; count <= blanks; count++)
cout << ' ';
for (count=1; count <=starsInLine; count++)
cout << "* "; count
cout << endl;
} 11
21. +
Void Functions – Example 1
Final Output:
*
**
***
****
*****
******
*******
********
*********
**********
22. + Void Functions – Reference Parameters
(Example 2)
#include <iostream> value parameter
using namespace std;
void getScore(int& score);
void printGrade(int cScore); :
void printGrade(int cScore) {
int main (int argc, const char * argv[]) cout << "Your grade for the course is ";
{ if (cScore >= 85)
int courseScore; cout << "A." << endl;
cout << "This program determines the grade for else if (cScore >= 65)
a given score." << endl; cout << "B." << endl;
getScore(courseScore); else if (cScore >= 55)
cout << "Score is " << courseScore << endl; cout << "C." << endl;
printGrade(courseScore); else if (cScore >= 45)
return 0; cout << "D." << endl;
} else
cout << "F." << endl;
void getScore(int& score) { }
cout << "Enter score: ";
cin >> score;
}
: reference parameter
23. + Void Functions – Reference Parameters
(Example 2)
#include <iostream>
using namespace std;
void getScore(int& score);
void printGrade(int cScore); This program determines the grade for a given score
int main (int argc, const char * argv[])
{
int courseScore;
cout << "This program determines the grade for
a given score." << endl;
getScore(courseScore);
courseScore
cout << "Score is " << courseScore << endl;
printGrade(courseScore);
return 0;
}
void getScore(int& score) {
cout << "Enter score: ";
cin >> score;
}
:
24. + Void Functions – Reference Parameters
(Example 2)
#include <iostream>
using namespace std;
void getScore(int& score);
void printGrade(int cScore); This program determines the grade for a given score
int main (int argc, const char * argv[])
{
int courseScore;
cout << "This program determines the grade for
a given score." << endl;
getScore(courseScore);
courseScore
cout << "Score is " << courseScore << endl;
printGrade(courseScore);
return 0;
}
void getScore(int& score) {
cout << "Enter score: ";
cin >> score;
}
: score
25. + Void Functions – Reference Parameters
(Example 2)
#include <iostream>
using namespace std;
void getScore(int& score);
void printGrade(int cScore); This program determines the grade for a given score
Enter score:
int main (int argc, const char * argv[])
{
int courseScore;
cout << "This program determines the grade for
a given score." << endl;
getScore(courseScore);
courseScore
cout << "Score is " << courseScore << endl;
printGrade(courseScore);
return 0;
}
void getScore(int& score) {
cout << "Enter score: ";
cin >> score;
}
: score
26. + Void Functions – Reference Parameters
(Example 2)
#include <iostream>
using namespace std;
void getScore(int& score);
void printGrade(int cScore); This program determines the grade for a given score
Enter score: 70
int main (int argc, const char * argv[])
{
int courseScore;
cout << "This program determines the grade for
a given score." << endl;
getScore(courseScore);
courseScore
cout << "Score is " << courseScore << endl;
printGrade(courseScore); 70
return 0;
}
void getScore(int& score) {
cout << "Enter score: ";
cin >> score;
}
: score
27. + Void Functions – Reference Parameters
(Example 2)
#include <iostream>
using namespace std;
void getScore(int& score);
void printGrade(int cScore); This program determines the grade for a given score
Enter score: 70
int main (int argc, const char * argv[]) Score is 70
{
int courseScore;
cout << "This program determines the grade for
a given score." << endl;
getScore(courseScore);
courseScore
cout << "Score is " << courseScore << endl;
printGrade(courseScore); 70
return 0;
}
void getScore(int& score) {
cout << "Enter score: ";
cin >> score;
}
:
28. + Void Functions – Reference Parameters
(Example 2)
#include <iostream>
using namespace std;
void getScore(int& score);
void printGrade(int cScore); This program determines the grade for a given score
Enter score: 70
int main (int argc, const char * argv[]) Score is 70
{
int courseScore;
cout << "This program determines the grade for
a given score." << endl;
getScore(courseScore);
courseScore
cout << "Score is " << courseScore << endl;
printGrade(courseScore); 70
return 0;
}
void getScore(int& score) {
cout << "Enter score: ";
cin >> score;
}
:
29. + Void Functions – Reference Parameters
(Example 2)
: This program determines the grade for a given score
void printGrade(int cScore) { Enter score: 70
cout << "Your grade for the course is "; Score is 70
if (cScore >= 85)
cout << "A." << endl;
else if (cScore >= 65)
cout << "B." << endl;
else if (cScore >= 55) cScore
cout << "C." << endl;
else if (cScore >= 45) 70
cout << "D." << endl;
else
cout << "F." << endl;
}
30. + Void Functions – Reference Parameters
(Example 2)
: This program determines the grade for a given score
void printGrade(int cScore) { Enter score: 70
cout << "Your grade for the course is "; Score is 70
if (cScore >= 85) Your grade for the course is
cout << "A." << endl;
else if (cScore >= 65)
cout << "B." << endl;
else if (cScore >= 55) cScore
cout << "C." << endl;
else if (cScore >= 45) 70
cout << "D." << endl;
else
cout << "F." << endl;
}
31. + Void Functions – Reference Parameters
(Example 2)
: This program determines the grade for a given score
void printGrade(int cScore) { Enter score: 70
cout << "Your grade for the course is "; Score is 70
if (cScore >= 85) Your grade for the course is B.
cout << "A." << endl;
else if (cScore >= 65)
cout << "B." << endl;
else if (cScore >= 55) cScore
cout << "C." << endl;
else if (cScore >= 45) 70
cout << "D." << endl;
else
cout << "F." << endl;
}
32. + Value-Returning Functions – Example 3
#include <iostream> function returns an int
using namespace std;
int getScore();
void printGrade(int cScore); :
void printGrade(int cScore) {
int main (int argc, const char * argv[]) cout << "Your grade for the course is ";
{ if (cScore >= 85)
int courseScore; cout << "A." << endl;
cout << "This program determines the grade for else if (cScore >= 65)
a given score." << endl; captures "B." << endl; returned
cout << the value
courseScore = getScore(); else if (cScore >= 55)
cout << "Score is " << courseScore << endl; cout << "C." << endl;
printGrade(courseScore); else if (cScore >= 45)
return 0; cout << "D." << endl;
} else
cout << "F." << endl;
int getScore() { }
int score; statement to return value
cout << "Enter score: ";
cin >> score;
return score;
}
:
33. + Value-Returning Functions – Example 3
#include <iostream>
using namespace std;
int getScore();
void printGrade(int cScore); This program determines the grade for a given score
int main (int argc, const char * argv[])
{
int courseScore;
cout << "This program determines the grade for
a given score." << endl;
courseScore = getScore();
courseScore
cout << "Score is " << courseScore << endl;
printGrade(courseScore);
return 0;
}
int getScore() {
int score;
cout << "Enter score: ";
cin >> score;
return score;
}
:
34. + Value-Returning Functions – Example 3
#include <iostream>
using namespace std;
int getScore();
void printGrade(int cScore); This program determines the grade for a given score
int main (int argc, const char * argv[])
{
int courseScore;
cout << "This program determines the grade for
a given score." << endl;
courseScore = getScore();
courseScore
cout << "Score is " << courseScore << endl;
printGrade(courseScore);
return 0;
}
int getScore() {
int score;
cout << "Enter score: ";
cin >> score;
return score; score
}
:
35. + Value-Returning Functions – Example 3
#include <iostream>
using namespace std;
int getScore();
void printGrade(int cScore); This program determines the grade for a given score
Enter score: 67
int main (int argc, const char * argv[])
{
int courseScore;
cout << "This program determines the grade for
a given score." << endl;
courseScore = getScore();
courseScore
cout << "Score is " << courseScore << endl;
printGrade(courseScore);
return 0;
}
int getScore() {
int score;
cout << "Enter score: ";
67
cin >> score;
return score; score
}
:
36. + Value-Returning Functions – Example 3
#include <iostream>
using namespace std;
int getScore();
void printGrade(int cScore); This program determines the grade for a given score
Enter score: 67
int main (int argc, const char * argv[])
{
int courseScore;
cout << "This program determines the grade for
a given score." << endl;
courseScore = getScore();
courseScore
cout << "Score is " << courseScore << endl;
printGrade(courseScore); 67
return 0;
}
int getScore() {
int score;
cout << "Enter score: ";
67
cin >> score;
return score; score
}
:
37. + Value-Returning Functions – Example 3
#include <iostream>
using namespace std;
int getScore();
void printGrade(int cScore); This program determines the grade for a given score
Enter score: 67
int main (int argc, const char * argv[]) Score is 67
{
int courseScore;
cout << "This program determines the grade for
a given score." << endl;
courseScore = getScore();
courseScore
cout << "Score is " << courseScore << endl;
printGrade(courseScore); 67
return 0;
}
int getScore() {
int score;
cout << "Enter score: ";
cin >> score;
return score;
}
:
38. + Value-Returning Functions – Example 3
#include <iostream>
using namespace std;
int getScore();
void printGrade(int cScore); This program determines the grade for a given score
Enter score: 67
int main (int argc, const char * argv[]) Score is 67
{ Your grade for the course is B.
int courseScore;
cout << "This program determines the grade for
a given score." << endl;
courseScore = getScore();
courseScore
cout << "Score is " << courseScore << endl;
printGrade(courseScore); 67
return 0;
}
int getScore() {
int score;
cout << "Enter score: ";
cin >> score;
return score;
}
:
39. +
References:
D.S. Malik (2012). C++ Programming: Program Design
Including Data Structures (5th ed), Thomson Course
Technology.
Chapter 6 – User-Defined Functions I
Chapter 7 – User-Defined Functions II