Top Banner
1 Lab Session-VI CSIT-120 Lab Session-VI CSIT-120 Fall 2000 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL) We will learn how to write independent functions that can be called from main() We do not use parameter passing, however, we learn how functions can return values Lab VI Continues (FINAL SESSION)
29

1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

1

Lab Session-VI CSIT-120 Lab Session-VI CSIT-120 Fall 2000Fall 2000

• Let us look at C++ syntax rules in brief• Next, we complete the lab session-V• Lab session-VI deals with functions (OPTIONAL)• We will learn how to write independent functions

that can be called from main()• We do not use parameter passing, however, we

learn how functions can return values• Lab VI Continues (FINAL SESSION)

Page 2: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

2

C++ Syntax Rules in BriefC++ Syntax Rules in Brief

• #include <iostream.h>

• #…..other include files go here

• void main(void)

• {– Declarations of variables and constants

– Assignment and control statements

• }

Page 3: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

3

C++ Syntax Rules in BriefC++ Syntax Rules in Brief

• Make it a habit to put declarations in the beginning. For example,– int employeeID;

– const int current-year=2000;

– float deduction-amount;

• Once a data item has been declared, do not mention its data type in assignment statement

• int employeeID=23; is WRONG• employeeID=23; isRIGHT

Page 4: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

4

C++ Syntax Rules in BriefC++ Syntax Rules in Brief

• If you write a control statement, its conditions must be in parenthesis with no semicolon after the parenthesis

• For example, displaying employeeID if employeeID is 24;

• if (employeeID == 24)

• { cout<<“ID is “<<employeeID<<endl; }

• Please note “double-equal” in comparing, no semicolon after comparison statement and joining several displayable messages with <<

Page 5: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

5

C++ Syntax Rules in BriefC++ Syntax Rules in Brief

• Different comparison statements• if (employeeID != 24) { cout<<endl;}• (if employeeID is not equal to 24, do a blank cout)• if (employeeID >= 24)• if (employeeID <=24)• if (employeeID > 24)• if (employeeID < 24)• We could also use an else clause to capture the false

results of comparison

Page 6: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

6

C++ Syntax Rules in BriefC++ Syntax Rules in Brief

• For example, if employeeID is 24, display “ID is 24” else do a blank line display

• if (employeeID == 24)• { cout<<“ID is “<<employeeID<<endl; }

• else {cout<<endl;}• Use braces to show blocks of code even if

only a single statement is there in if part or else part (for clarity purposes)

Page 7: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

7

C++ Syntax Rules in BriefC++ Syntax Rules in Brief

• In assigning values, there can be only one destination shown on left of the single equal sign

• deductions = pay- (pay*tax-percentage);

• Please note the use of parenthesis to emphasize and make the expression very clear

Page 8: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

8

The for StatementThe for Statement

• “for” is a loop statement that is controlled through a loop control variable

• for (lcv=1; lcv<=100; lcv++)• The above loop will start with lcv=1 and it

will run until lcv equals 100. The step size is 1 (lcv++)

• Compare it to the above• for (lcv=1; lcv<=100; lcv+=2)

Page 9: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

9

The for StatementThe for Statement

• “for” statement and while statement can produce identical loops

• “for” is preferred if the number of iterations is known beforehand. “while” is preferred if the total iterations cannot be computed in advance

Page 10: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

10

Logical OperatorsLogical Operators

• Logical AND is represented by &&

• Logical OR is represented by ||

• Logical NOT is represented by !

• Comparing if equal is represented by ==• Example: Write a logical expression to be true if x is

restricted between 1 and 100• Write a logical expression to check if x equals

y-50 and x is positive• Experiment 5.5

Page 11: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

11

Lab ExerciseLab Exercise

• Develop a program that takes a number from the user and then enters a loop to keep displaying the result of successively multiplying the number by 2. The loop is terminated when the number exceeds half of INT_MAX (Do not forget to include the header file limits.h)

• Example: 2,4,8,16,32,64,…………..

Page 12: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

12

FunctionsFunctions

• Functions are “subroutines”, “procedures”, “modules” or “program units”.

• Functions perform a given task, usually one function is dedicated to one task

• One advantage of using functions is to avoid repeated coding for the same job

Page 13: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

13

Functions Reduce Code SizeFunctions Reduce Code Size

• For example, consider a program that prints text in a box, something like below

• ****************************************

• ****************************************

• ****************************************

• * *

• * The Journey Through Central Spine *

• * *

• ****************************************

• ****************************************

• ****************************************

• We can achieve this printout with a program containing 9 cout statements OR we can develop a function to print aesterik lines

Page 14: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

14

Program with 9 cout LinesProgram with 9 cout Lines

