COSC 181 Foundations of Computer Programming Class 20 6 4 Function Definitions with Multiple Parameters Multiple parameters Functions often require more than one piece of information to perform their tasks Specified in both the function prototype and the function header as a comma separated list of parameters 2 1 Fig 6 3 GradeBook h 2 Def in it io n of c las s GradeBook that f inds the max imum of three grades 3 Member funct ions are def ined in GradeBook cpp 4 inc lude string program uses C standard string class 5 us ing std string 6 7 GradeBook c lass def init ion 8 c las s GradeBook 9 GradeB ook h 10 public 11 GradeBook string constructor initializes course name 12 vo id setCourseName string function to set the course name 13 string getCourseName function to retrieve the course name 14 vo id displayMessage display a welcome message 15 vo id inputGrades input three grades from user 16 vo id displayGradeReport display a report based on the grades 17 in t maximum int int int determine max of 3 values 18 private 19 string courseName course name for this GradeBook 20 int maximumGrade maximum of three grades 21 end c lass GradeBook Outline 1 of 1 Prototype for a member function that takes three arguments Data member to store maximum grade 3 1 Fig 6 4 GradeBook cpp 2 3 4 determ ines the max imum of three grades inc lude iostream 5 6 7 us ing std cout us ing std cin us ing std endl Member funct ion def in i t io ns for c lass GradeBook that 8 9 inc lude GradeBook h include definition of class GradeBook 10 11 constructor initializes courseName with string supplied as argument 12 initializes studentMaximum to 0 13 GradeBook GradeBook string name 14 15 setCourseName name validate and store courseName 16 maximumGrade 0 this value will be replaced by the maximum grade Outline GradeB ook cpp 1 of 3 17 end GradeBook constructor 18 19 function to set the course name limits name to 25 or fewer characters 20 vo id GradeBook setCourseName string name 21 22 23 24 if name length 25 if name has 25 or fewer characters courseName name store the course name in the object else if name is longer than 25 characters 25 26 27 set courseName to first 25 characters of parameter name courseName name substr 0 25 select first 25 characters cout Name name exceeds maximum length 25 n 28 Limiting courseName to first 25 characters n endl 29 end if else 30 end function setCourseName 4 31 32 funct ion to ret r ie ve the course name Outline 33 str ing GradeBook getCourseName 34 35 return courseName 36 end funct ion getCourseName GradeB 37 38 disp la y a welcome message to the GradeBook user ook cpp 39 void GradeBook displayMessage 40 41 42 43 th is statement ca l ls getCourseName to get the name of the course th is GradeBook represents cout Welcome to the grade book for n getCourseName n endl 44 45 2 of 3 end funct ion disp layMessage 46 47 input three grades f rom user determ ine max imum 48 void GradeBook inputGrades 49 50 int grade1 f ir s t grade entered by user 51 int grade2 second grade entered by user 52 int grade3 th ir d grade entered by user 53 54 cout Enter three integer grades 55 cin grade1 grade2 grade3 56 57 store max imum in member studentMax imum 58 maximumGrade maximum grade1 grade2 grade3 59 Call to function maximum passes three arguments end funct ion inputGrades 5 60 61 returns the max imum of it s three integer parameters 62 int GradeBook maximum int x int y int z 63 64 int maximumValue x assume x is the largest to start 65 66 determine whether y is greater than maximumValue 67 if y maximumValue 68 Outline maximum member function header GradeB ook cpp Comma separated parameter list maximumValue y make y the new maximumValue 69 70 determine whether z is greater than maximumValue 71 if z maximumValue 72 3 maximumValue z make z the new maximumValue of 3 73 74 return maximumValue 75 end function maximum Returning a value to the caller 76 77 display a report based on the grades entered by user 78 void GradeBook displayGradeReport 79 80 output maximum of grades entered 81 cout Max imum of grades entered maximumGrade endl 82 end funct ion displayGradeReport 6 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Fig 6 5 f ig06 05 cpp Create GradeBook object input grades and disp lay grade report inc lude GradeBook h include definition of class GradeBook int main create GradeBook object GradeBook myGradeBook CS101 C Programming myGradeBook displayMessage display welcome message myGradeBook inputGrades read grades from user myGradeBook displayGradeReport display report based on grades return0 indicate successful termination end main Outline fig06 0 5 cpp 1 of 1 Welcome to the grade book for CS101 C Programming Enter three integer grades 86 67 75 Max imum of grades entered 86 Welcome to the grade book for CS101 C Programming Enter three integer grades 67 86 75 Max imum of grades entered 86 Welcome to the grade book for CS101 C Programming Enter three integer grades 67 75 86 Max imum of grades entered 86 7 Software Engineering Observation 6 4 The commas used in line 58 of Fig 6 4 to separate the arguments to function maximum are not comma operators as discussed in Section 5 3 The comma operator guarantees that its operands are evaluated left to right The order of evaluation of a function s arguments however is not specified by the C standard Thus different compilers can evaluate function arguments in different orders The C standard does guarantee that all arguments in a function call are evaluated before the called function executes 8 6 4 Function Definitions with Multiple Parameters Cont Compiler uses a function prototype to Check that calls to the function contain the correct number and types of arguments in the correct order Ensure that the value returned by the function is used correctly in the expression that called the function Each argument must be consistent with the type of the corresponding parameter Parameters are also called formal parameters 9 Portability Tip 6 1 Sometimes when a function s arguments are more involved expressions such as those with calls to other functions the order in which the compiler evaluates the arguments could affect the values of one or more of the arguments If the evaluation order changes between compilers the argument values passed to the function could vary causing subtle logic errors 10 Error Prevention Tip 6 2 If you have doubts about the order of evaluation of a function s arguments and whether the order would affect the values passed to the function evaluate the arguments in
View Full Document