In this step, you'll learn about function prototypes and how to use header files to organize and declare functions in C++. Function prototypes declare the function's name, return type, and parameters without providing the implementation (the body of the function). By using header files, you separate the interface of a function (its declaration) from its implementation. This separation improves code organization, making it easier to maintain and reuse functions in multiple parts of your program or in multiple programs.
Open the WebIDE and create three files in the ~/project
directory:
First, create math_functions.h
:
touch ~/project/math_functions.h
Add the following code to math_functions.h
:
#ifndef MATH_FUNCTIONS_H
#define MATH_FUNCTIONS_H
// Function prototypes for mathematical operations
int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
double divide(double a, double b);
#endif // MATH_FUNCTIONS_H
.h
files are used for declarations, containing function prototypes and other declarations but not implementations. This way, you can declare functions without implementing them. The #ifndef
, #define
, and #endif
directives are called include guards, and they prevent multiple inclusions of the header file, which can cause errors.
Next, create math_functions.cpp
:
touch ~/project/math_functions.cpp
Add the following code to math_functions.cpp
:
#include "math_functions.h"
// Function implementations
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(double a, double b) {
// Check for division by zero
if (b == 0) {
return 0;
}
return a / b;
}
This .cpp
file contains the actual implementations of the functions that were declared in the header file.
Finally, create main.cpp
:
touch ~/project/main.cpp
Add the following code to main.cpp
:
#include <iostream>
#include "math_functions.h"
int main() {
// Demonstrate function calls from header file
int x = 10, y = 5;
std::cout << "Addition: " << x << " + " << y << " = " << add(x, y) << std::endl;
std::cout << "Subtraction: " << x << " - " << y << " = " << subtract(x, y) << std::endl;
std::cout << "Multiplication: " << x << " * " << y << " = " << multiply(x, y) << std::endl;
std::cout << "Division: " << x << " / " << y << " = " << divide(x, y) << std::endl;
return 0;
}
This main.cpp
file includes the math_functions.h
header file, which makes the function prototypes available. It can then use the implemented functions in math_functions.cpp
.
Compile the program using multiple source files:
g++ math_functions.cpp main.cpp -o math_operations
./math_operations
Example output:
Addition: 10 + 5 = 15
Subtraction: 10 - 5 = 5
Multiplication: 10 * 5 = 50
Division: 10 / 5 = 2
Let's create another example with a more complex header file:
Create calculator.h
:
touch ~/project/calculator.h
Add the following code to calculator.h
:
#ifndef CALCULATOR_H
#define CALCULATOR_H
class Calculator {
public:
// Function prototypes for calculator operations
int add(int a, int b);
int subtract(int a, int b);
int calculate(int a, int b, char operation);
};
#endif // CALCULATOR_H
This header file declares a class called Calculator
and its public methods.
Create calculator.cpp
:
touch ~/project/calculator.cpp
Add the following code to calculator.cpp
:
#include "calculator.h"
int Calculator::add(int a, int b) {
return a + b;
}
int Calculator::subtract(int a, int b) {
return a - b;
}
int Calculator::calculate(int a, int b, char operation) {
switch (operation) {
case '+': return add(a, b);
case '-': return subtract(a, b);
default: return 0;
}
}
This calculator.cpp
provides the implementations for the methods declared in the calculator.h
header file.
Create calculator_main.cpp
:
touch ~/project/calculator_main.cpp
Add the following code to calculator_main.cpp
:
#include <iostream>
#include "calculator.h"
int main() {
Calculator calc;
std::cout << "Calculator Operations:" << std::endl;
std::cout << "10 + 5 = " << calc.calculate(10, 5, '+') << std::endl;
std::cout << "10 - 5 = " << calc.calculate(10, 5, '-') << std::endl;
return 0;
}
This main file uses the Calculator
class and performs operations.
Compile the calculator program:
g++ calculator.cpp calculator_main.cpp -o calculator
./calculator
Example output:
Calculator Operations:
10 + 5 = 15
10 - 5 = 5
Key points about function prototypes and header files:
- Header files (.h) declare function prototypes, classes, and other declarations. They act as an interface, outlining what functions are available.
- Source files (.cpp) implement the actual functions declared in the header files. They contain the code for how the functions work.
#ifndef
, #define
, and #endif
(include guards) prevent multiple inclusions of the same header file, which avoids potential compilation errors.
- Using header files promotes modularity and code reusability.
- Header files allow you to separate the "what" (declarations) from the "how" (implementations).
- They facilitate code organization, making it easier to maintain and understand.
You can think of header files like a restaurant menu. The menu (header) lists what's available, while the kitchen (source file) prepares the actual dishes.