In C++ programming, you can categorize functions into four different categories based on the arguments we are passing to the function and the return type of the function. These are:
- Function with no argument and no return type value.
- Function with argument but no return type value.
- Function with no argument but with return type value.
- Function with argument and with return type value.
1. Function with no argument and no return type value.
In this type of function, we do not pass any value while calling the function and no value is returned from the function as an output.
Example:
#include<iostream> using namespace std; //function with no argument and no return type void sum(){ int x = 10, y = 5, z = 8; int sum = x + y + z; cout<<"Total Sum = "<<sum; } int main(){ //function call sum(); return 0; }
Total Sum = 23
Here in the above code, the sum() function is called inside main() without any arguments. The sum() function performs the summation of three variable x, y, and z, and print the answer. As the return type of sum() is void so no value is returned by the function.
2. Function with argument but no return type value.
In this type of function, we do pass values while calling the function but no value is returned from the function as an output.
Example:
#include<iostream> using namespace std; //function with argument but no return type void sum(int x, int y, int z){ int sum = x + y + z; cout<<"Total Sum = "<<sum; } int main(){ int a = 10, b = 3, c = 9;
//function call
sum(a, b, c);
return 0;
}
Output:
Total Sum = 22
Here in the above code, the sum() function is called inside main() with three arguments a, b, and c, and the sum() function performs the addition of the three values and prints the total sum. The return type of the function sum() is void so no value is returned by the function.
3. Function with no argument but with return type value.
In this type of function, we do not pass values while calling the function but some value is returned from the function as an output.
Example:
#include<iostream> using namespace std; //function with no argument but return type int sum(){ int x = 10, y = 8, z = 15, sum; sum = x + y + z; return sum; } int main(){ //function call int answer = sum(); cout<<"Total Sum = "<<answer; return 0; }
Output:
Total Sum = 33
Here in the above code, the sum() function is called inside the main() function with no arguments, and the sum() function perform the addition of value x, y, and z and return the sum value.
4. Function with argument and with return type value.
In this type of function, we do pass values while calling the function and some value is returned from the function as an output.
Example:
#include<iostream> using namespace std; //function with argument and with return type int sum(int x, int y){ int sum; sum = x + y; return sum; } int main(){ int m = 10, n = 20; //function call int answer = sum(m , n); cout<<"Total Sum = "<<answer; return 0; }
Output:
Total Sum = 30
Here in the above, the sum() is called inside main() with two arguments m and n, and sum() adds those two values and returns the value of sum which is an integer. type.
Based on your requirement and condition you can choose any method out of four to solve your problem.
Read next:
No comments:
Post a Comment