DEV Community

Mayank Roy
Mayank Roy

Posted on

Day 8: Exploring Functions in C++ - The Building Blocks of Reusability

1. Functions in C++ (Syntax, Return Type, etc.)

  • A function is a block of code designed to perform a specific task.

  • Functions in C++ follow this basic syntax:

  returnType functionName(parameters) {
    // function body
}

Enter fullscreen mode Exit fullscreen mode
  • Return Type: Specifies the data type of the value returned by the function. Use void if no value is returned.

  • Function Name: Describes what the function does.

  • Parameters: Input values for the function (optional).

#include <iostream>
using namespace std;

// Function to check tea temperature
int checkTeaTemperature(int temperature) {
    return temperature;
}

int main() {
    int temp = checkTeaTemperature(85);  // Function call
    cout << "The tea temperature is " << temp << "°C" << endl;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

2. Declaring a Function

  • Function declaration tells the compiler about the function’s name, return type, and parameters. It’s also called a function prototype.

  • The function body is defined elsewhere.

#include <iostream>
using namespace std;

// Declaring the function (prototype)
void serveChai(int cups);

int main() {
    serveChai(3);  // Function call
    return 0;
}

// Function definition is done later
void serveChai(int cups) {
    cout << "Serving " << cups << " cups of chai!" << endl;
}
Enter fullscreen mode Exit fullscreen mode

3. Defining a Function

  • A function definition includes the full function with the body.

  • You must define the function after declaring it if it’s not inline.

#include <iostream>
using namespace std;

// Function definition with body
void makeChai() {
    cout << "Boiling water, adding tea leaves, and serving chai!" << endl;
}

int main() {
    makeChai();  // Calling the function
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

4. Calling a Function

  • To execute a function, you call it by its name followed by parentheses.

  • If the function takes arguments, pass them inside the parentheses.

#include <iostream>
using namespace std;

// Function to brew tea
void brewChai() {
    cout << "Chai is being brewed!" << endl;
}

int main() {
    brewChai();  // Function call
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

5. Function Parameters (Formal, Actual, Default Parameters)

  • Formal parameters: Defined in the function signature.
  • Actual parameters: Values passed during the function call.
  • Default parameters: Parameters with default values if none are passed.
#include <iostream>
using namespace std;

// Function with default parameter
void serveChai(string teaType = "Masala Chai") {
    cout << "Serving " << teaType << endl;
}

int main() {
    serveChai();               // Uses default parameter
    serveChai("Green Chai");    // Uses actual parameter
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

6. Pass by Value

  • Pass by value means the function receives a copy of the argument. Changes made inside the function do not affect the original variable.
#include <iostream>
using namespace std;

void pourChai(int cups) {
    cups = cups + 1;  // Modifies local copy
    cout << "Poured " << cups << " cups of chai!" << endl;
}

int main() {
    int chaiCups = 2;
    pourChai(chaiCups);  // Passing by value
    cout << "Total chai cups outside function: " << chaiCups << endl;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

7. Pass by Reference

  • Pass by reference passes the actual variable, so changes in the function affect the original variable.
#include <iostream>
using namespace std;

void refillChai(int &cups) {  // Pass by reference
    cups += 2;
    cout << "Refilled to " << cups << " cups of chai!" << endl;
}

int main() {
    int chaiCups = 3;
    refillChai(chaiCups);  // Passing by reference
    cout << "Total chai cups now: " << chaiCups << endl;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

8. Scope of Variables

  • Variables declared inside a function have local scope (accessible only within the function).
  • Variables declared outside all functions have global scope (accessible from any function).
#include <iostream>
using namespace std;

int globalChaiStock = 100;  // Global variable

void serveChai() {
    int localCups = 5;  // Local variable
    cout << "Serving " << localCups << " cups from " << globalChaiStock << " total stock." << endl;
}

int main() {
    serveChai();
    cout << "Global chai stock after serving: " << globalChaiStock << endl;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

9. Function Overloading

  • Function overloading allows multiple functions with the same name but different parameter types or numbers.
#include <iostream>
using namespace std;

// Function overloading
void brewChai(int cups) {
    cout << "Brewing " << cups << " cups of chai." << endl;
}

void brewChai(string teaType) {
    cout << "Brewing " << teaType << "." << endl;
}

int main() {
    brewChai(3);             // Calls int version
    brewChai("Masala Chai");  // Calls string version
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

10. Lambda Functions

  • A lambda function is an anonymous function that can be defined inline using the[] syntax.
  • They’re useful for short, simple functions.
#include <iostream>

using namespace std;



int main(){

   //lambda
   [](int cups){
        cout << "Preparing " << cups << " cups of tea" << endl;
   }(5);

    return 0;

}
Enter fullscreen mode Exit fullscreen mode

Summary:

  • Function Declaration & Definition: Tell the compiler about a function and define what it does.
  • Calling Functions: Execute the function by passing arguments if required.
  • Pass by Value/Reference: Controls whether changes affect the original value or a copy.
  • Function Overloading: Allows multiple functions with the same name but different parameter lists.
  • Lambda Functions: Short, inline functions used for simple tasks.

Top comments (0)