1.
Basic Function Definition
A simple function in C++ that adds two integers and returns the result:
#include <iostream>
using namespace std;
// Function prototype (declaration)
int addition(int a, int b);
// Main function
int main() {
int z;
z = addition(5, 3); // Calling the function
cout << "The result is " << z << endl;
return 0;
}
// Function definition
int addition(int a, int b) {
int r;
r = a + b; // Add the two numbers
return r; // Return the result
}
Explanation:
• addition(int a, int b) is a function that takes two integers as arguments, adds
them, and returns the result.
• The main function calls addition(5, 3), which adds 5 and 3 and stores the
result in z.
2. Function with No Return Value (void)
A function that does not return a value but prints a message:
#include <iostream>
using namespace std;
// Function prototype
void printMessage();
// Main function
int main() {
printMessage(); // Calling the function
return 0;
}
// Function definition
void printMessage() {
cout << "Hello, World!" << endl;
}
Explanation:
• The printMessage function is of type void, meaning it does not return any
value. It simply prints a message to the console.
3. Function with Default Parameters
A function where one of the parameters has a default value:
#include <iostream>
using namespace std;
// Function prototype with default parameter
int divide(int a, int b = 2);
// Main function
int main() {
cout << "Result of divide(12): " << divide(12) << endl; // b defaults to 2
cout << "Result of divide(12, 4): " << divide(12, 4) << endl; // b is 4
return 0;
}
// Function definition
int divide(int a, int b) {
int r;
r = a / b; // Divide a by b
return r;
}
Explanation:
• The divide function has a default parameter b with a default value of 2.
• When called with a single argument (divide(12)), the function uses the
default value for b.
4. Recursive Function
A function that calls itself to calculate the factorial of a number:
#include <iostream>
using namespace std;
// Function prototype
int factorial(int n);
// Main function
int main() {
int num = 5;
cout << "Factorial of " << num << " is " << factorial(num) << endl;
return 0;
}
// Recursive function definition
int factorial(int n) {
if (n > 1)
return n * factorial(n - 1); // Recursive call
else
return 1; // Base case
}
Explanation:
• The factorial function uses recursion to calculate the factorial of a number.
• It multiplies n by the factorial of n-1 until n is equal to 1.
5. Function with Reference Parameters
A function where parameters are passed by reference:
#include <iostream>
using namespace std;
// Function prototype
void swap(int &x, int &y);
// Main function
int main() {
int a = 10, b = 20;
cout << "Before swap: a = " << a << ", b = " << b << endl;
swap(a, b); // Calling the function
cout << "After swap: a = " << a << ", b = " << b << endl;
return 0;
}
// Function definition
void swap(int &x, int &y) {
int temp;
temp = x;
x = y;
y = temp;
}
Explanation:
• The swap function uses references to swap the values of a and b.
• Because x and y are references, changes to them affect the original variables
passed to the function.
Summary
• Basic Functions: Perform simple operations and return values.
• Void Functions: Perform operations without returning a value.
• Default Parameters: Allow functions to be called with fewer arguments.
• Recursive Functions: Call themselves to perform repetitive tasks.
• Reference Parameters: Allow functions to modify the original variables.
This code and explanations align with the concepts introduced in your slides,
providing practical examples of how to use functions in C++.