This preview shows pages 1–9. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 © Janice Regan, CMPT 128, February. 2007 CMPT 128: Introduction to Computing Science for Engineering Students Functions Parameters passed by reference © 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 © 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 © 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 © 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 } © 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 ) © 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 © Janice Regan, CMPT 128, February 2007 8 The body of a function...
View
Full
Document
This note was uploaded on 05/18/2010 for the course CMPT 128 taught by Professor Regan during the Spring '08 term at Simon Fraser.
 Spring '08
 REGAN

Click to edit the document details