In C++, the scope of a variable refers to the part of the program where the variable can be accessed or used. A global variable is accessible throughout the entire program, while a local variable is only accessible within the function or block where it is declared.
#include <iostream> void print(); int i = 10; // Global variable int main() { std::cout << i << "\n"; // Outputs 10 print(); } void print() { int j = 0; // Local variable i = 20; std::cout << i << "\n"; // Outputs 20 std::cout << j << "\n"; // Outputs 0 }
In C++, functions are typically divided into two parts: declarations and definitions. A declaration tells the compiler about a function's name, return type, and parameters, while a definition provides the actual body of the function.
// ~~~~~~ main.cpp ~~~~~~ #include <iostream> #include "functions.hpp" int main() { std::cout << say_hi("John"); } // ~~~~~~ functions.hpp ~~~~~~ // Function declaration std::string say_hi(std::string name); // ~~~~~~ functions.cpp ~~~~~~ #include <string> #include "functions.hpp" // Function definition std::string say_hi(std::string name) { return "Hello, " + name + "!\n"; }
A function template is a way to create a generic function in C++ that can work with any data type. Instead of writing multiple functions for different data types, a function template allows you to write one function and specify the data type when calling it.
#include <iostream> template <typename T=""> T add(T a, T b) { return a + b; } int main() { std::cout << add(5, 10) << "\n"; // Outputs 15 std::cout << add(5.5, 10.5) << "\n"; // Outputs 16.0 }
In C++, default arguments are values that are automatically assigned to function parameters if no arguments are provided by the caller. This allows functions to be called with fewer arguments than they have parameters.
#include <iostream> void greet(std::string name = "Guest") { std::cout << "Hello, " << name << "!\n"; } int main() { greet(); // Outputs: Hello, Guest! greet("Alice"); // Outputs: Hello, Alice! }
Function overloading in C++ allows you to have multiple functions with the same name but different parameters. This enables a function to handle different types of input and return different types based on the arguments provided.
#include <iostream> int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } int main() { std::cout << add(5, 10) << "\n"; // Outputs 15 std::cout << add(5.5, 10.5) << "\n"; // Outputs 16.0 }
An inline function is a function defined with the keyword `inline`. It suggests to the compiler that it should insert the function's code directly into the calling code, rather than calling the function normally. This can make the program run faster by eliminating the overhead of a function call, but it can also increase the size of the binary.
#include <iostream> inline int add(int a, int b) { return a + b; } int main() { std::cout << add(5, 10) << "\n"; // Outputs 15 }
A return value is the value that a function gives back to the part of the program that called it. If a function is declared to return a specific type, it must use the return statement to provide a value of that type.
#include <iostream> int add(int a, int b) { return a + b; // Return the sum of a and b } int main() { int result = add(5, 10); std::cout << result << "\n"; // Outputs 15 }
Function parameters are placeholders for the actual values that are passed to the function when it is called. Parameters act like variables inside the function that hold the data provided by the caller.
#include <iostream> void printNumber(int number) { std::cout << "The number is: " << number << "\n"; } int main() { printNumber(10); // Outputs: The number is: 10 }
C++ comes with a standard library of built-in functions that can be used to perform various operations, such as mathematical calculations or input/output operations. These functions are available after including the appropriate header files.
#include <iostream> #include <cmath> // Include the cmath library int main() { std::cout << "Square root of 25 is: " << sqrt(25) << "\n"; // Outputs: Square root of 25 is: 5 }
Calling a function in C++ means to execute the function. To call a function, you need to provide the function's name followed by a set of parentheses. If the function requires parameters, you provide the arguments inside the parentheses.
#include <iostream> void sayHello() { std::cout << "Hello, World!" << "\n"; } int main() { sayHello(); // Call the function }
A void function is a type of function that does not return a value. It performs an action but does not provide any information back to the caller.
#include <iostream> void displayMessage() { std::cout << "This is a message." << "\n"; } int main() { displayMessage(); // Outputs: This is a message. }
To use a function in C++, you first need to declare it (tell the compiler about its existence) and then define it (provide the actual implementation of the function). The declaration is usually placed in a header file, and the definition is placed in a .cpp file.
#include <iostream> // Function declaration void sayGoodbye(); int main() { sayGoodbye(); // Call the function } // Function definition void sayGoodbye() { std::cout << "Goodbye!" << "\n"; }
Function arguments are the actual values passed to a function's parameters when the function is called. The arguments must match the parameters in both number and type.
#include <iostream> void greet(std::string name) { std::cout << "Hello, " << name << "!\n"; } int main() { greet("Alice"); // Outputs: Hello, Alice! }
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!