Function parameters in C++ allow you to pass data into functions, enabling code reusability and modularity. Parameters can be passed by value, by reference, or by pointer, each serving different purposes and use cases.
#include <iostream>
void printNumber(int num) {
std::cout << "Number: " << num << std::endl;
}
int main() {
printNumber(5);
return 0;
}
In this method, a copy of the actual parameter is passed to the function. Changes made to the parameter inside the function do not affect the original argument.
Console Output:
Number: 5
When passing by reference, the function receives a reference to the actual parameter, allowing modifications to the original data.
#include <iostream>
void increment(int &num) {
num++;
}
int main() {
int value = 10;
increment(value);
std::cout << "Value: " << value << std::endl;
return 0;
}
The original variable is modified, as the function directly accesses the memory location of the argument.
Console Output:
Value: 11
Pointers allow functions to modify the actual parameter by passing the address of the variable. This method is useful for dynamic memory allocation.
#include <iostream>
void setToZero(int *num) {
*num = 0;
}
int main() {
int value = 20;
setToZero(&value);
std::cout << "Value: " << value << std::endl;
return 0;
}
The function can directly modify the value at the memory location pointed by the pointer.
Console Output:
Value: 0
C++ allows you to provide default values for function parameters. If no argument is passed, the default value is used.
#include <iostream>
void greet(std::string name = "Guest") {
std::cout << "Hello, " << name << "!" << std::endl;
}
int main() {
greet("Alice");
greet();
return 0;
}
This feature enhances flexibility, allowing function calls with fewer arguments.
Console Output:
Hello, Alice!
Hello, Guest!
C++ supports function overloading, allowing multiple functions with the same name but different parameter lists.
#include <iostream>
void print(int num) {
std::cout << "Integer: " << num << std::endl;
}
void print(double num) {
std::cout << "Double: " << num << std::endl;
}
int main() {
print(5);
print(3.14);
return 0;
}
Function overloading provides the ability to handle different data types with a unified interface.
Console Output:
Integer: 5
Double: 3.14
Using the 'const' keyword, you can prevent functions from modifying the parameter, ensuring data integrity.
#include <iostream>
void display(const int &num) {
std::cout << "Number: " << num << std::endl;
}
int main() {
int val = 7;
display(val);
return 0;
}
Const parameters ensure that the function cannot alter the input data, which is crucial for maintaining consistency.
Console Output:
Number: 7
Variadic functions can accept a variable number of arguments, using ellipses (...) to denote the additional parameters.
#include <iostream>
#include <cstdarg>
void printNumbers(int count, ...) {
va_list args;
va_start(args, count);
for (int i = 0; i < count; ++i) {
int num = va_arg(args, int);
std::cout << num << " ";
}
va_end(args);
std::cout << std::endl;
}
int main() {
printNumbers(3, 1, 2, 3);
return 0;
}
This method is useful for functions that need to handle lists of arguments of varying lengths.
Console Output:
1 2 3
Inline functions are expanded in place, reducing the overhead of a function call. They are defined using the 'inline' keyword.
#include <iostream>
inline int add(int a, int b) {
return a + b;
}
int main() {
std::cout << "Sum: " << add(3, 4) << std::endl;
return 0;
}
Inline functions can improve performance by eliminating the call overhead, especially for small, frequently called functions.
Console Output:
Sum: 7
Lambdas provide a concise way to define anonymous functions, often used for short-term operations that do not require a separate function.
#include <iostream>
int main() {
auto add = [](int a, int b) { return a + b; };
std::cout << "Lambda Sum: " << add(5, 6) << std::endl;
return 0;
}
Lambdas enhance code readability and are particularly useful in algorithms and event handling.
Console Output:
Lambda Sum: 11
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies