⚡ Mastering Inline Functions & Default Arguments in C++
To optimize performance and simplify function signatures, C++ provides two powerful features: **Inline Functions** (which instruct the compiler to insert function code directly at call sites to eliminate call overhead) and **Default Arguments** (which supply automatic fallback values for parameters when callers omit them).
🚀 Inline Functions
- Keyword: Preceded by `inline`.
- Mechanism: Replaces the function call instruction with the actual function body at compile time.
- Use Case: Best suited for small, frequently executed routines (getters, setters, math helpers).
⚙️ Default Arguments
- Mechanism: Assigns default values in function signatures (`int x = 10`).
- Rule: Default arguments must always be assigned from **right to left** (trailing parameters).
- Benefit: Reduces function overloading boilerplate.
📊 End-to-End Inline Expansion & Default Argument Resolution Workflow Diagram
1. Function Declaration
Define `inline` keyword and assign default parameter values.
2. Compile-Time Inspection
Compiler evaluates call site arguments and inline feasibility.
3. Argument Padding
Missing trailing arguments are filled with default fallback values.
6. Zero Overhead Execution
High-speed execution without stack frame overhead.
5. Code Substitution
Inline bodies pasted directly into machine code instructions.
4. Optimization Check
Compiler approves inline expansion for small routines.
#include <iostream>
using namespace std;
// Inline function suggestion for speed optimization
inline int calculateSquare(int x) {
return x * x;
}
int main() {
int val = 12;
int result = calculateSquare(val); // Expanded inline by compiler
cout << "--- Inline Square Calculator ---" << endl;
cout << "Input Value : " << val << endl;
cout << "Squared : " << result << endl;
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
// Default argument for shipping fee is 99.00
double calculateOrderTotal(double itemSubtotal, double shippingFee = 99.00) {
return itemSubtotal + shippingFee;
}
int main() {
double standardOrder = calculateOrderTotal(1499.00); // Uses default shipping (99)
double expressOrder = calculateOrderTotal(1499.00, 299.00); // Overrides shipping
cout << "--- E-Commerce Shipping Calculator ---" << endl;
cout << "Standard Order Total : ₹" << fixed << setprecision(2) << standardOrder << endl;
cout << "Express Order Total : ₹" << expressOrder << endl;
return 0;
}
#include <iostream>
using namespace std;
inline int fastAbsolute(int n) {
return (n < 0) ? -n : n;
}
int main() {
int negativeVal = -48;
int positiveVal = fastAbsolute(negativeVal);
cout << "--- Inline Absolute Value ---" << endl;
cout << "Original Value : " << negativeVal << endl;
cout << "Absolute Value : " << positiveVal << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
void queryDatabase(string tableName, int limit = 50, int offset = 0) {
cout << "Query: SELECT * FROM " << tableName << " LIMIT " << limit << " OFFSET " << offset << endl;
}
int main() {
cout << "--- Database Pagination ---" << endl;
queryDatabase("users"); // Uses default limit (50) and offset (0)
queryDatabase("products", 10); // Custom limit (10), default offset (0)
queryDatabase("orders", 25, 100); // Custom limit and offset
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
inline double celsiusToFahrenheit(double c) {
return (c * 9.0 / 5.0) + 32.0;
}
int main() {
double celsius = 37.5;
double fahrenheit = celsiusToFahrenheit(celsius);
cout << "--- Inline Temperature Converter ---" << endl;
cout << "Celsius : " << fixed << setprecision(1) << celsius << "°C" << endl;
cout << "Fahrenheit : " << fahrenheit << "°F" << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
void registerUser(string username, string role = "Customer", string country = "India") {
cout << "Registered User: " << username << " | Role: " << role << " | Country: " << country << endl;
}
int main() {
cout << "--- User Registration ---" << endl;
registerUser("Aarav Sharma"); // Uses all defaults
registerUser("Priya Singh", "Admin"); // Custom role, default country
registerUser("John Doe", "Moderator", "United States"); // Custom role and country
return 0;
}
#include <iostream>
using namespace std;
inline int findMax(int a, int b) {
return (a > b) ? a : b;
}
int main() {
int x = 85, y = 142;
int maximum = findMax(x, y);
cout << "--- Inline Maximum Finder ---" << endl;
cout << "Comparing " << x << " and " << y << endl;
cout << "Maximum Value: " << maximum << endl;
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
double calculateInterest(double principal, int years, double annualRate = 8.5) {
return (principal * annualRate * years) / 100.0;
}
int main() {
double standardInterest = calculateInterest(100000.00, 3); // Uses default 8.5% rate
double premiumInterest = calculateInterest(100000.00, 3, 7.0); // Custom 7.0% rate
cout << "--- Loan Interest Calculator ---" << endl;
cout << "Standard Interest (8.5%): ₹" << fixed << setprecision(2) << standardInterest << endl;
cout << "Premium Interest (7.0%) : ₹" << premiumInterest << endl;
return 0;
}
#include <iostream>
using namespace std;
inline double calculatePerimeter(double length, double width) {
return 2.0 * (length + width);
}
int main() {
double l = 15.5, w = 8.0;
double perimeter = calculatePerimeter(l, w);
cout << "--- Inline Perimeter Calculator ---" << endl;
cout << "Length: " << l << " | Width: " << w << endl;
cout << "Perimeter: " << perimeter << " units" << endl;
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
// Inline function with default tax rate (5%)
inline double computeTaxedTotal(double subtotal, double gstRate = 0.05) {
return subtotal + (subtotal * gstRate);
}
int main() {
double cartTotal = 3500.00;
double defaultTaxTotal = computeTaxedTotal(cartTotal); // Uses default 5% GST
double luxuryTaxTotal = computeTaxedTotal(cartTotal, 0.18); // Custom 18% GST
cout << "--- Tax Invoice Generator ---" << endl;
cout << "Cart Subtotal : ₹" << fixed << setprecision(2) << cartTotal << endl;
cout << "Total with 5% GST : ₹" << defaultTaxTotal << endl;
cout << "Total with 18% GST : ₹" << luxuryTaxTotal << endl;
return 0;
}