📌 Function Examples in C++

 


Functions in C++ allow code reusability by defining blocks of code that can be called multiple times. Here are some important function examples in C++:


1️⃣ Hello World using Function

📌 A simple function to print "Hello World!"

#include <iostream>
using namespace std;

// Function definition
void sayHello() {
    cout << "Hello, World!" << endl;
}

int main() {
    sayHello();  // Function call
    return 0;
}

🔹 Output

Hello, World!

2️⃣ Call by Value & Reference in C++

📌 Call by Value: The function gets a copy of the argument.
📌 Call by Reference: The function gets the actual variable reference.

#include <iostream>
using namespace std;

// Call by Value
void callByValue(int x) {
    x = x + 10;
    cout << "Inside Call by Value: " << x << endl;
}

// Call by Reference
void callByReference(int &x) {
    x = x + 10;
    cout << "Inside Call by Reference: " << x << endl;
}

int main() {
    int num = 5;

    callByValue(num);
    cout << "After Call by Value: " << num << endl;

    callByReference(num);
    cout << "After Call by Reference: " << num << endl;

    return 0;
}

🔹 Output

Inside Call by Value: 15
After Call by Value: 5
Inside Call by Reference: 15
After Call by Reference: 15

3️⃣ Function Overloading in C++

📌 Multiple functions with the same name but different parameters.

#include <iostream>
using namespace std;

// Function Overloading
void display(int x) {
    cout << "Integer: " << x << endl;
}
void display(double x) {
    cout << "Double: " << x << endl;
}
void display(string x) {
    cout << "String: " << x << endl;
}

int main() {
    display(10);
    display(3.14);
    display("Hello");

    return 0;
}

🔹 Output

Integer: 10
Double: 3.14
String: Hello

4️⃣ Function Recursion Example in C++

📌 A function that calls itself to find the factorial of a number.

#include <iostream>
using namespace std;

int factorial(int n) {
    if (n == 0)
        return 1;
    else
        return n * factorial(n - 1);
}

int main() {
    int num = 5;
    cout << "Factorial of " << num << " is " << factorial(num) << endl;
    return 0;
}

🔹 Output

Factorial of 5 is 120

5️⃣ Arithmetic Operations using Function

📌 Addition, Subtraction, Multiplication, and Division using functions.

#include <iostream>
using namespace std;

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) { return a / b; }

int main() {
    int x = 10, y = 5;

    cout << "Addition: " << add(x, y) << endl;
    cout << "Subtraction: " << subtract(x, y) << endl;
    cout << "Multiplication: " << multiply(x, y) << endl;
    cout << "Division: " << divide(x, y) << endl;

    return 0;
}

🔹 Output

Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2

6️⃣ Check Prime Number using Function

📌 A function to check whether a number is prime or not.

#include <iostream>
using namespace std;

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;
}

int main() {
    int num = 17;
    if (isPrime(num))
        cout << num << " is a Prime Number" << endl;
    else
        cout << num << " is not a Prime Number" << endl;

    return 0;
}

🔹 Output

17 is a Prime Number

7️⃣ Tables using Function in C++

📌 Function to print multiplication tables.

#include <iostream>
using namespace std;

void printTable(int num) {
    for (int i = 1; i <= 10; i++) {
        cout << num << " x " << i << " = " << num * i << endl;
    }
}

int main() {
    int number = 5;
    printTable(number);
    return 0;
}

🔹 Output

5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50

8️⃣ Display Marksheet using Function in C++

📌 Function to calculate total, percentage, and grade.

#include <iostream>
using namespace std;

void displayMarksheet(string name, int m1, int m2, int m3) {
    int total = m1 + m2 + m3;
    float percentage = total / 3.0;
    char grade;

    if (percentage >= 90) grade = 'A';
    else if (percentage >= 75) grade = 'B';
    else if (percentage >= 50) grade = 'C';
    else grade = 'F';

    cout << "Student: " << name << endl;
    cout << "Total Marks: " << total << endl;
    cout << "Percentage: " << percentage << "%" << endl;
    cout << "Grade: " << grade << endl;
}

int main() {
    displayMarksheet("Alice", 85, 78, 92);
    return 0;
}

🔹 Output

Student: Alice
Total Marks: 255
Percentage: 85%
Grade: B

📌 Summary

✔ Functions increase code reusability.
Call by Value vs. Call by Reference affects how values are passed.
Function Overloading allows multiple functions with the same name.
Recursion is useful for factorials and other iterative processes.
Functions make complex programs modular and organized.

🚀 Need more examples? Let me know! 🔥

Post a Comment

0 Comments