I am just beginning to learn about subprograms in C++. Would appreciate any help determining which of the above call statements would be invalid to use.
Can arithmetic operators be used in the parameters such as in #1 and #3?
Firstly, they're called "functions", not "subprograms".
Every single one of those could be a valid function call. However, we'd need to see the function declaration in order to know whether they are valid or not.
Here a hint:
In C++, arguments could be taken either as value or as reference.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int my_func( int a ) // takes an argument by value, returns a value
{ return a+1; }
int& another_func( int &a ) // takes an argument by reference, returns a reference
{ a += 1; return a; }
#include <iostream>
int main()
{
int a = 3;
std::cout << my_func(a) << std::endl;
std::cout << my_fnuc(a+10) << std::endl;
std::cout << another_func(a) << std::endl;
std::cout << another_func(a+10) << std::endl;
}
subprogram is fine. Its is used generally across many languages. Honestly, function is incorrect, as the y=f(x) terminology/concept breaks down with both void routines and pass by reference routines. And for some unholy reason a function in a class is a method, whatever the heck that even means. Still, incorrect or not, c++ lingo would prefer "function" (incorrectly used or not) & "method" for "subroutines" or "subprograms". /shrug I think we know what it means, regardless.
Can arithmetic operators be used in the parameters such as in #1 and #3?
Yes, so long as the parameter is passed by value and of a type that supports addition of a number to it. It would not be legal, for example, if the the parameter type was a string or a pointer/reference to an integer.
vnum is passed by value and the other 2 are reference.
the function has 3 params and no default values, so calls to it must have 3 params. 2 or 4 is right out.
you should be able to answer the question now if you read all the posts.
use of num3 in main is questionable. It will generally compile and run, but it has a random value from whatever bytes happen to be at that memory location because it was never assigned a value. Because it is used to hold the result (which assigns it) it is ok, but good programming would be to set it to zero anyway when it is declared (yes, I know you did not write it).
So would call statement #3 also be invalid because the middle parameter contains an arithmetic operator but the middle parameter for the function was passed by reference?