A function is a block of code that performs a specific task and can be reused whenever needed.
- Avoids writing the same code again and again.
- Makes programs simple and easy to understand by breaking into smaller parts.
- Accepts input (parameters) and gives output.
- Finding and fixing errors become easier.

Built-in functions
- These predefined functions provided by programming languages or libraries to perform common tasks such as mathematical calculations, input/output, or string operations.
- For example, the sqrt() function from the C++ <cmath> library calculates the square root of a number.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// built-in function
double result = sqrt(25.0);
cout << "Square Root: " << result << endl;
return 0;
}
Output
Square Root: 5
User-defined
- These functions are functions written by programmers to perform specific tasks required in a program.
- They help organize code and allow reuse of logic whenever needed.
#include <iostream>
using namespace std;
void greet() {
cout << "Hello, World!" << endl;
}
int main() {
greet(); // calling user-defined function
return 0;
}
Output
Hello, World!
Function Visualizer
Where are Functions Used in DSA?
In DSA, functions are used to implement algorithms and solve problems in a structured and reusable way.
- Implement algorithms like searching and sorting.
- Handle recursion (like factorial, tree traversal).
- Break problems into smaller subproblems.
- Operate on data structures like arrays, linked lists, trees, and graphs.
- Make code clean and easy to understand.