×

Advanced Coding & Software Engineering Program

Duration: 1 Year (12 Months)

Join our premium 1-year program to master cutting-edge technologies and become an industry-ready Software Engineer!

Course Coverage

  • Languages: C, C++, Java, JavaScript, Python
  • Web Technologies: HTML, CSS, Bootstrap 5, MERN Stack, Full Stack Development
  • Databases: MySQL, MongoDB
  • Data Science Libraries: Pandas, NumPy
  • Development Tools: Visual Studio Code, IntelliJ IDEA, PyCharm, Postman, Git, GitHub
  • Cloud Platforms: Vercel, MongoDB Atlas

Program Highlights

  • Live Classes: Interactive sessions with real-time doubt resolution
  • Hands-On Sessions: Practical coding exercises to build real-world skills
  • Industry Experts: Learn from professionals with years of experience
  • Live Project: Work on real-world projects to apply your skills
  • Get Certificate: Earn a professional certificate upon program completion

Course Fee: Only ₹1020 / month
Limited Period Offer!

Functions in C++ – Complete Tutorial



Last Updated on: 9th Dec 2025 16:04:23 PM

1. Introduction to Functions in C++

In programming, many tasks are repeated again and again.
If we write the same code multiple times, the program becomes:

  • Long

  • Hard to read

  • Difficult to maintain

To solve this problem, C++ provides Functions.

 

A function is a block of code designed to perform a specific task, which can be called whenever needed. Functions help in code reusability, modularity, and clean program structure.

 

Every C++ program starts execution from the main() function, but large programs consist of many user-defined functions.

 

What is a Function?

A function in C++ is a self-contained block of statements that performs a specific operation, takes input as parameters (optional), processes the data, and may return a result.

 

Functions help to:

  • Break complex problems into smaller parts

  • Reduce code duplication

  • Improve readability

  • Enhance testing and debugging

 

Why Use Functions?

✔ Code reusability
✔ Easy debugging
✔ Shorter and cleaner code
✔ Modular program structure
✔ Team collaboration becomes easier

 

Types of Functions in C++

C++ provides two main types of functions:

  1. Built-in (Library) Functions

  2. User-defined Functions

 

1. Built-in Functions

Predefined functions provided by C++ libraries.

 

Examples

sqrt(), pow(), strlen(), cout, cin


Example Program

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    cout << sqrt(25);
    return 0;
}


 

2. User-Defined Functions

Functions created by the programmer to perform customized tasks.

 

Structure of a Function

return_type function_name(parameters) {
    // function body
    return value;
}

 

Example

int add(int a, int b) {
    return a + b;
}

 

 Components of a Function 

Every function in C++ consists of:

  1. Function Declaration (Prototype)

  2. Function Definition

  3. Function Call

 

Function Declaration (Prototype)

Tells the compiler about the function before it is used.

Syntax 

return_type function_name(parameter_list);

 

Example

int add(int, int);

 

Function Definition

The function definition contains the actual body of the function, where the logic is written.

Syntax 

return_type function_name(parameter_list)
{
    // function body
    return value;
}

 

Example 

int add(int a, int b) {
    return a + b;
}

 

Function Calling

A function call executes the function by passing required values (arguments).

Syntax

function_name(arguments);

 

Example

int result = add(10, 20);

 

Example: Complete Function Program

#include <iostream>
using namespace std;

int add(int, int);

int main() {
    int sum = add(5, 7);
    cout << "Sum = " << sum;
    return 0;
}

int add(int a, int b) {
    return a + b;
}

 

Output

Sum = 12

 

Types of User-Defined Functions (Based on Arguments & Return Value)

 

1. Function with No Arguments and No Return Value

   Function takes no input and returns no output.

 
void greet() {
    cout << "Hello User!";
}

 

2. Function with Arguments but No Return Value

void printSum(int a, int b) {
    cout << a + b;
}

 

3. Function with Arguments and Return Value

int square(int n) {
    return n * n;
}

 

4. Function with No Arguments but Return Value

int getYear() {
    return 2025;
}

 

 void  Functions

A void function does not return any value.

void show()
{
    cout << "Welcome";
}

 

10. Function Parameters vs Arguments

Term Meaning
Parameter Variables in function definition
Argument Values passed during function call

 

Example 

int add(int a, int b);   // parameters
add(10, 20);            // arguments

 


 

Call by Value & Call by Reference

 


 

11.Call by Value

A copy of the variable is passed to the function. Original value does not change.

 

Example 

void increment(int x)
{
    x++;
}

int main()
{
    int a = 10;
    increment(a);
    cout << a;  // Output: 10
}

 

12. Call by Reference

Address of the variable is passed. Original value changes.

 

Example 

void increment(int &x)
{
    x++;
}

int main()
{
    int a = 10;
    increment(a);
    cout << a;  // Output: 11
}

 

13. Default Arguments

Default values assigned to parameters.

void show(int x = 5)
{
    cout << x;
}

show();     // 5
show(10);   // 10
 

14. Inline Functions

Compiler replaces function call with function code to reduce execution time.

inline int square(int x)
{
    return x * x;
}

 

15. Function Overloading

Multiple functions with same name but different parameters.

int add(int a, int b);
float add(float a, float b);

 

16. Recursive Functions

A function that calls itself to solve a problem.

 

Example: Factorial

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

 

17. Scope of Variables

  • Local Variable – declared inside function

  • Global Variable – declared outside all functions

 

Real-Life Examples of Functions

✅ ATM machine →  withdraw() checkBalance() 
✅ E-Commerce →   addToCart() makePayment() 
✅ Calculator →  add() subtract() 
✅ Bank App →  deposit() transfer() 

 

Best Practices

✔ Use meaningful function names
✔ Keep functions small
✔ Use comments
✔ Avoid global variables
✔ Use pass-by-reference when required

 

Practice Questions

  1. Create a function to find the largest of three numbers

  2. Function to check prime number

  3. Recursive function for Fibonacci

  4. Calculator using functions

  5. Swap two numbers using call by reference

 

21. Conclusion

Functions are the backbone of structured and modular programming in C++.
Every professional C++ program heavily depends on functions.

 

Mastering functions is essential for DSA, competitive programming, and real-world projects.

 

Keep practicing — you're doing amazing!

Happy Coding!    yes


Online - Chat Now
Let’s Connect

Inquiry Sent!

Your message has been successfully sent. We'll get back to you soon!

iKeySkills Logo