Skip to content

C++ - Functions

Chapter 8: Functions

In this chapter, we will learn about functions in C++. Our learning will be based on the task based approach. Each task will help you to understand the requirements and then you will be able to implement the code.

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

Notes:

  • 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
    }
  • 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).

Code Snippet:

#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;
}

2. Declaring a Function

Notes:

  • 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.

Code Snippet:

#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;
}

3. Defining a Function

Notes:

  • A function definition includes the full function with the body.
  • You must define the function after declaring it if it’s not inline.

Code Snippet:

#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;
}

4. Calling a Function

Notes:

  • To execute a function, you call it by its name followed by parentheses.
  • If the function takes arguments, pass them inside the parentheses.

Code Snippet:

#include <iostream>
using namespace std;
// Function to brew tea
void brewChai() {
cout << "Chai is being brewed!" << endl;
}
int main() {
brewChai(); // Function call
return 0;
}

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

Notes:

  • 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.

Code Snippet:

#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;
}

6. Pass by Value

Notes:

  • Pass by value means the function receives a copy of the argument. Changes made inside the function do not affect the original variable.

Code Snippet:

#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;
}

7. Pass by Reference

Notes:

  • Pass by reference passes the actual variable, so changes in the function affect the original variable.

Code Snippet:

#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;
}

8. Scope of Variables

Notes:

  • 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).

Code Snippet:

#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;
}

9. Function Overloading

Notes:

  • Function overloading allows multiple functions with the same name but different parameter types or numbers.

Code Snippet:

#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;
}

10. Lambda Functions

Notes:

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

Code Snippet:

#include <iostream>
using namespace std;
int main() {
// Lambda function to prepare chai
auto prepareChai = [](int cups) {
cout << "Preparing " << cups << " cups of chai." << endl;
};
prepareChai(2); // Calling lambda function
return 0;
}

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.