C++: Practice: Functions Comprehensive

Last updated: 2026-08-26

Previous lessons covered various aspects of functions separately, but in real programs they are used together.

In this lesson, we'll practice making functions work together through several comprehensive examples.


1. Example 1: Simple Math Utility Library

(1) 1.1 Requirements Analysis

We'll build a math utility library with the following functions:

  1. int add(int a, int b) — Addition
  2. int subtract(int a, int b) — Subtraction
  3. int multiply(int a, int b) — Multiplication
  4. double divide(int a, int b) — Division (returns floating point)
  5. int power(int base, int exp) — Exponentiation
  6. bool isPrime(int n) — Prime check

▶ Example 1: Math Utility Library (Difficulty ⭐⭐)

CPP 📖 Display only
#include <iostream>
#include <cmath>

// Function prototypes
int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
double divide(int a, int b);
int power(int base, int exp);
bool isPrime(int n);

int main() {
	int x = 10, y = 3;
	
	std::cout << "x + y = " << add(x, y) << std::endl;
	std::cout << "x - y = " << subtract(x, y) << std::endl;
	std::cout << "x * y = " << multiply(x, y) << std::endl;
	std::cout << "x / y = " << divide(x, y) << std::endl;
	std::cout << "x ^ y = " << power(x, y) << std::endl;
	std::cout << "Is x prime? " << (isPrime(x) ? "Yes" : "No") << std::endl;
	
	return 0;
}

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

int subtract(int a, int b) {
	return a - b;
}

int multiply(int a, int b) {
	return a * b;
}

double divide(int a, int b) {
	if (b == 0) {
		return 0.0;
	}
	return static_cast<double>(a) / b;
}

int power(int base, int exp) {
	if (exp < 0) {
		return 0; // Simplified: negative exponents not supported
	}
	int result = 1;
	for (int i = 0; i < exp; i++) {
		result *= base;
	}
	return result;
}

bool isPrime(int n) {
	if (n <= 1) {
		return false;
	}
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0) {
			return false;
		}
	}
	return true;
}
52 logic lines (exceeds 40-line limit, display only)

Output:

TEXT 📖 Display only
x + y = 
x - y = 
x * y = 
x / y = 
x ^ y = 
Is x prime? YesNo

Sample run:

TEXT 📖 Display only
x + y = 13
x - y = 7
x * y = 30
x / y = 3.33333
x ^ y = 1000
Is x prime? No


2. Example 2: Number Guessing Game (Function Version)

(1) 2.1 Requirements Analysis

Rewrite the previous number guessing game using functions:

  1. void printWelcome() — Print welcome message
  2. int getSecretNumber() — Generate secret number
  3. int getGuess() — Get user's guess
  4. void printResult(int guess, int secretNumber) — Print result (too high/too low/correct)
  5. void playGame() — Main game logic

▶ Example 2: Number Guessing Game (Function Version) (Difficulty ⭐⭐)

CPP 📖 Display only
#include <iostream>
#include <cstdlib>
#include <ctime>

// Function prototypes
void printWelcome();
int getSecretNumber();
int getGuess();
void printResult(int guess, int secretNumber);
void playGame();

int main() {
	std::srand(static_cast<unsigned int>(std::time(nullptr)));
	
	char playAgain;
	do {
		playGame();
		std::cout << "Want to play again? (y/n): ";
		std::cin >> playAgain;
	} while (playAgain == 'y' || playAgain == 'Y');
	
	std::cout << "Thanks for playing, goodbye!" << std::endl;
	return 0;
}

// Function definitions
void printWelcome() {
	std::cout << "=========== Number Guessing Game ===========" << std::endl;
	std::cout << "I'm thinking of a number from 1-100, guess it!" << std::endl << std::endl;
}

int getSecretNumber() {
	return std::rand() % 100 + 1;
}

int getGuess() {
	int guess;
	std::cout << "Enter your guess: ";
	std::cin >> guess;
	return guess;
}

void printResult(int guess, int secretNumber) {
	if (guess > secretNumber) {
		std::cout << "Too high! Try again." << std::endl;
	} else if (guess < secretNumber) {
		std::cout << "Too low! Try again." << std::endl;
	} else {
		std::cout << "🎉 Congratulations! You got it!" << std::endl;
	}
}

void playGame() {
	printWelcome();
	int secretNumber = getSecretNumber();
	int guess;
	int attempts = 0;
	
	do {
		guess = getGuess();
		attempts++;
		printResult(guess, secretNumber);
	} while (guess != secretNumber);
	
	std::cout << "You guessed " << attempts << " times." << std::endl << std::endl;
}
50 logic lines (exceeds 40-line limit, display only)

Output:

TEXT 📖 Display only
Want to play again? (y/n):
Thanks for playing, goodbye!
=========== Number Guessing Game ===========
I'm thinking of a number from 1-100, guess it!
Enter your guess:
Too high! Try again.
Too low! Try again.
🎉 Congratulations! You got it!
You guessed  times.


3. Example 3: Simple Calculator (Function Overloading Version)

(1) 3.1 Requirements Analysis

Implement a calculator using function overloading, supporting both int and double types.

▶ Example 3: Calculator (Function Overloading Version) (Difficulty ⭐⭐)

CPP
#include <iostream>
#include <iomanip>

// Function prototypes (overloaded)
int calculate(int a, int b, char op);
double calculate(double a, double b, char op);

