Defining & Calling
Functions in C++
Learn how to define reusable blocks of code, pass parameters, and use return types to build modular programs.
What Is a Function?
A function is a named, self-contained block of code that performs a specific task. Instead of writing the same logic repeatedly, you write it once inside a function and call it whenever needed.
Anatomy of a Function
Why Functions Matter
Without functions, programs become long, repetitive, and hard to debug. Functions let you isolate logic, test pieces independently, and collaborate with other programmers.
The Function Syntax Rule
returnType functionName(paramType param1, paramType param2) { // function body: statements that do the work return value; // send a result back to the caller }
int, double, void).return.Understanding Return Types
The return type tells the compiler what kind of data the function produces. If a function doesn't produce a value, use void.
intdoublebooltrue or false for yes/no decisions.voidParameters vs Arguments
How a Function Call Works
void vs Non-void Functions
| void Function | Non-void Function | |
|---|---|---|
| Purpose | Performs an action (side effect) | Computes and returns a value |
| Return statement | Optional (or return;) |
Required with a value |
| Can assign to variable? | No | Yes |
| Example | void greet() |
int add(int a, int b) |
How to Write a Function
void.return if non-void.Example 1: Square a Number
int square(int n) { return n * n; } int main() { int result = square(7); cout << result; // Output: 49 return 0; }
Example 2: Max of Two Values
int maxVal(int a, int b) { if (a > b) { return a; } return b; } int main() { cout << maxVal(12, 8); // Output: 12 cout << maxVal(3, 15); // Output: 15 return 0; }
Example 3: A void Function
void printLine(string msg, int times) { for (int i = 0; i < times; i++) { cout << msg << endl; } } int main() { printLine("Hello!", 3); // Prints "Hello!" three times return 0; }
Declaration vs Definition
// Declaration (prototype) - tells compiler the function exists int add(int a, int b); int main() { cout << add(2, 3); // Works because of the prototype above return 0; } // Definition - provides the actual body int add(int a, int b) { return a + b; }
Common Mistakes
int foo() but forgetting the return statement. The compiler may warn, and the result is undefined.string from a function declared as int. The return type and actual value must match.add(5) when it expects two parameters. Every parameter needs a matching argument.int x = printMsg(); when printMsg is void. You can't store "nothing".Summary Cheat Sheet
| Concept | Meaning | Example |
|---|---|---|
| Return type | Data type of the output | int, void |
| Parameter | Placeholder in definition | int x |
| Argument | Actual value at call site | 5 |
| Prototype | Forward declaration | int add(int, int); |
| Function call | Execute the function | add(2, 3) |
Question 1 of 5
What is the return type of the following function?void displayScore(int score) { ... }
Question 2 of 5
Given the function:double half(double x) { return x / 2.0; }
What does half(10.0) return?
x. The body computes \( 10.0 \div 2.0 = 5.0 \) and returns it.Question 3 of 5
Which line is a valid function prototype (declaration)?
Question 4 of 5
What is wrong with this code?int getAge() { cout << 25; }
int, so it must return an integer value. Printing to the console with cout is not the same as returning. It needs return 25;.Question 5 of 5
In the call greet("Alice", 3), what are "Alice" and 3 called?