Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6 WEEK 1 - INTRODUCTION TO VOID FUNCTIONS (PROCEDURES)

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
Introduction to Flowcharting
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 11 – Fundraiser Application: Introducing Scope.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 1- 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2- 1.
CONTROL STRUCTURES: SEQUENTIAL, SELECTIVE, AND REPETITIVE
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 9- 1.
Computer Science 1620 Loops.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
© 2010 Pearson Education, Inc. All rights reserved.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2005 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Monday, 9/23/02, Slide #1 CS 106 Intro to CS 1 Monday, 9/23/02  QUESTIONS??  Today:  Discuss Lab 3  Do Exercises  Introduction to functions  Reading:
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
11.3 Function Prototypes A Function Prototype contains the function’s return type, name and parameter list Writing the function prototype is “declaring”
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
© 2010 Pearson Education, Inc. All rights reserved.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Part 1 Conditionals and Loops.
1 10/30/06CS150 Introduction to Computer Science 1 Functions Chapter 3, page 313.
Chapter 6: Functions.
Software Engineering 1 (Chap. 1) Object-Centered Design.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Functions Programming in C++ Computer Science Dept Va Tech August 2000 ©2000 William D McQuain 1 #include // for file streams for input/output #include.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Limits.
Chapter 1 Quiz Questions (CGS-3464) Mahendra Kumar
Introduction to PASS, and Exercises on Operators and Basic I/O.
1 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11.5 Lines and Curves in Space.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 4 Applications of the Derivative.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1 Functions.
Fundamental Programming: Fundamental Programming Introduction to C++
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 3.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 7 WEEK 1 – 구조체 (STRUCTURES)
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
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 실 습실 습 4.
Arrays float Scores[9]; ? index: element // one dimensional array 1.
Fundamental Programming Fundamental Programming Data Processing and Expressions.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Starting Out with C++: From Control Structures through Objects
Presentation transcript:

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6 WEEK 1 - INTRODUCTION TO VOID FUNCTIONS (PROCEDURES)

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 2 학습목표 1. 모듈성 (modularity ) 의 중요성 학습 2. 함수 ( 프로시져 ) 개발과 호출 방법 학습 3. 함수원형 (prototype) 의 역할 4. 매개변수 (parameter) 가 없는 함수 5. 함수에 매개변수 전달 방법 6. 실 매개변수와 형식매개변수 이해 7. 값에 의한 전달 vs. 참조에 의한 전달 8. 소형 프로그램 개발 ( 프로시져 )

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 3 LAB ASSIGNMENTS( 실습과제 ) Lesson 6A Lab 6.1: 매개변수가 없는 함수 Lab 6.2: 값에 의한 전달 Lesson 6B Lab 6.3: 참조에 의한 전달 Lab 6.4: Student-Generated Code Assignments

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 4 Lab 6.1 매개변수가 없는 함수 에서 download “ proverb.cpp ” // This program prints the proverb // "Now is the time for all good men to come to the aid of their party." // in a function (procedure) called by the main function Exercise 1: proverb.cpp 프로그램의 시작 부분에 있는 주석의 격언을 출력하도록 프로그램을 완성하라. main 함수에서 proverb 함수를 호출하게 하고, 호출된 proverb 함수에서 격언을 출력하게 하라.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 5 // This program prints the proverb // "Now is the time for all good men to come to the aid of their party." // in a function (procedure) called by the main function. // PLACE YOUR NAME HERE. #include using namespace std; void proverb(); // prototype for the proverb function int main() { // Fill in the code to call the proverb function. return 0; } /*********************************************************** * proverb * * This function prints a proverb. * ***********************************************************/ void proverb() { // Fill in the body of the function that prints to the screen // the proverb given in the comments at the beginning of the program. } proverb.cpp

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 6 Lab 6.2 값에 의한 전달 실습 에서 download “newproverb.cpp” Exercise 1: 다음과 같이 동작하도록 프로그램을 완성하라. asks the student to complete the program so that it allows the user to choose, by inputting a number, what the last word of the proverb will be. An input of 1 should cause the proverb to end with the word “party” and any other number to have it end with the word “country.”

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 7 // This program will allow the user to input from the keyboard whether // the last word to the following proverb should be party or country. // "Now is the time for all good men to come to the aid of their ________. // Inputting a 1 will print "party". Any other number will print "country". // PLACE YOUR NAME HERE. #include using namespace std; // Fill in the prototype of the proverb function. int main () { int wordCode; cout << "Given the phrase:" << endl; cout << "Now is the time for all good men to come to the aid of their ___\n"; cout << "Input a 1 if you want the sentence to be finished with party.\n"; cout << "Input any other number for the word country.\n"; cout << "Please input your choice now.\n"; cin >> wordCode; cout << endl; proverb(wordCode); return 0; } newproverb.cpp

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 8 /********************************************************************* * proverb * * This function takes number from the call. If the number is a 1 * * it prints "Now is the time for all good men to come to the aid of* * their party." Otherwise it prints "Now is the time for all good * * men to come to the aid of their country." * *********************************************************************/ void proverb (int number) { // Fill in the body of the function to accomplish what is described above. }

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 9 Exercise 2: 위 프로그램을 수정하여 입력 값이 1 일 경우는 끝에 “party” 를 2 일 경우는 “country” 그리고 다른 값일 경우는 오류 메시지와 함께 다시 입력을 시도하게 하라. ( 자료 검증을 위한 루프문 사용 ) Given the phrase: Now is the time for all good men to come to the aid of their ___ Input a 1 if you want the sentence to be finished with party. Input a 2 if you want the sentence to be finished with country. Please input your choice now. 4 I am sorry but that is an incorrect choice; Please input a 1 or 2. 2 Now is the time for all good men to come to the aid of their country.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Exercise 3: 위 프로그램을 수정하여 사용자가 격언의 마지막 단어를 입력하게 하라. (proverb 함수 매개변수 자료형의 수정 요구 !) Given the phrase: Now is the time for all good men to come to the aid of their ___ Please input the word you would like to have finish the proverb. family Now is the time for all good men to come to the aid of their family.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Lab 6.3 참조에 의한 전달 실습 에서 download “paycheck.cpp ” // This program takes two numbers (pay rate and hours) and multiplies them // to get gross pay. It then calculates net pay by subtracting 15%. Exercise 1: paycheck.cpp 를 완성하라. 사용자로부터 시간당 수당 금액과 근무 시간을 입력 받아 함수 paycheck 함수에서 grosspay 와 netpay 를 계산한 다음 이들 값을 참조에 의해 전달된 main 함수에서 출력하게 하라.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide // PLACE YOUR NAME HERE. #include using namespace std; // Function prototypes void description(); void paycheck(double, int, double&, ________ ); int main() { double payRate, grossPay, netPay; int hours; cout << setprecision(2) << fixed; cout << "Welcome to the Payroll Program." << endl; description(); cout << "Please input the pay per hour: "; cin >> payRate; cout << endl << "Please input the number of hours worked: "; cin >> hours; cout << endl; paycheck(payRate, hours, grossPay, ______ ); // Write statements to print out both gross pay and net pay, with labels. cout << "We hope you enjoyed this program." << endl; return 0; } paycheck.cpp

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide /******************************************************* * description * * This function prints a program description * *******************************************************/ void description() { cout << "************************************************" << endl << endl; cout << "This program takes two numbers (pay rate and hours)" << endl; cout << "and outputs net pay. "<< endl; cout << "************************************************" << endl << endl; } /******************************************************* * paycheck * * This function computes gross and net pay, placing * * the results in reference parameters. * *******************************************************/ void paycheck(double rate, int time, double& gross, ______________) { // Write an assignment statement to compute gross pay. // Write an assignment statement to compute net pay. }

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Exercise 2: 다음과 같은 자료에 대해 출력 결과가 나오게 프로그램을 실행하여 보자. Welcome to the Payroll Program. ************************************************ This program takes two numbers (pay rate and hours) and outputs net pay. ************************************************ Please input the pay per hour: 9.50 Please input the number of hours worked: 40 The gross pay is $380 The net pay is $323 We hope you enjoyed this program.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Exercise 3: 프로그램의 paycheck 함수 내에서 gross 와 net 변수는 값에 의한 전달인가 ? 참조에 의한 전달인가 ? 값에 의한 전달을 사용하면 결과는 ? Exercise 4: gross 값과 net 값이 main 함수 대신에 paycheck 함수에서 출력되도록 수정하여 보자. 참조에 의한 전달을 사용할 필요가 있나 ? Exercise 2 에서 사용한 자료로 실행하여 보자. 같은 결과를 보여주는가 ?

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide All of the options for this lab require students to use functions and to pass parameters correctly. 선택 1: 두 수를 입력 받아 서로 값을 바꾸는 프로그램을 작성하라. 참조에 의한 전달을 사용하라. 첫 번째 수를 입력하시오 80 두 번째 수를 입력하시오. 70 입력한 수는 80 과 70 입니다. Swap 후 첫 번째 수는 80 입니다. Swap 후 두 번째 수는 70 입니다. Lab 6.4 독립적인 소형 프로그램 개발

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Option 2: This is a simple miles per hour problem requiring miles and hours to be passed to a function which will compute milesPerHour. The problem statement specifically requires that milesPerHour be passed as a parameter. Thus, even if a student understands value returning functions (not introduced until next week’s labs), one should not be used here because the idea is to know which parameters should be passed by value and which by reference. For a correct solution miles and hours should be passed by value and milesPerHour by reference. Option 3: This program has main call a convert function to convert the hours and minutes passed to it into hours. The problem statement does not specify whether there are to be two or three parameters. If three are used, hours and minutes should be passed by value and convertedHours, which holds the result, should be passed by reference. If only two parameters are passed, with the idea that hours will be updated to hold the resulting total hours, then minutes must be passed by value and hours must be passed by reference. Option 4: This option combines Options 2 and 3. First a convert function converts minutes and hours into hours so that this result can be passed to a mph function, along with miles traveled, to obtain the milesPerHour.

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