19
1 CS 1430: Programming in C++

1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

Embed Size (px)

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 Parameters

Citation preview

Page 1: 1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

1

CS 1430: Programming in C++

Page 2: 1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

2

Quiz 1

Page 3: 1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

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

Page 4: 1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

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

Page 5: 1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

5

Function prototype

int Largest(int num1, int num2, int num3);

Page 6: 1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

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; }

Page 7: 1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

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.

Page 8: 1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

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; }

Page 9: 1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

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.

Page 10: 1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

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; }

Page 11: 1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

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

Page 12: 1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

12

Tracing in VS

J:\Public_html\CS143\Labs\LargestByFunction

Break PointsLocalsAutos

Debug menu Start Debugging F5

Step OverTrace IntoLargest Returned

Page 13: 1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

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

Page 14: 1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

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

Page 15: 1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

15

Using Input File• Lab 4

• Program 2

• How to do it?– Lab4 description

Page 16: 1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

16

How to Prepare for Test 1Test 1: Friday

60 PointsC++ Statements

TracingC++ Program

Other staff

Review Notes: 01 – 10Quizzes

LabsProg1Prog2

Page 17: 1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

17

DO ALL QUIZZES!

Page 18: 1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

STAR PROGRAMS EARLY!

18

Page 19: 1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function

19

DO NOT MISS CLASSES!