> num; //Continue() Makes user input number to continue program void Continue() { int num; cout << "Enter a number to continue: " << endl; cin >> num; }"> > num; //Continue() Makes user input number to continue program void Continue() { int num; cout << "Enter a number to continue: " << endl; cin >> num; }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

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:

Similar presentations


Presentation on theme: "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:"— Presentation transcript:

1 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: 3.1-3.2  New files/handouts: Lab03 exercises, FunctionEx1.cpp, FunctionEx2.cpp

2 Monday, 9/23/02, Slide #2 Chapter 3: Simple Functions  Modularity:  More complex programs are built by putting previously written “program modules” together  In C++ each “program module” is a function, with a name.  Each C++ program must have at least one function, namely the function named main().  We can define more functions that can then be used (“called”) from main()

3 Monday, 9/23/02, Slide #3 Example 1: A Function for Lab03Exercise.cpp  This file uses the following code five times:  We can write a function named Continue() that does this code for us (see FunctionEx1.cpp): int num; cout << "Enter a number to continue: " << endl; cin >> num; //Continue() Makes user input number to continue program void Continue() { int num; cout << "Enter a number to continue: " << endl; cin >> num; }

4 Monday, 9/23/02, Slide #4 Basic Function Syntax  The simplest kind of function is a void function with no parameters (empty parentheses after name). The syntax for function definition is:  To use a void function we define in a program, we can  1. Put its definition above the main() function  2. Use the function name inside main() as a complete statement. void FunctionName() { //declarations and statements }

5 Monday, 9/23/02, Slide #5 Function parameters  The function Continue() needed no information to do its job.  Some functions must be supplied with data when called in order to work  Example: Again from Lab 03, we could write a function that computes and prints a weighted average  This function must be supplied with homework grade, exam grade, and participation grade  In the line that calls the function these values are put inside the parentheses. The function copies and uses these values

6 Monday, 9/23/02, Slide #6 Example 2: A function to compute and print a weighted average  See FunctionEx2.cpp: //PrintAverage() Computes and prints a weighted // average of homework, exam, and participation grades void PrintAverage(double hw, double ex, double pa) { double HwWeight = 0.50; double ExWeight = 0.35; double PaWeight = 0.15; double av = hw * HwWeight + ex *ExWeight + pa * PaWeight; cout << endl << "Student average = " << av << endl; }


Download ppt "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:"

Similar presentations


Ads by Google