Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;
}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote