21
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 실 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Embed Size (px)

DESCRIPTION

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 3 LAB ASSIGNMENTS( 실습과제 ) Lesson 6C Lab 6.5: 변수의 유효 범위 Lab 6.6: 정적 변수 vs 지역 변수 Lesson 6D Lab 6.7: 값의 반환, 함수의 과적 Lab 6.8: Student-Generated Code Assignments

Citation preview

Page 1: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter

실 습6-2WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Page 2: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 2Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

학습목표

1. 유효범위 (scope) 의 개념 학습2. 정적 변수 vs 지역 변수의 차이점 학습3. 값을 반환하는 함수 개발 방법 학습4. 함수의 과적 (overloading) 개념 학습5. 스트브와 드라이버를 통한 검사와 디버깅

Page 3: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 3Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

LAB ASSIGNMENTS( 실습과제 )

Lesson 6C Lab 6.5: 변수의 유효 범위 Lab 6.6: 정적 변수 vs 지역 변수

Lesson 6D Lab 6.7: 값의 반환 , 함수의 과적 Lab 6.8: Student-Generated Code Assignments

Page 4: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 4Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Lab 6.5 변수의 유효 범위

http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download “ scope.cpp” // This program demonstrates scope rules.// PLACE YOUR NAME HERE.

Exercise 1: 유효 범위에 대한 scope.cpp 프로그램의 출력 결과를 먼저 예측하여 본 후 실행 결과와 비교하여 보라 .

Page 5: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 5Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

#include <iostream>#include <iomanip>using namespace std;

// _____________ variable definitionsint sum1 = 0, sum2 = 0;// Function prototypevoid studyScope(int);int main(){ // ______________ variable declarations int number = 10;

cout << "Starting in main, the value of number is " << number << endl; cout << "Starting in main, the value of sum1 is " << sum1 << endl; cout << "Starting in main, the value of sum2 is " << sum2 << endl << endl; studyScope(number); cout << "Back in main, the value of number is " << number << endl; cout << "Back in main, the value of sum1 is " << sum1 << endl; cout << "Back in main, the value of sum2 is " << sum2 << endl << endl; return 0;}

scope.cpp

Page 6: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 6Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

/********************************************************* * studyScope * * This function exists to illustrate scope rules. * *********************************************************/void studyScope(int myParameter){ // ___________ variable definition studyScope's outer block int number = 5;

{// ____________ variable defeinition in studyScope's inner block int number = 1;

number++; cout << "In studyScope's inner block number is now " << number << endl; sum1 += myParameter; sum2 += number; } number++; cout << "In studyScope's outer block number is now " << number << endl << endl; sum2 += number;}

Page 7: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 7Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Lab 6.6 정적 변수 vs 지역 변수 http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download “money.cpp” // This program illustrates the use of local variables vs. static local// variables. It also illustrates the use of a default parameter.

Exercise 1: money.cpp 프로그램에서 함수에게 전달된 money 의 누산 합계를 유지하기 위해 지역 변수와 정적 변수를 사용한다 . 주석에 지시한 대로 프로그램을 완성하라 . ( 실행 예 )Adding $2.50The total of all amounts so far is:Regular Total: $2.50Static Total: $2.50Adding $1.25The total of all amounts so far is:Regular Total: $1.25Static Total: $3.75Adding default amount of $1.00The total of all amounts so far is:Regular Total: $1.00Static Total: $4.75The correct total should now be $4.75

Page 8: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 8Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

#include <iostream>#include <iomanip>using namespace std;// Function prototypevoid totalMoney(double amount __________); // Uses a default value of 1.00 if // no value is passed to the functionint main(){ cout << setprecision(2) << fixed << showpoint;

cout << "\nAdding $2.50 \n"; // Fill in the code to call the totalMoney function and pass it 2.50. cout << "\nAdding $1.25 \n"; // Fill in the code to call the totalMoney function and pass it 1.25.

cout << "\nAdding default amount of $1.00\n"; // Fill in the code to call the totalMoney function and use the // default argument value, rather than passing a value to the function. cout << "The correct total should now be $4.75 \n\n";

