C++ Basics · Chapter 8

Functions in C++: Void & Return Types Explained With Examples

Learn how to write reusable, organized code using functions. Master parameters, return values, pass-by-value, pass-by-reference, and function prototypes.

By Arj Updated July 2025 14 min read No prior experience needed
What you'll learn
  • What functions are and why they are essential
  • How to define and call functions in C++
  • How to use parameters and return values effectively
  • The difference between pass-by-value and pass-by-reference
  • How function prototypes help organize code

1What is a function?

A function is a reusable block of code that performs a specific task. It allows you to avoid repeating code and helps keep programs clean, organized, and maintainable.

Every C++ program starts with a function called main(), but you can create your own functions too. They are one of the most fundamental tools in programming.

Why functions matter
Imagine you are calculating the square of a number in five different places. Instead of writing the same code five times, you write it once as a function and call it whenever needed. This is the essence of code reuse.

2Function basics

General syntax:

cpp
return_type function_name(parameter_list) {
    // code block
    return value;
}

Components:

  • return_type – Type of value the function returns (int, double, void, etc.)
  • function_name – Name used to call the function
  • parameter_list – Inputs (if any) that the function receives
  • return – Sends a value back to the caller (omitted for void functions)

3Example 1: A simple function (void)

This function has no parameters and returns no value (void). It simply prints a message.

cpp
#include <iostream>
using namespace std;

void greet() {
    cout << "Welcome to Painless Programming!" << endl;
}

int main() {
    greet();  // function call
    greet();  // call again
    return 0;
}
OutputWelcome to Painless Programming! Welcome to Painless Programming!
Reusability in action
The greet() function is called twice, but the code is written only once. This saves time and reduces errors.

4Example 2: Function with parameters

This function takes a string parameter and prints a personalized greeting.

cpp
void sayHello(string name) {
    cout << "Hello, " << name << "!" << endl;
}

int main() {
    sayHello("Ali");
    sayHello("Sara");
    return 0;
}
OutputHello, Ali! Hello, Sara!

5Example 3: Function with return value

This function takes two integers, adds them, and returns the result.

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

int main() {
    int result = add(5, 3);
    cout << "Sum: " << result << endl;
    return 0;
}
OutputSum: 8
Return type matters
The return type of the function must match the type of the value returned. If the function returns int, the return statement must provide an int value.

6Function declaration and definition

You can declare a function before main() and define it later. This is called a function prototype.

cpp
int multiply(int, int); // declaration (prototype)

int main() {
    cout << multiply(4, 5) << endl;
    return 0;
}

int multiply(int a, int b) { // definition
    return a * b;
}
Output20

Function prototypes are useful when your function is defined after main(). They tell the compiler the function's name, return type, and parameters before the function is called.

7Pass-by-value vs pass-by-reference

Pass-by-value (default)

The function gets a copy of the argument. Changes inside the function do not affect the original variable.

cpp
void change(int x) {
    x = 100;
}

int main() {
    int a = 5;
    change(a);
    cout << a << endl; // Still 5
    return 0;
}
Output5

Pass-by-reference (using &)

The function receives the actual variable. Changes inside the function affect the original variable.

cpp
void change(int &x) {
    x = 100;
}

int main() {
    int a = 5;
    change(a);
    cout << a << endl; // Now 100
    return 0;
}
Output100
When to use each
Use pass-by-value when you do not need to modify the original. Use pass-by-reference when you need to modify the original or when passing large objects to avoid copying overhead.

8Multiple parameters and return types

You can pass as many parameters as needed. The function returns exactly one value, but that value can be any type.

cpp
double average(int x, int y) {
    return (x + y) / 2.0;
}

int main() {
    cout << average(10, 20) << endl;
    return 0;
}
Output15
Returning multiple values
C++ does not allow returning multiple values directly from a function. However, you can simulate it using references, pointers, or structures (which we will cover in later chapters).

9Common mistakes to avoid

1. Forgetting to return a value
A non-void function must return a value of the correct type. Forgetting to do so causes undefined behavior.
2. Using pass-by-value when you need pass-by-reference
If you need to modify the original variable, use a reference parameter (int &x). Otherwise, you are only modifying a copy.
3. Not declaring functions before main()
If you define a function after main(), you must provide a prototype before calling it. Otherwise, the compiler does not know about the function.
4. Incorrect parameter order
When calling a function, the order of arguments must match the order of parameters in the function definition.

10Mini exercises

Try these yourself
  1. Write a function isEven(int x) that returns true if the number is even, and false otherwise.
  2. Create a function factorial(int n) that returns the factorial of n.
  3. Write a function swap(int &a, int &b) that swaps the values of two variables.
  4. Define a function that prints the multiplication table of a given number up to 10.

Hint: For factorial, multiply numbers from 1 to n. For the multiplication table, use a loop inside the function.

11Quiz yourself

Functions Quiz
Question 1 of 10
Coming up next
Memory in C++
Learn about stack vs heap memory, pointers, references, and dynamic memory allocation in C++.
Continue to Chapter 9

Stop wrestling with confusion.

Join thousands of students mastering Computer Science without the academic jargon.

From syntax to systems. We break down the hardest ideas in computer science so you can actually build things.

© 2026 Painless Programming. Built for students.
Scroll to Top