int main() {
	int intResult = calculate(10, 3, '+');
	double doubleResult = calculate(10.0, 3.0, '/');
	
	std::cout << "10 + 3 = " << intResult << std::endl;
	std::cout << std::fixed << std::setprecision(2);
	std::cout << "10.0 / 3.0 = " << doubleResult << std::endl;
	
	return 0;
}

// Function definitions (overloaded)
int calculate(int a, int b, char op) {
	switch (op) {
	case '+': return a + b;
	case '-': return a - b;
	case '*': return a * b;
	case '/': return a / b; // Integer division
	default: return 0;
	}
}

double calculate(double a, double b, char op) {
	switch (op) {
	case '+': return a + b;
	case '-': return a - b;
	case '*': return a * b;
	case '/': return b != 0 ? a / b : 0.0;
	default: return 0.0;
	}
}
▶ Try it Yourself

Output:

TEXT 📖 Display only
10 + 3 = 
10.0 / 3.0 = 


4. Practice Exercise: Student Grade Management (Function Version)

▶ Example 4: Student Grade Management (Difficulty ⭐⭐⭐)

CPP 📖 Display only
#include <iostream>
#include <string>

// Function prototypes
void printMenu();
void addStudent(std::string names[], int scores[], int& count);
void printStudents(const std::string names[], const int scores[], int count);
double calculateAverage(const int scores[], int count);
int findMaxScore(const int scores[], int count);

int main() {
	const int MAX_STUDENTS = 100;
	std::string names[MAX_STUDENTS];
	int scores[MAX_STUDENTS];
	int count = 0;
	
	int choice;
	do {
		printMenu();
		std::cout << "Choose (1-4): ";
		std::cin >> choice;
		
		if (choice == 1) {
			addStudent(names, scores, count);
		} else if (choice == 2) {
			printStudents(names, scores, count);
		} else if (choice == 3) {
			if (count == 0) {
				std::cout << "No student data!" << std::endl;
			} else {
				std::cout << "Average: " << calculateAverage(scores, count) << std::endl;
				std::cout << "Highest score: " << findMaxScore(scores, count) << std::endl;
			}
		} else if (choice == 4) {
			std::cout << "Goodbye!" << std::endl;
		} else {
			std::cout << "Error: Please enter a number between 1-4!" << std::endl;
		}
		
		std::cout << std::endl;
		
	} while (choice != 4);
	
	return 0;
}

// Function definitions
void printMenu() {
	std::cout << "=========== Student Grade Management ===========" << std::endl;
	std::cout << "1. Add student" << std::endl;
	std::cout << "2. Show all students" << std::endl;
	std::cout << "3. Statistics" << std::endl;
	std::cout << "4. Exit" << std::endl;
}

void addStudent(std::string names[], int scores[], int& count) {
	if (count >= 100) {
		std::cout << "Maximum number of students reached!" << std::endl;
		return;
	}
	
	std::cout << "Enter name: ";
	std::cin.ignore();
	std::getline(std::cin, names[count]);
	
	std::cout << "Enter score: ";
	std::cin >> scores[count];
	
	count++;
	std::cout << "Added successfully!" << std::endl;
}

void printStudents(const std::string names[], const int scores[], int count) {
	if (count == 0) {
		std::cout << "No student data!" << std::endl;
		return;
	}
	
	std::cout << "=========== Student List ==========" << std::endl;
	for (int i = 0; i < count; i++) {
		std::cout << i + 1 << ". " << names[i] << " - " << scores[i] << std::endl;
	}
}

double calculateAverage(const int scores[], int count) {
	if (count == 0) {
		return 0.0;
	}
	
	int sum = 0;
	for (int i = 0; i < count; i++) {
		sum += scores[i];
	}
	
	return static_cast<double>(sum) / count;
}

int findMaxScore(const int scores[], int count) {
	if (count == 0) {
		return 0;
	}
	
	int maxScore = scores[0];
	for (int i = 1; i < count; i++) {
		if (scores[i] > maxScore) {
			maxScore = scores[i];
		}
	}
	
	return maxScore;
}
87 logic lines (exceeds 40-line limit, display only)

Output:

TEXT 📖 Display only
Choose (1-4):
No student data!
Average:
Highest score:
Goodbye!
Error: Please enter a number between 1-4!
=========== Student Grade Management ===========
1. Add student
2. Show all students
3. Statistics
4. Exit
Maximum number of students reached!
Enter name:
Enter score:
Added successfully!
No student data!
=========== Student List ==========
.  - 

❓ FAQ

Q When should I write code as a function?
A > - Code is repeated 2+ times → write a function > - A code block has a clear purpose → write a function (improves readability) > - Needs to be reused → write a function
Q Are function prototypes required?
A If the function definition comes before main, you don't need a prototype.

📖 Summary


📝 Exercises

  1. Basic (Difficulty ⭐): Rewrite the previous "calculate rectangle area" program as a function version:

    1. Write a function double calculateArea(double width, double height)
    2. Call it in main
  2. Intermediate (Difficulty ⭐⭐): Write a program with the following functions: 3. int fibonacci(int n) — Returns the nth Fibonacci number 4. void printFibonacci(int n) — Prints the first n terms 5. In main, let the user input n and call these two functions

  3. Challenge (Difficulty ⭐⭐⭐): Extend the "Student Grade Management" program with the following features: 6. Delete student (by name or index) 7. Sort by grade (high to low) 8. Save to file / Load from file (optional)


5. 🚀 Next Steps

Congratulations! You've completed all lessons in Phase 2 (Functions). Next, we enter Phase 3 (Arrays, Strings, Structs), learning how to handle multiple values of the same type and C++ strings.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