in c++
1)Code the function definition for twoFunction, picking up the array myscore. myscore has no return value.
2)How can the function twoFunction change the contents of the second element of myscore?
3) If twoFunction does change the contents of the second element of myscore, what happens to the second element of myscore in the calling program?
#include <iostream>
using namespace std;
void twoFunction(int myscore[]) // 1)
{
myscore[1] = 100; // 2) the second element of the
array is changed
}
int main() {
int myscore[] = {1,2,3,4,5};
cout<<"\nArray before function call : ";
for(int i=0;i<5;i++)
cout<<myscore[i]<<" ";
twoFunction(myscore);
cout<<"\nArray after function call : ";
for(int i=0;i<5;i++)
cout<<myscore[i]<<" "; // 3) After function call , the
value of second element of array is changed.
return 0;
}
Output:
Array before function call : 1 2 3 4 5 Array after function call : 1 100 3 4 5
2)
The value of the second element of array changes after calling the function. This is due to the fact that array name which is passed as argument to the function will act as the base address of array. Any changes made into the array will be done on addresses. That's why the change is reflected back in the main function.
Do ask if any doubt. Please upvote.
Get Answers For Free
Most questions answered within 1 hours.