C++ For Science and Engineering Lecture 15 John Chrispell Tulane University Wednesday September 29, 2010
Function Review Recall the basics you already know about functions. Provide a function definition. Provide a function prototype. Provide a call to the function. Sometimes aspects of this are done for you (using a standard library or package). Other times we do them ourself. #include <iostream > void s i m p l e ( ) ; i n t main ( ) { s i m p l e ( ) ; void s i m p l e ( ) { s t d : : cout << I m but a s i m p l e f u n c t i o n. << s t d : : e n d l ; John Chrispell, Wednesday September 29, 2010 slide 3/23
More on functions. Functions can be broken into two groups: Functions that don t return values: void type. Functions that do return values: non void type. The goal of a function is to preform some simple task. A function that prints Go tigers! an integer number of times: void s i m p l e ( i n t n ){ f o r ( i n t i = 0 ; i < n ; i ++){ s t d : : cout << Go t i g e r s! << s t d : : e n d l ; // Has no r e t u r n s t a t e m e n t. John Chrispell, Wednesday September 29, 2010 slide 5/23
More on functions. Functions that don t return values: void type. void functionname ( P a r a m e t e r L i s t ){ s t atement ( s ) ; return ; // O p t i o n a l Functions that do return values: non void type. typename functionname ( P a r a m e t e r L i s t ){ s t atement ( s ) ; return v a l u e ; // v a l u e i s type c a s t // to type typename. Note if the function is of type double and we return an int. The value is type cast as a double. John Chrispell, Wednesday September 29, 2010 slide 7/23
Return Restrictions C++ can not return an array directly. You may return: basic types (double, int, char... ) structures pointers Note you may return an array that is part of a structure or object, even if you can t return the array directly. We also make note that a function terminates when it executes a return statement. i n t b i g g e r ( i n t a, i n t b ){ i f ( a > b ){ return a ; // t e r m i n a t e s f u n c t i o n e l s e { return b ; // t e r m i n a t e s f u n c t i o n. John Chrispell, Wednesday September 29, 2010 slide 9/23
Prototypes What does the prototype do? Prototypes describe the function interface to the compiler. Tells the compiler number and type of function arguments. Tells the compiler what type of value is returned. Prototyping makes compiling of large programs that span many files easier. Typically we place prototypes in header files so different parts of the code can access them. All functions may not live in the same file. The easy way to get a prototype is to copy the function header and add a semicolon to it. double cube ( double x ) ; double cube ( double ) ; // We can drop v a r i a b l e names. John Chrispell, Wednesday September 29, 2010 slide 11/23
Prototypes Note if you do use variable names in the prototype the do not nee to match the function definition. In classic C function prototyping is optional. In C++ prototyping is manditory. What prototypes do for you! Make the compiler correctly handle the function return value. The compiler checks that you use the correct number of arguments. The compiler checks that the arguments used are of correct type. If possible it converts argument to the correct type. John Chrispell, Wednesday September 29, 2010 slide 13/23
Passing by value C++ normally passes arguments by value. That means the numeric value of the argument is passed to the function, where it is assigned to a new variable. The values of variables are thus insulated inside main from the actions inside the function. A variable that is used to receive passed values is called a formal argument or formal parameter. Variables and parameters declared within a function are private to the function. When the function is called the computer allocates memory and frees the memory for the variables when the function terminates. Variables that are treated in this manner are automatic variables as they are automatically created and destroyed during program execution. John Chrispell, Wednesday September 29, 2010 slide 15/23
lotto.cpp #include <iostream > long double p r o b a b i l i t y ( unsigned numbers, unsigned p i c k s ) ; i n t main ( ) { using namespace std ; double total, choices ; cout << Enter the t o t a l number o f c h o i c e s on the game card and\n the number of p i c k s allowed : \ n ; while ( ( c i n >> t o t a l >> c h o i c e s ) && c h o i c e s <= t o t a l ){ cout << You have one chance i n ; cout << p r o b a b i l i t y ( t o t a l, c h o i c e s ) ; // compute the odds cout << of winning. \ n ; cout << Next two numbers (q to quit ) : ; cout << Done \n ; return 0 ; / ================================================================== / / The f o l l o w i n g f u n c t i o n c a l c u l a t e s the p r o b a b i l i t y o f p i c k i n g p i c k s / / numbers c o r r e c t l y from numbers choices. / / ================================================================== / long double p r o b a b i l i t y ( unsigned numbers, unsigned p i c k s ){ long double r e s u l t = 1. 0 ; // h e r e come some l o c a l v a r i a b l e s long double n ; unsigned p ; f o r ( n = numbers, p = picks ; p > 0 ; n, p ){ r e s u l t = r e s u l t n / p ; return r e s u l t ; John Chrispell, Wednesday September 29, 2010 slide 17/23
arrfun1.cpp #i n c l u d e <iostream > const i n t A r S i z e = 8 ; i n t sum arr1 ( i n t a r r [ ], i n t n ) ; // p r o t o t y p e i n t sum arr2 ( i n t arr, i n t n ) ; // prototype i n t main ( ) { using namespace std ; i n t c o o k i e s [ A r S i z e ] = { 1, 2, 4, 8, 1 6, 3 2, 6 4, 1 2 8 ; i n t sum = 0 ; / some systems r e q u i r e p r e c e d i n g i n t with s t a t i c to / / e n a b l e a r r a y i n i t i a l i z a t i o n / sum = sum arr1 ( cookies, ArSize ) ; cout << Total cookies eaten (1): << sum << \n ; sum = sum arr2 ( cookies, ArSize ) ; cout << Total cookies eaten (2): << sum << \n ; return 0 ; / r e t u r n the sum o f an i n t e g e r a r r a y / i n t sum arr1 ( i n t arr [ ], i n t n ){ i n t t o t a l = 0 ; f o r ( i n t i = 0 ; i < n ; i ++) t o t a l = t o t a l + a r r [ i ] ; return t o t a l ; / a second method means the same / i n t sum arr2 ( i n t arr, i n t n ){ i n t t o t a l = 0 ; f o r ( i n t i = 0 ; i < n ; i ++) t o t a l = t o t a l + a r r [ i ] ; return t o t a l ; John Chrispell, Wednesday September 29, 2010 slide 19/23
arrfun1.cpp Notes on the previous listing: The name of an array is the same as if it were a pointer. c o o k i e s == &c o o k i e s [ 0 ] // a r r a y name i s a d d r e s s o f f i r s t element. There are two exceptions to this rule. 1 Array declarations use the array name to lable the storage. 2 Applying sizeof to an array name yields the size of the whole array in bytes. The notations i n t a r r and i n t a r r [ ] // a r e o n l y synonymous i n the f u n c t i o n header and p r o t o t y p e. we can not use in arr[] to declare a pointer in the body of a function. RECALL a r r [ i ] == ( a r r + i ) // v a l u e s i n two n o t a t i o n s &a r r [ i ] == a r r + i // a d d r e s s e s i n two n o t a t i o n s John Chrispell, Wednesday September 29, 2010 slide 21/23
arrfun2.cpp #include <iostream > const i n t A r S i z e = 8 ; i n t sum arr ( i n t a r r [ ], i n t n ) ; // use s t d : : i n s t e a d o f u s i n g d i r e c t i v e i n t main ( ) { i n t c o o k i e s [ A r S i z e ] = { 1, 2, 4, 8, 1 6, 3 2, 6 4, 1 2 8 ; std : : cout << cookies << = array address, ; s t d : : cout << s i z e o f c o o k i e s << = s i z e o f c o o k i e s \n ; i n t sum = sum arr ( cookies, ArSize ) ; s t d : : cout << Total c o o k i e s e a t e n : << sum << s t d : : e n d l ; sum = sum arr ( c o o k i e s, 3 ) ; // a l i e s t d : : cout << F i r s t t h r e e e a t e r s a t e << sum << c o o k i e s. \ n ; sum = sum arr ( c o o k i e s + 4, 4 ) ; // a n o t h e r l i e std : : cout << Last f o u r e a t e r s ate << sum << c o o k i e s. \ n ; return 0 ; / ==================================== / / r e t u r n the sum o f an i n t e g e r a r r a y / / ==================================== / i n t sum arr ( i n t a r r [ ], i n t n ){ i n t t o t a l = 0 ; s t d : : cout << a r r << = a r r, ; s t d : : cout << s i z e o f a r r << = s i z e o f a r r \n ; f o r ( i n t i = 0 ; i < n ; i ++){ t o t a l = t o t a l + a r r [ i ] ; return t o t a l ; John Chrispell, Wednesday September 29, 2010 slide 23/23