Call statements

1)funct1(num1 +9, num1, num2);
2)funct1(2, num1);
3)funct1(num1, num2 + 5, num3);
4)funct1( 9, num1, num2 , num3);

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?
Last edited on
I don't think we can help if you don't have any work for us to help with. Could you explain what you think about each one?
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.
Last edited on
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;
}


From your edit:

Can arithmetic operators be used in the parameters such as in #1 and #3?


In some circumstances yes, in others no. I can only reiterate:

MikeyBoy wrote:
we'd need to see the function declaration in order to know whether they are valid or not.

Last edited on
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.



Last edited on
Sorry my prof wasn't too clear at first. This is the code he gave us:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void  funct1 (int vnum , int & rnum1 , int & rnum2)
{
	vnum  += 7;
	rnum1  += 4;
	rnum2 = vnum + rnum1;
}

int main( )
{
	int num1 = 5,  num2 = 16 , num3;

	funct1 (num1 , num2 , num3);
	cout   <<   endl   <<   “num1=”   <<   num1   <<   “\tnum2=”   <<   num2   <<   “\tnum3=”   <<   num3;
	return 0;
}
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).
Last edited on
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?
yes.
Topic archived. No new replies allowed.