Upload
lara-morse
View
18
Download
0
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
C++ Functions Revisited
INFSY 307
C++
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.
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!
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;
}
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;
}
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.
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
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
.