In such cases we can in effect pass by reference

Info icon This preview shows pages 33–37. Sign up to view the full content.

` In such cases, we can, in effect, pass by reference , which means we pass a pointer to the parameter . ` This will allow the function to alter the variable which is used to pass the parameter’s value in the calling environment .
Image of page 33

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

` Consider a simple function to swap, or exchange two values, with the following declaration: void swap(int x, int y);
Image of page 34
` Consider using this function to swap, or exchange two values, in the following code: /* Recall the declaration of the function: void swap(int x, int y); */ int a = 10; int b = 5; swap(a, b); ` Even if swap correctly exchanges a and b in the called function, swap(), the value of a and b in this, the calling function will still have their original values ` What to do?
Image of page 35

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

int a = 10; int b = 5; swap(&a, &b); /*Now, swap will be able to exchange the values of a and b in the calling environment */ NOTICE: The declaration of swap must be changed too: void swap(int *x, int *y); because we are now passing 2 8-byte addresses rather than 2 4-byte integers
Image of page 36
` Suppose we want to call a function declared as: int sum(const int *array, int size ); It sums the elements of an array given the address of the start of the array and its size: int array[6] = {18, 16, 15, 20, 19, 17}; int size = 6; int total; . . . . total = sum(array, size); /* OR total = sum(&array[0], size); */ . . . . ` Any time a pointer is passed as a parameter, if the function will not write to variables pointed to by the pointer, the const keyword should be used. This is why the first parameter of sum should be declared with the const keyword above. It will allow the function sum() to read values from the array, but not affect the values in any way.
Image of page 37
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern