- C++ Basics
- C++ Introduction
- C++ Installation
- C++ Syntax
- C++ Hello World
- C++ Comments
- C++ Variables
- C++ Data Types
- C++ Constants
- C++ Type Casting
- C++ Inline
- C++ File Inclusion
- C++ Date & Time
- C++ Return Types
- C++ Object Oriented
- C++ Classes
- C++ Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Exceptions
- C++ Advanced
- C++ Conditions
- C++ Loops
- C++ Functions
- C++ Structures
- C++ Enums
- C++ References
- C++ Pointers
- C++ Data Structures
- C++ Libs
- C++ Data Structures
- C++ Arrays
- C++ Vectors
- C++ Lists
- C++ Linked List
- C++ Deque
- C++ Stacks
- C++ Queues
- C++ Priority Queues
- C++ Sets
- C++ Maps
- C++ Unordered Sets and Maps
- C++ Graphs
C++ Functions
In C++, functions are blocks of code that perform a specific task and can be reused throughout a program. Functions help organize code, improve readability, and allow for reuse of logic.
Basic Structure of a Function
A C++ function has the following general structure:
cppreturn_type function_name(parameters) {
// function body
// logic of the function
return value; // (optional, depending on the return type)
}
return_type: Specifies the type of value the function returns. If the function does not return any value,voidis used.function_name: The name of the function. It should be meaningful and follow the naming conventions.parameters: Input values the function needs to perform its task. These are optional and can be multiple.return value: If the function returns a value, it uses thereturnstatement.
Example 1: Function without Parameters and Return Value
cpp#include
using namespace std;
// Function declaration
void greet() {
cout << "Hello, World!" << endl;
}
int main() {
greet(); // Function call
return 0;
}
- Explanation:
- The function
greet()has no parameters and returns no value (voidreturn type). - It simply prints "Hello, World!" when called.
- The function
Example 2: Function with Parameters
cpp#include
using namespace std;
// Function declaration
void printSum(int a, int b) {
int sum = a + b;
cout << "Sum: " << sum << endl;
}
int main() {
printSum(5, 10); // Passing arguments
return 0;
}
- Explanation:
- The
printSum(int a, int b)function takes two integer parameters (aandb), adds them, and prints the result.
- The
Example 3: Function with a Return Value
cpp#include
using namespace std;
// Function declaration
int multiply(int x, int y) {
return x * y;
}
int main() {
int result = multiply(4, 5); // Call the function and store the result
cout << "Product: " << result << endl;
return 0;
}
- Explanation:
- The
multiply()function takes two integers and returns their product. - The returned value is stored in the
resultvariable and printed.
- The
Function Overloading
C++ supports function overloading, which allows multiple functions to have the same name but different parameter types or numbers.
Example of Function Overloading:
cpp#include
using namespace std;
// Function with two integer parameters
int add(int a, int b) {
return a + b;
}
// Function with two double parameters
double add(double a, double b) {
return a + b;
}
int main() {
cout << "Sum of integers: " << add(3, 4) << endl; // Calls int version
cout << "Sum of doubles: " << add(3.5, 2.8) << endl; // Calls double version
return 0;
}
- Explanation:
- There are two
add()functions, one that works with integers and one with doubles. - The appropriate version is called based on the type of arguments passed.
- There are two
Pass by Value vs Pass by Reference
Pass by Value: A copy of the argument is passed to the function. Any modifications inside the function do not affect the original value.
cppvoid increment(int x) { x++; // Only modifies the copy }Pass by Reference: The actual variable is passed to the function, and modifications inside the function affect the original variable. This is done using the reference operator (
&).cppvoid increment(int &x) { x++; // Modifies the original value }
Example of Pass by Reference:
cpp#include
using namespace std;
void increment(int &x) {
x++; // Modifies the original value
}
int main() {
int num = 5;
increment(num);
cout << "After increment: " << num << endl;
return 0;
}
- Explanation:
- The variable
numis passed by reference, so theincrement()function modifies the original value.
- The variable
Recursion
A recursive function is a function that calls itself to solve a problem.
Example of Recursion (Factorial):
cpp#include
using namespace std;
int factorial(int n) {
if (n <= 1) {
return 1; // Base case
}
return n * factorial(n - 1); // Recursive call
}
int main() {
cout << "Factorial of 5: " << factorial(5) << endl;
return 0;
}
- Explanation:
- The
factorial()function computes the factorial of a number recursively by calling itself.
- The
Key Points:
- Functions help in organizing and reusing code.
- Functions can have parameters and return values.
- Function overloading allows multiple functions with the same name but different signatures.
- Pass by value passes a copy, while pass by reference passes the original variable.
- Recursive functions call themselves to solve problems like factorial or Fibonacci sequence.
C++ functions are a powerful way to structure code, making it modular and reusable.