+ 4
anyone expalain me how this c++ program works?
PROGRAM: #include <iostream> using namespace std; void func1 (int x[]){ x[0]=0; } void func2(int y){ y=0; } int main(){ int x[]={7}, y=8; func1(x); func2 (y); cout << x[0] << y; return 0; } OUTPUT: 08
4 Antworten
+ 15
Arrays are passed by reference (address), while variables are passed by value to functions. We can see that func1 which receives an array as a parameter is able to change the original value of the array, while func2 does not.
+ 1
Arrays can actually be modified by a function calls because it's address is passed by reference to the function that calls them while the same cannot be said for variable which is passed as a copy except you explicitly pass it with the reference operator.
0
Music. Replay
0
the array above can be altered by the fun1 but the variable can't