return 0;}

money.cpp

Page 9: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 9Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

/********************************************************************** * totalMoney * * This function is passed an amount of money, which it adds to two * * accumulators -- one is a local variable and the other is a static * * local variable. It then prints out the two totals to illustrate * * the difference in the two kinds of variables. * **********************************************************************/void totalMoney(double amount){ double regularTotal = 0.0; // Define a regular local double variable ____________ staticTotal = 0.0; // Define a static double variable

// Fill in the code to add the amount passed in to regularTotal. // Fill in the code to add the amount passed in to staticTotal.

// Display the new totals cout << "\nThe total of all amounts so far is: \n"; cout << "Regular Total: $" << regularTotal << endl; cout << "Static Total: $" << staticTotal << endl;}

Page 10: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 10Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Lab 6.7 값의 반환 , 함수의 과적 http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download “convertmoney.cpp ”

// This program will convert American dollars to euros, // pesos, and yen.

Exercise 1: 한화에 대한 오늘의 환율표를 인터넷에서 찾아 convertToWon 함수를 추가한 후 실행해 보자 .

Page 11: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 11Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

// This program will convert American dollars to euros, pesos, and yen.// PLACE YOUR NAME HERE.

#include <iostream>#include <iomanip>using namespace std;

// Function prototypesvoid convertToEuros(double dollars, double& euros);void convertToPesos(double dollars, double& pesos);void convertToYen (double dollars, double& yen);

// Global constant CONVERSION RATESconst double NUM_EUROS = .8218, // Number of euros to the dollar NUM_PESOS = 10.8, // Number of pesos to the dollar

NUM_YEN = 108.5; // Number of yen to the dollar

convertmoney.cpp

Page 12: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 12Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

int main (){ double dollars, euros, pesos, yen; cout << "Input the amount of American Dollars \n" << "you want converted to foreign currency: "; cin >> dollars; // Perform the conversions convertToEuros(dollars, euros); convertToPesos(dollars, pesos); convertToYen (dollars, yen);

// call Korean won // Display the results cout << fixed << showpoint << setprecision(2); cout << "\n$" << dollars << " = \n" << setw(17) << euros << " euros \n" << setw(17) << pesos << " pesos \n" << setw(17) << yen << " yen \n\n"; return 0;}

Page 13: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 13Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

/******************************************************************** * convertToEuros * * This function converts dollars to euros and places the result in * * a reference parameter so the result will be known to main. * ********************************************************************/void convertToEuros (double dollars, double& euros){ euros = dollars * NUM_EUROS; }/******************************************************************** * convertToPesos * * This function converts dollars to pesos and places the result in * * a reference parameter so the result will be known to main. * ********************************************************************/void convertToPesos (double dollars, double& pesos){ pesos = dollars * NUM_PESOS; }/******************************************************************** * convertToYen * * This function converts dollars to yen and places the result in * * a reference parameter so the result will be known to main. * ********************************************************************/void convertToYen (double dollars, double& yen){ yen = dollars * NUM_YEN; }

Page 14: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 14Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Exercise 2: euros, pesos, yen 변수를 참조로 사용하는 대신에 각 함수에서 변환한 값을 세 변수 각각에 배정하도록 수정하라 .

Exercise 3: Exercise 2 를 수정하여 변수에 값을 반환받는 대신에 이 반환 받은 값을 cout 문에서 바로 출력하게 하라 .

http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download “overloaded.cpp ”

Exercise 4: void multiConversion(double dollars, double& euros, double& pesos)

void multiConversion(double dollars, double& euros, double& pesos, double& yen) 과적함수 multiConversion 의 stub 를 완성하라 .

Page 15: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 15Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

// overloaded.cpp // This program will input American money and convert it to foreign // currency. It illustrates the use of overloaded functions. // PLACE YOUR NAME HERE.

#include <iostream>#include <iomanip>using namespace std;

// Function prototypes void multiConversion (double dollars, double& euros, double& pesos);void multiConversion (double dollars, double& euros, double& pesos, double& yen);

// Global constant CONVERSION RATESconst double NUM_EUROS = .8218, // Number of euros to the dollar NUM_PESOS = 10.8, // Number of pesos to the dollar

NUM_YEN = 108.5; // Number of yen to the dollar

overloaded.cpp

Page 16: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 16Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

int main (){ double dollars, euros = 0, pesos = 0, yen = 0;

cout << fixed << showpoint << setprecision(2); cout << "Input the amount in American Dollars to \n" << "convert to euros and pesos: $" ; cin >> dollars; // Fill in the code to call the multiConversion function that // converts dollars to euros and pesos. Note that it requires // 3 arguments. Be sure to place them in the correct order. cout << "\n$" << dollars << " equals " << euros << " euros and "

<< pesos << " pesos.\n\n"; cout << "Input the amount in American Dollars to \n" << "convert to euros, pesos, and yen: $" ; cin >> dollars; // Fill in the code to call the multiConversion function that // converts dollars to euros, pesos, and yen. Note that it requires // 4 arguments. Be sure to place them in the correct order. cout << "\n$" << dollars << " equals " << euros << " euros, "

<< pesos << " pesos, and " << yen << " yen.\n\n"; return 0;}

Page 17: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 17Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

/******************************************************************** * multiConversion * * This version of the overloaded multiConversion function converts * * dollars to euros and pesos. It has 3 parameters. * ********************************************************************/void multiConversion(double dollars, double& euros, double& pesos){ // Currently this function is just a stub. Remove the cout statement // below and replace it with the code needed to complete this function. cout << "\nThe 3-parameter multiConversion function was called \n" << "with the value " << dollars << " passed to it.\n"; }/******************************************************************** * multiConversion * * This version of the overloaded multiConversion function converts * * dollars to euros, pesos, and yen. It has 4 parameters. * ********************************************************************/void multiConversion(double dollars, double& euros, double& pesos, double& yen) { // Currently this function is just a stub. Remove the cout statement // below and replace it with the code needed to complete this function. cout << "\nThe 4-parameter multiConversion function was called \n" << "with the value " << dollars << " passed to it.\n"; }

Page 18: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 18Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Lab 6.8 독립적인 소형 프로그램 개발 (Stub & Driver)

도전 : 스타 선발 ( 교과서 p.373)

연예인 선발대회에 심사위원이 5 명 있으며 , 이들은 각 참가자에 대해 0 에서 10 사이의 점수를 준다 . 8.3 과 같이 소숫점을 가진 점수도 허용된다 .

참가자의 최종 점수는 받은 점수 중 최고점과 최하점을 버리고 남은 세 점수의 평균으로 결정된다 .

이와 같이 참가자의 점수를 계산하는 프로그램을 작성하라 . 프로그램은 다음 함수를 사용하여야 한다 .

Page 19: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 19Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

 void getJudgeData() 는 사용자에게 한 심사위원의 점수를 요구하고 값을 참조 매개변수에 저장한 다음 그 값의 유효성을 검증한다 . 이 함수는 심사위원의 점수가 모두 입력되기 위해 main 에 의해 5 번 호출된다 .

getJudgeData(score1); // 각 score 는 double getJudgeData(score2); getJudgeData(score3); getJudgeData(score4); getJudgeData(score5);

 void calcScore() 는 행위자가 받은 점수 중 최고점과 최하점을 버리고 남은 세 점수의 평균을 계산하고 출력한다 . main 에 의해 한 번만 호출되며 다섯 개의 점수를 전달받는다 .

calcScore 는 다음 두 함수에 의해 반환된 정보를 사용하여 어떤 점수를 제외할지를 결정한다 . int findLowest() 는 전달된 다섯 개의 점수 중 최하점을 찾아 반환한다 . int findHigest() 는 전달된 다섯 개의 점수 중 최고점을 찾아 반환한다 .

Page 20: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 20Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

// Function prototypesvoid getJudgeData(double &);void calcScore(double, double, double, double, double);

double findLowest(double, double, double, double, double);

double findHighest(double, double, double, double, double);

Page 21: Copyright  2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE

Slide 5- 21Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Thanks!!!!!!!!