Sunday, August 2, 2009

New to c++, pls help with arrays, call by value, and by reference?

HI,





I am still new in this language. i would like some help. i need to write a program that finds an avarage marks of "y" students by rerunning and terminating the program. i need this in a 2 dimensional array to store a roll number and marks of the "y" number of the students. it should include array of pointers, call by value and call by reference.





my main problem is to combine all these in one program.


give me the codes and an explanation on calling by value and reference.





your help will mean alot to me. thank you.

New to c++, pls help with arrays, call by value, and by reference?
I've been trying to pass on a multi-dimensional array as parameter to a function but I think it'd never work. But I do know how to pass an array to a function.. All you have to do is use pointers... Pointers are very useful when handling arays especially when passing them on to functions as parameters. For example:





void doThis(int *myArray)


{


// some statements


}





This is passing an array as parameter by reference... Since when we say pointer, you are actually pointing to the specific address of the whatever variable being passed on. So, with the use of pointer, whatever changes you make with the variable, they are readily passed to the actual source or address.. This is Calling By Reference.


While Call by Value through arrays is usually done like this...





void doThis(int myArray[])


{


// some statements


}





Remember, when using ByValue parameters, you cannot change the value of the passing variable. You can only temporarily change the value of the variable being passed but only within the premises of the function.


No comments:

Post a Comment