In C++, functions are used to perform specific tasks, encapsulating code that can be reused and organized into smaller, manageable parts. There are two main types of functions in C++: predefined functions and user-defined functions.
1. Predefined Functions:
These are functions that come with C++ libraries and can be used directly without the need to define them. They are provided by the C++ Standard Library (e.g., <iostream>
, <cmath>
, etc.).
Example:
int sqrt(int x)
– This predefined function calculates the square root of a number.
Text Data:
#include <cmath>
– Includes the library for mathematical functions.sqrt(x)
– Predefined function that returns the square root ofx
.
2. User-Defined Functions:
These are functions created by the programmer to perform a specific task. They allow for better modularity, reusability, and manageability of code.
4 Types of User-Defined Functions:
User-defined functions can be classified based on whether they accept arguments and whether they return a value.
- Function with No Return Value and No Arguments
- Function with No Return Value but with Arguments
- Function with Return Value and No Arguments
- Function with Return Value and Arguments
1. Function with No Return Value and No Arguments:
In this type of function, no parameters are passed, and the function does not return any value. It simply performs an action.
Text Data:
void displayMessage()
– Declares a function that doesn't return a value and takes no arguments.displayMessage();
– Calls the function to display a message.
Example:
#include <iostream>
using namespace std;
void displayMessage() {
cout << "Hello, welcome to the program!" << endl;
}
int main() {
displayMessage(); // Function call
return 0;
}
2. Function with No Return Value but with Arguments:
In this type, the function takes arguments but does not return any value. It processes the data passed as arguments.
Text Data:
void printSum(int a, int b)
– Declares a function that takes two integers as arguments but does not return a value.printSum(a, b);
– Calls the function and passes two integer values.
Example:
#include <iostream>
using namespace std;
void printSum(int a, int b) {
cout << "Sum: " << a + b << endl;
}
int main() {
int x = 5, y = 10;
printSum(x, y); // Function call with arguments
return 0;
}
3. Function with Return Value and No Arguments:
In this type, the function does not take any arguments but returns a value to the calling function.
Text Data:
int getValue()
– Declares a function that returns an integer and takes no arguments.int result = getValue();
– Calls the function and stores the returned value in a variable.
Example:
#include <iostream>
using namespace std;
int getValue() {
return 42; // Function returns an integer
}
int main() {
int result = getValue(); // Function call
cout << "Returned value: " << result << endl;
return 0;
}
4. Function with Return Value and Arguments:
In this type, the function accepts arguments and returns a value based on the arguments passed to it.
Text Data:
int multiply(int a, int b)
– Declares a function that takes two integers as arguments and returns an integer.int result = multiply(a, b);
– Calls the function and stores the returned value.
Example:
#include <iostream>
using namespace std;
int multiply(int a, int b) {
return a * b; // Function returns the product of a and b
}
int main() {
int x = 3, y = 7;
int result = multiply(x, y); // Function call with arguments
cout << "Product: " << result << endl;
return 0;
}
Summary of the 4 Types of User-Defined Functions:
- No Return, No Arguments – Performs an action without returning a result.
- No Return, With Arguments – Performs an action using arguments, but doesn't return a result.
- With Return, No Arguments – Returns a value without needing any arguments.
- With Return, With Arguments – Returns a value based on the arguments provided.
In conclusion, user-defined functions in C++ help break down complex tasks into smaller, reusable components. They can have different combinations of return values and arguments based on the task at hand.
No comments:
Post a Comment