8
C++ Functions Revisited INFSY 307 C++

C++ Functions Revisited

Embed Size (px)

DESCRIPTION

INFSY 307 C++. C++ Functions Revisited. Must always consider three parts of the Program: Function Prototype: Can be in the header or top of a function (including main) Function Call: May be in ANY function - PowerPoint PPT Presentation

Citation preview

Page 1: C++ Functions Revisited

C++ Functions Revisited

INFSY 307

C++

Page 2: C++ Functions Revisited

Must always consider three parts of the Program:

• Function Prototype: Can be in the header or top of a function (including main)

2. Function Call: May be in ANY function (main ( ) or any other)

3. Function Header: Indicates the top of the function.

Page 3: C++ Functions Revisited

C++ Functions

Function Prototype void get_number ();

Function Callm.get_number ();

Function

void math::get_number ()

Note: 1) format of each!

2) typing and argument

consistency!

Page 4: C++ Functions Revisited

C++ Function – given a return value

Function Prototype int get_number ();

Function Call int x;

x=m.get_number ();

Function

int math::get_number ()

{

int num;

num=read_char_to_int ();

return num;

}

Page 5: C++ Functions Revisited

C++ Functions Function name

– Name must correspond to name in prototype Function type must correspond to type in

prototype Local variables only available during function

execution/global variables do not lose value

int math::get_number ()

{

int num;

num=read_char_toint ();

return num;

}

Page 6: C++ Functions Revisited

C++ Functions

CONSIDERATIONS! Provide function PRE and POST condition

comments under EACH the prototype

Others must know what is the beginning state of associated variables, what the function does, and what to expect as complete after execution.

Page 7: C++ Functions Revisited

Function Arguments:

1. Are contained in each critical component:a) Function callb) Function prototypec) Function Heading

2. Must match as far as:

a) typeb) order

Page 8: C++ Functions Revisited

C++ Sample Function with argument list and a return value

void samp::output (double value, int years, char name [])

{

cout <<setw (20)<<“ “<< “Name is: “<<name<<endl;

cout<<setw (20)<<“ “<<“Years in Program: “years<<endl;

cout<<setw (20)<<“ “<<“GPA: “<<value<<endl;

return;

}

int main ()

samp s;

char name [];

int yrs;

double gpa;

s.output (gpa, yrs, name); //assume are defined

.