• cout<<“****************************************”<<endl;

• cout<<“****************************************”<<endl;

• cout<<“****************************************”<<endl;

• cout<<“* *”<<endl;

• cout<<“* The Journey Through Central Spine *”<<endl;

• cout<<“* *”<<endl;

• cout<<“****************************************”<<endl;

• cout<<“****************************************”<<endl;

• cout<<“****************************************”<<endl;.

Page 15: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

15

Same Program with a FunctionSame Program with a Function

• Print_three_star_lines();• cout<<“* *”<<endl;

• cout<<“* The Journey Through Central Spine *”<<endl;• cout<<“* *”<<endl;

• Print_three_star_lines();

• Now the program is more compact and readable. Actual function definition will be developed after the main() function’s code

• DEMONSTRATION OF PROGRAM

Page 16: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

16

Function Specific ActionsFunction Specific Actions

• If you plan to work with functions, make sure that you name your functions according to C++ naming conventions

• (letters, underscores, digits, no digits in the beginning)

• As demonstrated, function data type is specified before its name. If the function does not return anything, void is used

• Also note the DECLARATION before the actual function code, ending with a ;

Page 17: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

17

Function CallFunction Call

• Notice the function name is simply repeated when it is called from the main routine

• print_three_star_lines();

• The empty parenthesis indicate the fact that no data is passed to this function. A semicolon is a must to terminate any C++ statement (except loop starters)

Page 18: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

18

The Events The Events

• When a function is called from the main routine, the following events take place

• Control (of the running program) is transferred to the function as well as any data that may be needed

• Function performs its task and at the end, transfers the control back to the calling routine

Page 19: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

19

Why does the Function name Why does the Function name appear so many times?appear so many times?

• Count the number of times the function name is repeated in our program

• The name appears each time for a different purpose

• Firstly we have to declare the function. Function declaration is called its prototype

• void print_three_star_lines();

Page 20: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

20

Why does the Function name Why does the Function name appear so many times?appear so many times?

• Next, we have to define the function,i.e. its actual source code

• void print_three_star_lines() {………….}

• Finally, we have to make the function call

• print_three_star_lines();

• What do you know about main function when it reads void main(){……….}?

Page 21: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

21

Lab VI ContinuedLab VI Continued

• We look at some C++ code segments

• We solve one programming problem

• Lab Assignment#5 due 12/7

Page 22: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

22

Basic ConceptsBasic Concepts

• What will be the output? Don’t run this code, just predict

• void main()• {• int num1,num2;• num1=12345; num2=6789;• cout<<num1<<num2<<endl;• }

Page 23: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

23

Basic ConceptsBasic Concepts

• What is wrong with this code segment?• void main()• {• int num1, myloop;• while (myloop<24)• cout<<“Once in myloop, show no mercy”<<endl;• }

Page 24: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

24

Basic ConceptsBasic Concepts

• What is wrong with this code segment?• Void main()

• { int mybirthdate;

• mybirthdate =2;

• while (mybirthdate<24);

• {

• cout<<“Enter my birth date (date only)”<<endl;

• cin>> mybirthdate;

• }

• cout<<“you got it…………\n”;

• }

Page 25: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

25

Basic ConceptsBasic Concepts

• What is the error in this code segment?

• int cans;

• for(int cans=1; cans<=100; cans++) {

• cout<<“Each one has a refund of 5 cents\n”;

• cout<<“count until 100 cans are done”;

• cans--;

• }

Page 26: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

26

Basic ConceptsBasic Concepts

• Fix this code segment

• int k;

• cout<<“Number please”; cin>>k;

• if (k=5) cout<<“You typed 5”;

• else cout<<“Nothing else please”;

Page 27: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

27

Lab Programming ExerciseLab Programming Exercise

• Write a program that accepts a digit entered by a user and then displays it in words.

• For example, user enters 8, program displays, “You Entered Eight”

• User enters 0, program displays “You Entered Zero”

• and so on………..

Page 28: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

28

Lab Assignment#5 Due 12/7Lab Assignment#5 Due 12/7

• A car can hold 12 gallons of gasoline and it can travel 360 miles without refuelling. Write a program the displays the trip meter of the car followed by amount of fuel left. Every time the user enters letter ‘r’, 10 miles are added to the trip meter and appropriate amount of fuel is deducted. As the amount of fuel becomes 1 gallon, the program prints a warning.

Page 29: 1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)

29

Sample OutputSample Output

• s• Miles so far: 160 Fuel left: 6.66667• s• Miles so far: 160 Fuel left: 6.66667• r• Miles so far: 170 Fuel left: 6.33333• r• Miles so far: 180 Fuel left: 6