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.
- 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.
2Function basics
General syntax:
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 functionparameter_list– Inputs (if any) that the function receivesreturn– Sends a value back to the caller (omitted forvoidfunctions)
3Example 1: A simple function (void)
This function has no parameters and returns no value (void). It simply prints a message.
#include <iostream> using namespace std; void greet() { cout << "Welcome to Painless Programming!" << endl; } int main() { greet(); // function call greet(); // call again return 0; }
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.
void sayHello(string name) { cout << "Hello, " << name << "!" << endl; } int main() { sayHello("Ali"); sayHello("Sara"); return 0; }
5Example 3: Function with return value
This function takes two integers, adds them, and returns the result.
int add(int a, int b) { return a + b; } int main() { int result = add(5, 3); cout << "Sum: " << result << endl; return 0; }
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.
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; }
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.
void change(int x) { x = 100; } int main() { int a = 5; change(a); cout << a << endl; // Still 5 return 0; }
Pass-by-reference (using &)
The function receives the actual variable. Changes inside the function affect the original variable.
void change(int &x) { x = 100; } int main() { int a = 5; change(a); cout << a << endl; // Now 100 return 0; }
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.
double average(int x, int y) { return (x + y) / 2.0; } int main() { cout << average(10, 20) << endl; return 0; }
9Common mistakes to avoid
void function must return a value of the correct type. Forgetting to do so causes undefined behavior.
int &x). Otherwise, you are only modifying a copy.
main(), you must provide a prototype before calling it. Otherwise, the compiler does not know about the function.
10Mini exercises
- Write a function
isEven(int x)that returnstrueif the number is even, andfalseotherwise. - Create a function
factorial(int n)that returns the factorial of n. - Write a function
swap(int &a, int &b)that swaps the values of two variables. - 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.
