1 / 16

Engr 0012 (04-1) LecNotes 20-01

Value-oriented. Address-oriented. int a;. int *pa;. 0065FDF4. 0065FE4D. a. 154. pa. 0065FDF4. int. int address. Variable (data object) declaration in C. two methods of variable declaration. address. address. name. name. type. type. a ==> value (154).

bkatherine
Download Presentation

Engr 0012 (04-1) LecNotes 20-01

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Value-oriented Address-oriented int a; int *pa; 0065FDF4 0065FE4D a 154 pa 0065FDF4 int int address Variable (data object) declaration in C two methods of variable declaration address address name name type type a ==> value (154) pa ==> address (0065FDF4) &pa ==> address (0065FE4D) &a ==> address (0065FDF4) *pa ==> value (154) Engr 0012 (04-1) LecNotes 20-01

  2. arithmetic and logical operators in C integer division whenever values on both sides of operator are integers otherwise, if one is floating point, promotion takes place Engr 0012 (04-1) LecNotes 20-02

  3. long division is performed integer division whenever values on both sides of operator are integers a = 38/3; (= 12) b = 38%3; (= 2) Engr 0012 (04-1) LecNotes 20-03

  4. subset type will be promoted to superset type before executing ==> a = 38.0/3.0; (==> 12.66666…) can safely assign values of subset type to superset type // variable declaration int a = 3; double b; … // algorithm b = a; promotion if values are of different type in an expression a = 38.0/3; promoting type int value to type double Engr 0012 (04-1) LecNotes 20-04

  5. demotion // variable declaration int a; double b = 3.2; … // algorithm a = b; demoting type double value to type int don’t do it - may lead to unexpected results Engr 0012 (04-1) LecNotes 20-05

  6. trace main() { // begin main // variable declaration int a = 3, b = 3, c = 2, d = 7, e, f; double alfa = 2.0, beta = 5.0, gamma = 4.0, delta, epsilon; // algorithm delta = (a*b/c)*gamma; e = d%b; epsilon = (alfa*beta/gamma)*b; beta = (1/2)*beta; f = d/b; printf( "\ndelta = %10.2f \ne = %d \nepsilon = %.3f" "\nbeta = %f \nf = %8d \n\n", delta, e, epsilon, beta, f); } // end main Engr 0012 (04-1) LecNotes 20-06

  7. type of value sent back by return statement values required, addresses shared functions prototype type name( parameter list ); double mycos( double angle ); void convert( double bearing, char *pface, double *pturn, char *pturndir ); int getint1( void ); void getint2( int *pvalue ); Engr 0012 (04-1) LecNotes 20-07

  8. returns type double value through return statement requires type double value to do its job returns no values through return statement requires one type double value to do its job and shares three addresses with calling function functions double mycos( double angle ); void convert( double bearing, char *pface, double *pturn, char *pturndir ); Engr 0012 (04-1) LecNotes 20-08

  9. returns type int value through return statement requires no values to do its job returns no values through return statement shares one address with calling function functions int getint1( void ); void getint2( int *pvalue ); Sharing an address means that both the function called and the calling function have access to the information at that address (and can change the information at the address) Engr 0012 (04-1) LecNotes 20-09

  10. agreement between value-oriented declaration and address provided in scanf agreement between declared type and placeholder functions int getint1( void ) */ function purpose function needs function results */ { // begin getint1 // variable declaration int value; // algorithm printf( “Please enter an integer ==> ” ); scanf( “%d”, &value ): return( value ); } // end getint1 Engr 0012 (04-1) LecNotes 20-10

  11. agreement between address-oriented declaration and address provided in scanf agreement between declared type and placeholder functions void getint2( int *pvalue ) */ function purpose function needs function results */ { // begin getint2 // algorithm printf( “Please enter an integer ==> ” ); scanf( “%d”, pvalue ): } // end getint2 Engr 0012 (04-1) LecNotes 20-11

  12. functions functions can be called from any other function functions can even call themselves (recursion) rules on calling statements must have same number of parameters as prototype each parameter must be of same type value parameters require values values can be supplied in at least five ways values could be promotable but not demotable address parameters require addresses Engr 0012 (04-1) LecNotes 20-12

  13. functions rules on calling statements, continued variable type is never used in calling statement a void parameter list is denoted by empty parentheses values sent back by the return statement may be captured with an assignment statement (otherwise, return statement values are lost) Engr 0012 (04-1) LecNotes 20-13

  14. prototype double mycos( double angle ); call accel = mycos( 3.2 ); providing an actual value an arithmetic expression a function call an (proper) variable name functions examples potential = mycos( 4.0*PI*current/3.0 ); position = mycos( 2 ); force = mycos( x ); velocity = mycos( *ptime ) intensity = mycos( sin(x) ); values can be provided by: Engr 0012 (04-1) LecNotes 20-14

  15. prototype void convert( double bearing, char *pface, double *pturn, char *pturndir ); call convert( 333, &face, &turn, &direction ); prototype int getint1( void ); call choice = getint1( ); prototype void getint2( int *pvalue ); call getint2( &choice ); functions examples Engr 0012 (04-1) LecNotes 20-15

  16. trace int trace( int v1, int *pv2, int *pv2); main() { // begin main // variable declaration int a, b, c; // algorithm a = trace(1, &b, &c); printf( "\na = %d \nb = %d \nc = %d" a, b, c); } // end main //************************************** int trace(int v1, int *pv2, int *pv3) { // begin trace // algorithm printf(“Enter integer ==> ”); scanf( “%d”, pv2); *pv3 = v1*(*pv2); return(v1 + (*pv2) + (*pv3) ); } // end trace Engr 0012 (04-1) LecNotes 20-16

More Related