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.
Functions in C++
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
voidif no value is returned. - Function Name: Describes what the function does.
- Parameters: Input values for the function (optional).
For example,
#include <iostream>using namespace std;
// Function to check tea temperatureint checkTeaTemperature(int temperature) { return temperature;}
int main() { int temp = checkTeaTemperature(85); // Function call cout << "The tea temperature is " << temp << "°C" << endl; return 0;}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.
For example,
#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 latervoid serveChai(int cups) { cout << "Serving " << cups << " cups of chai!" << endl;}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.
For example,
#include <iostream>using namespace std;
// Function definition with bodyvoid makeChai() { cout << "Boiling water, adding tea leaves, and serving chai!" << endl;}
int main() { makeChai(); // Calling the function return 0;}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.
For example:
#include <iostream>using namespace std;
// Function to brew teavoid brewChai() { cout << "Chai is being brewed!" << endl;}
int main() { brewChai(); // Function call return 0;}Function 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.
For example,
#include <iostream>using namespace std;
// Function with default parametervoid serveChai(string teaType = "Masala Chai") { cout << "Serving " << teaType << endl;}
int main() { serveChai(); // Uses default parameter serveChai("Green Chai"); // Uses actual parameter return 0;}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.
For example,
#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;}Pass by Reference
Pass by reference passes the actual variable, so changes in the function affect the original variable.
For example,
#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;}Scope of Variable
- 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).
For example,
#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;}Function Overloading
Function overloading allows multiple functions with the same name but different parameter types or numbers.
For example,
#include <iostream>using namespace std;
// Function overloadingvoid 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;}Lambda Functions
- A lambda function is an anonymous function that can be defined inline using the
[]syntax. - They’re useful for short, simple functions.
For example,
#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.
Start your journey with ChaiCode
All of our courses are available on chaicode.com. Feel free to check them out.