The function call below is designed to return, in variable result, the product o
ID: 3548763 • Letter: T
Question
The function call below is designed to return, in variable result, the product of x and y. All the variables are of type int. What is the correct prototype for such a function?
myFunction(&result, &x, &y);
void myFunction(int, int, int) ;
void myFunction(int *, int *, int *) ;
void myFunction(const int *, const int *, const int *) ;
void myFunction(int *, const int *, const int *) ;
Explanation / Answer
The function call below is designed to return, in variable result, the product of x and y.
All the variables are of type int. What is the correct prototype for such a function?
myFunction(&result, &x, &y);
void myFunction(int *, const int *, const int *) ;
since x and y we dont want to modify inside function so it should be constant pointer.
but result we want to update so it should a pointer that is modifyable. so correct syntax is
void myFunction(int *, const int *, const int *) ;
// correct program that compiles is
#include <iostream>
using namespace std;
void myFunction(int *r, const int *x, const int *y)
{
*r = *x * *y;
}
int main()
{
int result = 0;
int x= 3;
int y= 5;
myFunction(&result,&x,&y);
cout << result << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.