{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture21 Passing By Reference

lecture21 Passing By Reference - CMPT 128 Introduction to...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
1 © Janice Regan, CMPT 128, February. 2007 CMPT 128: Introduction to Computing Science for Engineering Students Functions Parameters passed by reference
Image of page 1

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

View Full Document Right Arrow Icon
© Janice Regan, CMPT 128, February 2007 2 Defining a function (1) The first line of a function is the function definition head The function head has a type void quadraticSolver( double coeffSq, double coeffLin, double coefConst, double *root1p, double *root2p, int *solutionTypep ) The function head has a name or identifier void quadraticSolver( double coeffSq, double coeffLin, double coefConst, double& root1p, double& root2p, int& solutionTypep ) There is no ; at the end of a function definition head. The function definition head has 0 or more parameters (arguments) void quadraticSolver( double coeffSq , double coeffLin , double coefConst , double& root1 , double& root2 , int& solutionType ) Parameters are passed by value or by reference
Image of page 2
© Janice Regan, CMPT 128, February 2007 3 Defining a function (2) Each of a function’s parameters have types A function may have parameters of more than one type, each parameter must have a type void quadraticSolver( double coeffSq, double coeffLin, double coefConst, double& xroot1, double& xroot2, int& solutionType ) Parameters passed by reference have types ending with & for example double& is a reference to a double int& is a reference to an int
Image of page 3

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

View Full Document Right Arrow Icon
© Janice Regan, CMPT 128, February 2007 4 Sample Function void quadraticSolver( double coeffSq, double coeffLin, double coefConst, double& xroot1, double& xroot2, int& solutionType ) { //Determine the number of roots (0, 1 or 2) //Calculate the value of xroot1 // if there are 1 or 2 roots //Calculate the value of xroot2 // if there are 2 roots //These values are based on the //values of the coefficients passed into the // function (coeffSq, coeffLin, coeefConst) } Function definition Function body
Image of page 4
© Janice Regan, CMPT 128, February 2007 5 The body of a function After the function definition the body of the function is enclosed in {} void quadraticSolver( double coeffSq, double coeffLin, double coefConst, double& xroot1p, double& xroot2p, int& solutionTypep ) { local variable declarations: calculations to determine values of variables passed by reference (no function value passed back for void function) return }
Image of page 5

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

View Full Document Right Arrow Icon
© Janice Regan, CMPT 128, February 2007 6 References using pointers You can also pass a variable by reference using pointers void quadraticSolver( double coeffSq, double coeffLin, double coefConst, double * xroot1p, double * xroot2p, int * solutionTypep )
Image of page 6
© Janice Regan, CMPT 128, February 2007 7 Sample Function void quadraticSolver( double coeffSq, double coeffLin, double coefConst, double* xroot1p, double* xroot2p, int* solutionTypep ) { double xroot1; double xroot2; int numroots; //Calculate numroots, xroot1 and xroot2 *xroot1p = xroot1; *xroot2p = xroot2; *solutionTypep = numroots; } Function definition Function body
Image of page 7

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

View Full Document Right Arrow Icon
© Janice Regan, CMPT 128, February 2007 8 The body of a function After the function definition the body of the function is enclosed in {} void quadraticSolver( double coeffSq, double coeffLin, double coefConst, double * xroot1p, double * xroot2p, int * solutionTypep ) {
Image of page 8
Image of page 9
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