What You’ll Learn
By the end of this post, you will:
- Understand what functions are and why they’re used
- Learn how to define and call functions in C++
- Use parameters and return values effectively
- Differentiate between pass-by-value and pass-by-reference
What 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 and organized.
Every C++ program starts with a function called main()
— but you can create your own functions too.
Why Use Functions?
Imagine you’re calculating the square of a number in five different places. Instead of writing the same code five times, you can write it once as a function and call it whenever needed.
Function Basics
General Syntax:
return_type function_name(parameter_list) {
// code block
return value;
}
return_type
– Type of value the function returns (int
,void
, etc.)function_name
– Name used to call the functionparameter_list
– Inputs (if any)return
– Sends a value back to the caller
Example 1: A Simple Function
#include <iostream>
using namespace std;
void greet() {
cout << "Welcome to Painless Programming!" << endl;
}
int main() {
greet(); // function call
greet();
return 0;
}
Output:
Welcome to Painless Programming!
Welcome to Painless Programming!
Here, greet()
is a function with no parameters and no return value (void
).
Example 2: Function with Parameters
void sayHello(string name) {
cout << "Hello, " << name << "!" << endl;
}
int main() {
sayHello("Ali");
sayHello("Sara");
return 0;
}
Example 3: Function with Return Value
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 3);
cout << "Sum: " << result << endl;
return 0;
}
Function Declaration and Definition
You can declare a function before main()
and define it later.
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;
}
This is useful when your function is defined after main()
.
Pass-by-Value vs. Pass-by-Reference
Pass-by-Value
Default in C++. The function gets a copy of the argument. Changes don’t affect the original.
void change(int x) {
x = 100;
}
int main() {
int a = 5;
change(a);
cout << a << endl; // Still 5
return 0;
}
Pass-by-Reference
Pass the actual variable using reference (&
). Changes affect the original.
void change(int &x) {
x = 100;
}
int main() {
int a = 5;
change(a);
cout << a << endl; // Now 100
return 0;
}
Multiple Parameters and Return Types
You can pass as many parameters as needed, and return one value.
double average(int x, int y) {
return (x + y) / 2.0;
}
C++ does not allow returning multiple values directly from a function, but you can simulate it using references or structures (covered in later posts).
Quiz: Functions
Mini Exercises
- Write a function
isEven(int x)
that returnstrue
if a number is even. - Create a function
factorial(int n)
that returns the factorial ofn
. - 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.
Common Mistakes to Avoid
- Forgetting to
return
a value in non-void functions - Using pass-by-value when you actually need to modify the original
- Not declaring functions before
main()
if defining them after
Summary
In this post, you learned:
- How to define and use your own functions in C++
- How to use parameters and return values
- The difference between pass-by-value and pass-by-reference
- How functions can simplify and organize your code
What’s Next?
In the next post, we’ll dive into Memory in C++, including stack vs heap memory, pointers, references, and dynamic memory allocation.