Site icon Learn C++

Learn to Use Parameters in Functions in C++

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,

[crayon-6623a0180613e254282001/]

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.

[crayon-6623a01806148203855863/]

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,

[crayon-6623a01806152177907956/]

We can define many multiple variables in different types as below,

[crayon-6623a01806154622491352/]

We can call this function as below,

[crayon-6623a01806159061979019/]

As you see here we have four parameters and each has different data type.

Get started building powerful apps with C++Builder!

Exit mobile version