1 cs 1430: programming in c++. quiz 1 2 3 functions function prototype float sqrt(float x); function...
DESCRIPTION
3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function Definition float sqrt(float x) { // compute and return the square root } Function Header, Body Receiving value through parameters Function Call root1 = (-coefB + sqrt(Delta)) / (2 * coefA); Function Parameters (Arguments) Formal Parameters Actual ParametersTRANSCRIPT
1
CS 1430: Programming in C++
2
Quiz 1
3
Functions• Function Prototype float sqrt(float x); Function name, type, parameter and parameter type
• Function Definition float sqrt(float x) { // compute and return the square root } Function Header, Body Receiving value through parameters
• Function Call root1 = (-coefB + sqrt(Delta)) / (2 * coefA);
• Function Parameters (Arguments) Formal Parameters Actual Parameters
4
Write a C++ function to find and return the largest value of three integers.
Function prototype Name, Type, Parameters, Parameter Types
Function Name: Largest or LargestOfThreeInt Your choice but must be meaningful!
Function Type: int
Parameters: How many? num1, num2, num3 Data Type of each parameter int
5
Function prototype
int Largest(int num1, int num2, int num3);
6
// Function prototypeint Largest(int num1, int num2, int num3); // Specify types! // Formal parameters.
int main() { int score1, score2, score3, max;
cin >> score1 >> score2 >> score3;
// Call function to find the highest score max = Largest(score1, score2, score3); // Function call: No types! // Actual parameters. // Different names from formal parameters.
cout << "The largest score is " << max;
return 0; }
7
Function definition// --------------------------------------------// The function finds and returns the largest // value of three integers. // --------------------------------------------int Largest(int num1, int num2, int num3) { // local variable int maxVal;
maxVal = num1;
if (num2 > maxVal) maxVal = num2;
if (num3 > maxVal) maxVal = num3;
return maxVal; }// Could be implemented in different ways// Where to get the values of num1, num2, num3?// Actual parameters at function calls.
8
// Function definition // Function Header// Function bodyint Largest(int num1, int num2, int num3) { int maxVal;
maxVal = num1;
if (num2 > maxVal) maxVal = num2;
if (num3 > maxVal) maxVal = num3;
return maxVal; }
9
Programming Rules
Every function, except the main function, must have a comment which describes what it does.
Other rules on functions in the future.
10
// --------------------------------------------// Comment block// --------------------------------------------
// Includes
// Constants
// Function prototype int Largest(int num1, int num2, int num3);
int main() { int score1, score2, score3, max; cin >> score1 >> score2 >> score3;
max = Largest(score1, score2, score3);
cout << "The largest score is " << max;
return 0; }
// --------------------------------------------// The function finds and returns the largest // value of three integers.// --------------------------------------------int Largest(int num1, int num2, int num3) { // local variable int maxVal; … return maxVal; }
11
Function Call and Control Transfer
int main(){ int score1, score2, score3, max; cin >> score1 >> score2 >> score3;
max = Largest(score1, score2, score3); cout << “Max = “ << max; return 0;}
int Largest(int num1, int num2, int num3){ int maxVal; maxVal = num1;
if (num2 > maxVal) maxVal = num2;
if (num3 > maxVal) maxVal = num3;
return maxVal;}
main() Largest()
Function call
Passing parameters
Returning to main()
With return value
12
Tracing in VS
J:\Public_html\CS143\Labs\LargestByFunction
Break PointsLocalsAutos
Debug menu Start Debugging F5
Step OverTrace IntoLargest Returned
13
Tracing on Paper
Input values: 53 48 60
main() Largest() score1 score2 score3 max num1 num2 num3 maxVal ? ? ? ? ? ? ? ? 53 48 60
53 48 60
53
60
60
14
Schedule• Quiz4 -1 Submit to the Grader Due 10 pm Next Monday
• Lab 4
• Test 1 Friday
• Program 2 Due next Tuesday (better before Test 1) Grace: next Friday
15
Using Input File• Lab 4
• Program 2
• How to do it?– Lab4 description
16
How to Prepare for Test 1Test 1: Friday
60 PointsC++ Statements
TracingC++ Program
Other staff
Review Notes: 01 – 10Quizzes
LabsProg1Prog2
17
DO ALL QUIZZES!
STAR PROGRAMS EARLY!
18
19
DO NOT MISS CLASSES!