1 coms 261 computer science i title: functions date: october 12, 2005 lecture number: 17

28
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

Upload: garry-holland

Post on 17-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

1

COMS 261Computer Science I

Title: Functions

Date: October 12, 2005

Lecture Number: 17

Page 2: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

2

Announcements

• Read – Read Sections 6.1 – 6.4 for lab

• Homework– Exercises: 5.9, 5.10, 5.11, 5.22, 5.25

– Due Friday, 10/14/05

• Project 1– Exercise 5.41, Pig Latin Translator

– Due Friday, 10/14/05

Page 3: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

3

Review

• Strings– class

• Data members• Member functions

• Functions– Function return type

– Parameter passing by value

– Copy by value

Page 4: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

4

Outline

• Functions

Page 5: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

Execution Process

• Function body of invoked function is executed

• Flow of control then returns to the invocation statement

• The return value of the invoked function is used as the value of the invocation expression

Page 6: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

Function Prototypes

• Before a function can appear in an invocation its interface must be specified–Prototype or complete definition

int max(int a, int b)

Type of value that

the function returns

FunctionType FunctionName ( ParameterList )

A description of the form the

parameters (if any) are to take

Identifier name of

function

Page 7: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

• Includes description of the interface and the function body– Interface

• Similar to a function prototype, but parameters’ names are required

– Body• Statement list with curly braces that comprises

its actions• Return statement to indicate value of invocation

Function Definition

Page 8: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

8

float circleArea (float r) {const float Pi = 3.1415;

return Pi * r * r;

}

Function Definition

Function body

Return statement

Local object definition

Return type Formal parameterFunction name

Page 9: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

9

Function Invocation

cout << circleArea(myRadius) << endl;

To process the invocation, the function that contains the insertion statement is suspended and circleArea() does its job. The insertion operator is then completed using the value

supplied by circleArea().

Actual parameter

Page 10: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

Simple Programs

• Single file– Include statements

– Using statements

– Function prototypes

– Function definitions

Page 11: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

Simple Programs

• Functions use value parameter passing– Known as pass by value or call by value

• The actual parameter is evaluated• A copy is given to the invoked function

Page 12: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

12

#include <iostream>using namespace std;float circleArea(float r);

// main(): manage circle computationint main() { cout << "Enter radius: "; float myRadius; cin >> myRadius; float area = circleArea(myRadius); cout << "Circle has area " << area; return 0;}// circleArea(): compute area of radius r circlefloat circleArea(float r) { const float Pi = 3.1415; return Pi * r * r;}

Page 13: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

Value Parameter Rules

• Formal parameter– Created on function invocation

– Initialized with value of the actual parameter

– Changes to formal parameter do not affect actual parameter

– Reference to a formal parameter produces the value for it in the current activation record

– New activation record for every function invocation

Page 14: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

Value Parameter Rules

• Formal parameter– Formal parameter name is only known

within its function

– Formal parameter ceases to exist when the function completes

– Activation record memory is automatically released at function completion

Page 15: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

15

Returnvalue

FunctionInput stream

data

Output streamdata

Information to functioncan come from

parameters or an inputstream

Parameters

Information fromfunction can comethrough a return

value or an outputstream

Page 16: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

PromptAndRead()

// promptAndRead(): prompt and extract next

// integer

int promptAndRead() {

cout << "Enter number (integer): ";

int response;

cin >> response;

return response;

}

Page 17: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

Sum()

// sum(): compute sum of integers in a ... b

int sum(int a, int b) {

int total = 0;

for (int i = a; i <= b; ++i) {

total += i;

}

return total;

}

Page 18: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

Problem

• Definition– Input two numbers representing a range of

integers and compute and display the sum of the integers that lie in that range

Page 19: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

Problem

• Design– Prompt user and read the first number

– Prompt user and read the second number

– Calculate the sum of integers in the range smaller...larger by adding in turn each integer in that range

– Display the sum

Page 20: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

20

Range.cpp

#include <iostream>using namespace std;

int promptAndRead();int sum(int a, int b);

int main() {int firstNumber = promptAndRead();int secondNumber = promptAndRead();int rangeSum = sum(firstNumber , secondNumber);cout << "The sum from " << firstNumber << " to " << secondNumber << " is " << rangeSum << endl;return 0;

}

Page 21: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

21

Range.cpp

// promptAndRead(): prompt & extract next integerint promptAndRead() {

cout << "Enter number (integer): ";int Response;cin >> Response;return Response;

}

// sum(): compute sum of integers in a ... bint sum(int a, int b) {

int total = 0;for (int i = a; i <= b; ++i) {

total += i;}return total;

}

Page 22: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

Blocks and Local Scope

• A block is a list of statements within curly braces

{

.

.

.

}

Page 23: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

Blocks and Local Scope

• Blocks can be put anywhere a statement can be put

• Blocks within blocks are nested blocks{

{

.

.

.

}

}

Block is nestedwithin another block

Page 24: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

Local Object Manipulation

void f() { int i = 1; cout << i << endl; // insert 1 {

int j = 10; cout << i << j << endl; // insert 1 10

i = 2; cout << i << j << endl // insert 2 10

} cout << i << endl; // insert 2 cout << j << endl; // illegal}

Page 25: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

Name Reuse

• A nested block can define an object with the same name as an enclosing block

• The new definition is in effect in the nested block

Page 26: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

26

Don’t Do This At Home

void f() { { int i = 1; cout << i << endl; // insert 1 { cout << i << endl; // insert 1 char i = 'a'; cout << i << endl; // insert a } cout << i << endl; // insert 1 } cout << i << endl; // illegal insert

}

Page 27: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

Global Scope

• Objects not defined within a block are global objects– A global object can be used by any function

in the file that is defined after the global object

– It is best to avoid programmer-defined global objects• Exceptions tend to be important constants

Page 28: 1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17

Global Scope

• Global objects with appropriate declarations can even be used in other program files– cout, cin, and cerr are global objects that

are defined in by the iostream library

• Local objects can reuse a global object's name– Unary scope operator :: can provide access to

global object even if name reuse has occurred