Functions in C++ are blocks of code that perform specific tasks. They help in organizing code, making it reusable and easier to manage.
A function in C++ is defined with a return type, a name, and a parameter list. The syntax is: returnType functionName(parameterList)
.
#include <iostream>
using namespace std;
// Function declaration
int add(int a, int b);
int main() {
int sum = add(5, 3);
cout << "Sum: " << sum << endl;
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
A function must be declared before it is used. The definition provides the actual body of the function.
Functions can return values of different types, such as int
, float
, char
, etc. Use void
if no value is returned.
Console Output:
Sum: 8
Function overloading allows multiple functions with the same name but different parameters. It helps in handling different data types with the same function logic.
#include <iostream>
using namespace std;
// Overloaded functions
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int main() {
cout << "Int sum: " << add(5, 3) << endl;
cout << "Double sum: " << add(5.5, 3.3) << endl;
return 0;
}
Overloading improves code readability and usability by allowing the same operation to be performed on different data types.
Console Output:
Int sum: 8
Double sum: 8.8
Default arguments in C++ functions allow you to provide default values for function parameters. This makes some arguments optional when calling the function.
#include <iostream>
using namespace std;
// Function with default argument
void display(int a, int b = 10) {
cout << "a: " << a << ", b: " << b << endl;
}
int main() {
display(5);
display(5, 15);
return 0;
}
They simplify function calls by reducing the number of arguments needed, and they enhance flexibility by allowing different levels of function customization.
Console Output:
a: 5, b: 10
a: 5, b: 15
Inline functions are those whose function body is inserted directly into each call's place, reducing the overhead of function calls.
#include <iostream>
using namespace std;
// Inline function
inline int square(int x) {
return x * x;
}
int main() {
cout << "Square of 5: " << square(5) << endl;
return 0;
}
They can optimize performance by eliminating the overhead of function calls, especially for small functions. However, excessive use can increase code size.
Console Output:
Square of 5: 25
Recursive functions are those that call themselves to solve a problem. They are useful for problems that can be broken down into smaller, similar problems.
#include <iostream>
using namespace std;
// Recursive function
int factorial(int n) {
if (n <= 1) return 1;
else return n * factorial(n - 1);
}
int main() {
cout << "Factorial of 5: " << factorial(5) << endl;
return 0;
}
Recursion can simplify code and make it more readable, but it may lead to performance issues due to excessive function calls and stack usage.
Console Output:
Factorial of 5: 120
In C++, parameters can be passed by value or by reference. Passing by value copies the argument, while passing by reference allows modifications to the original data.
#include <iostream>
using namespace std;
// Pass by value
void passByValue(int a) {
a = 10;
}
// Pass by reference
void passByReference(int &a) {
a = 10;
}
int main() {
int num = 5;
passByValue(num);
cout << "After pass by value: " << num << endl;
passByReference(num);
cout << "After pass by reference: " << num << endl;
return 0;
}
Passing by value is safer as it does not alter the original data, but it can be inefficient for large data. Passing by reference is efficient but can lead to unintended modifications.
Console Output:
After pass by value: 5
After pass by reference: 10
Lambda expressions in C++ allow you to define anonymous functions. They are useful for short snippets of code that are not reused elsewhere.
#include <iostream>
using namespace std;
int main() {
// Lambda expression
auto add = [](int a, int b) { return a + b; };
cout << "Sum using lambda: " << add(5, 3) << endl;
return 0;
}
Lambdas simplify code by eliminating the need for separate function declarations and definitions, making code more concise and readable.
Console Output:
Sum using lambda: 8
Function templates in C++ allow you to create a single function that can work with different data types. They provide flexibility and code reusability.
#include <iostream>
using namespace std;
// Template function
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
cout << "Int sum: " << add(5, 3) << endl;
cout << "Double sum: " << add(5.5, 3.3) << endl;
return 0;
}
Templates reduce code duplication by allowing a single function to handle multiple data types, improving maintainability and efficiency.
Console Output:
Int sum: 8
Double sum: 8.8
Function pointers in C++ are used to store the address of a function. They allow functions to be passed as arguments to other functions, enabling dynamic function calls.
#include <iostream>
using namespace std;
// Function to be pointed to
void greet() {
cout << "Hello, World!" << endl;
}
int main() {
// Function pointer
void (*funcPtr)() = greet;
funcPtr();
return 0;
}
They allow for flexible and dynamic function calls, making them useful in scenarios like callback functions and event-driven programming.
Console Output:
Hello, World!
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