University of Pittsburgh, Dept. of Computer Science

CS401: Lecture Notes: *Functions*

Date: *Mon/ 06/18/01*

Coverage: *Chapter 5, section 7-11, Chapter 6*

Scribe: *Michael Sikorski*


Notes from June 18,2001

 

Housekeeping:

 

                Results from exam1

                                                                                    Average:      124.7

                                                                                    Std Dev:       19.1

                                                                                    90%              23

                                                                                    80%              25

                                                                                    70%              11

                                                                                    69%              10

 

                Exam 1 is 15% of total grade

                150 points

 

                Proj3 due Wed 6/20

                Read Chap 6

 

-----------------------------------------------------------------------------------------------------------------------

 

Functions

 

                Types                                                         Name                                                                  Types

 

 

What functions are not:

 

                Wrong:       x,y = f (1,2)               >> trying to use 2 return values

                                                               ** functions return a single value

 

            Wrong:       x = sqrt ("four")         >> "four" is a string (type mismatch)

                                                               ** functions only work on prescribed types

 

            Strange:      pow (2,3)                  >> absolutely useless

                                                               ** no assignment (=)

                                                               ** it will compile

                                                               ** use the result, unless you have a "void" function

 

Function Interface: 

 

            interface -- general form -- like buttons on a TV set

 

            Return Type        Function Name ( Parameter List)

                                                                                |

                                                                               \/

                                                                   Parameter Declarations

                                                                                |

                                                                               \/

                                                                       Type Name

 

Function Prototype:

            --> defines the interface of a function

            --> answers the question - "How do I use the function"

 

                                 Return Type    Name               Parameter List

            e.g.             int calcAnswer ( int x, int y );

 

Note: It is not necessary to give parameter names in a prototype

 

            e.g.             double sqrt ( double)                   from math library

 

 

 

Story Time:

            Bob & Doug McKenzy -- "Great White North" : example

 

            There answer to converting to metric: double it and add 30

 

                              sample code:

                              // Bob and Doug

                              #include

                                   :

                                   :

                              using namspace std;

 

                              int BDtoMetric ( int );  //prototype   Done outside main block {}

                              int main() {

                                          int valtoconvert, result

                                          cout << "Gimme a positive integer, you hoser!";

                                          cin >> valtoconvert;

                                          result = BDtoMetric(valtoconvert);   //valtoconvert is the argument

                                          cout << "Thats " << result <<  " in metric, eh\n";

                              }

                             

                              // Definition of BDtoMetric

                              int BDtoMetric ( int x) {

                                          int result;

                                          result = 2 * x + 30;

                                          return (result );

                              }

 

                                          OR

 

                              int BDtoMetric ( int x ) {

                              return ( 2 * x +30 );                  // more simplistic way

                              }

 

Definition of Funtion (cont) Procedure

            1) Checks name

            2) Checks parameters

            3) Checks return type

            4) Transfer of control

 

 

 

Void Function

 

            e.g.             #include <>                                                            

                                   :

                                   :

                              using namespace std:

 

                              void myFunct();

                              int main ()   {

                                          cout << "in the main()\n";

                                          myFunct();

                                          cout << "back in the main() \n"

                                          return 0;

                              }

                              void myFunct() {

                                          cout << "inside myFunct()\n";

                              }

 

            output

            ___________________________

            | In the main()

            | inside inside myFunct()

            | back in Main()

 

 

 

Programming Flow in Functions

 

           

 

                             

 

                              bool acceptableRH ( double );

 

                              int main() {

                                          double relHum;

                                          cout << "enter relative humidity ";

                                          cin >> relHum;

                                          while (!acceptableRH ( relHum )) {

                                                   cout << "try again" ;

                                                   cin >> relHum:

                                          }

                              }

 

                              bool acceptableRH (double );  // outside of main body

 

                              bool acceptableRH (double d) {

                                          if (d >= 0.0) && (d <= 1.0 )) {

                                                   return (true);

                                          else

                                                   return (false);

                                          }

 

            faster way:  return ((d>=0.0) && (d<=1.0));

 

 

e.g.                         bool inAcceptableRange ( double d, double low, double high) {

                                          return ((d>= low) && (d<=high)) ;

                              }

 

                              // plugging in your own upper and lower limits

                              while (!inAcceptableRange (relHum, 0.0, 1.0){

                              :

                              }

 

e.g.                         UPS Package Example:

 

Specifications:

            1) Regular package default

            2) Oversize (OS1)

                              a) length + girth > 84inches

                              b) length + girth <= 108 inches

                              c) wt < 30lbs

            3) Oversize (OS2)

                              a) length + girth >108 inches

                              b) wt < 70 lbs

            4) Not Shippable

 

 

Needed:

            1) function for girth:  2* (lw + sw)

                              input: lw long width

                                       sw short width

 

            2) length - straight forward

            3) return int ,           0 - reg

                                          1 - OS1

                                          2 - OS2

                                          -1 - not shipable

            4) return OS1 - bool

            5) return OS2 - bool

 

 

END of CLASS: