Commonly Used C++ Function Names and Keywords
1. Commonly Used C++ Function Names
sum
Calculates the sum of two or more numbers.
Example:
int sum(int a, int b) {
return a + b;
factorial
Calculates the factorial of a number.
Example:
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
isPrime
Checks if a number is prime.
Example:
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= n / 2; i++)
if (n % i == 0) return false;
return true;
}
gcd
Finds the greatest common divisor of two numbers.
Example:
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
return a;
sort
Sorts an array or list of elements.
Example:
#include <algorithm>
void sortArray(int arr[], int n) {
std::sort(arr, arr + n);
2. Commonly Used C++ Keywords with Explanations
if
Conditional statement that executes code if a condition is true.
Example:
if (a > b) {
// code to execute if true
}
for
Looping statement that iterates a set number of times.
Example:
for (int i = 0; i < 10; i++) {
// code to repeat
while
Looping statement that repeats code while a condition is true.
Example:
while (x < 10) {
// code to repeat
x++;
return
Exits a function and optionally provides a value back to the caller.
Example:
int add(int a, int b) {
return a + b;
switch
Selects code to execute based on the value of an expression.
Example:
switch (choice) {
case 1: // code for case 1; break;
case 2: // code for case 2; break;
default: // default code