In the C++ programming language, we can add functions with many parameters, each parameter may have different types. We can add as many parameters as we want by defining its type and its name to be used inside that function separated with ‘,’ coma. We can shape this usage in the most common syntax to define a function as below,
1 2 3 4 5 6 7 |
return_type function_name ( type1 parameter1, type2 parameter2, ...) // this is the function declaration, return type is void { // the definition of the function (statements and other functions) return return_value; } |
In the function declaration, the parameters have a clear correspondence to the arguments passed in the function call. So the called values should be in the same type or in a type that can be convertible to the defined parameter type. This is a very simple add function with two a and b integer parameters and its return type is also defined as an integer.
1 2 3 4 5 6 |
int add( int a, int b) { return a+b; } |
As you see we must define each type of parameter while both are integers. We can use bool, char, int, short int, unsigned short int, long int, unsigned long int, long long int, unsigned long long int, float, double, char, string, structures, objects, pointers, or any other class types as a parameter. Note that all defined variable names are only available to use inside the function. Avoid using the same names as in the outside of the function, you might have conflicts in usage, this generally happens with mixing general parameters with function parameters.
We can call this function to set a variable as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> int add (int a, int b) { return a+b; } int main() { int sum = add(300, 500); std::cout << sum << '\n'; } |
We can define many multiple variables in different types as below,
1 2 3 4 5 6 7 |
float myfunc( int i, float f, string str, boolean b) { float result= i*f ; return result; } |
We can call this function as below,
1 2 3 |
float f= myfunc( 5, 7.5, "test", 1); |
As you see here we have four parameters and each has different data type.
Get started building powerful apps with C++Builder!
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition