Loading...

Functions in C++

Imagine preparing for a school event. You don’t do everything yourself, you assign responsibilities: one person handles music, another decorates, someone prints banners.
Each helper has a clear task. When needed, you simply “call” them.

Programs work the same way.
Instead of writing long, messy code, you divide tasks into functions, small reusable blocks that do one job well.

Today, your program learns how to organize its work just like a real team.

A Simple Math Helper

You're going to build a small app with two functions:

  • One function adds two numbers
  • Another function prints a formatted result

Here’s the output:

text
4 lines
|
19/ 500 tokens
1
2
3
4
========================
   MATH HELPER
   Sum: 25
========================

You’ll learn how functions are created, called, and how they make programs cleaner and smarter.

Function Declaration and Definition

A function is a named block of code that performs a specific task.
You first declare it (tell the compiler it exists) and then define what it does.
It helps keep code organized and reusable.

Example

cpp
2 lines
|
23/ 500 tokens
1
2
int add(int a, int b); // declaration
int add(int a, int b) { return a + b; } // definition
Code Tools

Arguments and Return Values

Functions can take arguments (inputs) and send back return values (outputs).
Arguments help you pass information in, and return values give results back.

Example

cpp
3 lines
|
11/ 500 tokens
1
2
3
int add(int x, int y) {
    return x + y;
}
Code Tools

Here, x and y are inputs, and the sum is returned.

Scope of Variables

Scope decides where a variable can be used. Variables inside a function exist only inside that function. This keeps each part of your program protected from accidental changes.

Example

cpp
3 lines
|
14/ 500 tokens
1
2
3
void test() {
    int x = 10; // x exists only here
}
Code Tools

Pass-by-Value vs Pass-by-Reference

Pass-by-value sends a copy of the variable, changes do NOT affect the original.
Pass-by-reference sends the actual variable, changes DO affect the original.

Example

cpp
1 lines
|
9/ 500 tokens
1
void change(int &num) { num = 50; }
Code Tools

Here, the & makes the function modify the real value.

Inline Functions

An inline function is expanded where it’s called, avoiding the overhead of a normal function call. Useful for small, frequently used functions to improve performance.

Example

cpp
1 lines
|
11/ 500 tokens
1
inline int square(int n) { return n * n; }
Code Tools

Let’s Build the Math Helper

cpp
23 lines
|
104/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

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

void printResult(int result) {
    cout << "========================" << endl;
    cout << "   MATH HELPER" << endl;
    cout << "   Sum: " << result << endl;
    cout << "========================" << endl;
}

int main() {
    int n1 = 10;
    int n2 = 15;

    int total = add(n1, n2);
    printResult(total);

    return 0;
}
Code Tools

How It Works

  • add() receives two numbers and returns their sum.
  • printResult() displays the formatted output.
  • main() coordinates everything by calling the functions.

Your program is now organized and modular, just like professional code.

Learn Together with AI

Try prompts like:

  • “Explain pass-by-reference with a simple C++ example.”
  • “Create a function that multiplies three numbers.”
  • “Convert my function into an inline function.”

AI can generate new versions, fix errors, or show alternative solutions instantly.

Practice Time

Write a C++ program that:

  1. Creates a function multiply(int a, int b) that returns the product.
  2. Creates another function display(int result) that prints:
text
4 lines
|
21/ 500 tokens
1
2
3
4
========================
   PRODUCT RESULT
   Result: 45
========================
  1. Calls both functions from main().

Use arguments, return values, and clear function structure.

You can ask AI:

“Show me how to pass values to a function in C++.”

Frequently Asked Questions

Functions are small blocks of code that perform specific tasks. They help make programs more organized, readable, and reusable.

Declarations tell the compiler a function exists, while definitions describe what the function actually does. Both are needed for proper structure.

Arguments are values you pass into a function, and return values are what the function sends back. Together, they allow two-way communication.

Pass-by-value sends a copy of the data, changes inside the function don’t affect the original. Pass-by-reference modifies the actual variable.

Use inline functions for very small tasks that run often. They avoid the overhead of a normal function call.

Absolutely. You can write and test all function-based programs directly in the DevsCall AI Code Runner.

Still have questions?Contact our support team