techmore.in

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:

cpp
return_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, void is 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 the return statement.

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 (void return type).
    • It simply prints "Hello, World!" when called.

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 (a and b), adds them, and prints the result.

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 result variable and printed.

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.

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.

    cpp
    void 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 (&).

    cpp
    void 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 num is passed by reference, so the increment() function modifies the original value.

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.

Key Points:

  1. Functions help in organizing and reusing code.
  2. Functions can have parameters and return values.
  3. Function overloading allows multiple functions with the same name but different signatures.
  4. Pass by value passes a copy, while pass by reference passes the original variable.
  5. 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.