Engr 0012 (04-1) LecNotes Variable (data object) declaration in C two methods of variable declaration 154 a int 0065FDF4 Value-oriented int a; address name type a ==> value (154) &a ==> address (0065FDF4) 0065FDF4 pa int address 0065FE4D Address-oriented int *pa; address name type pa ==> address (0065FDF4) &pa ==> address (0065FE4D) *pa ==> value (154)
Engr 0012 (04-1) LecNotes 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 integer division whenever values on both sides of operator are integers a = 38/3; b = 38%3; (= 2) long division is performed (= 12)
Engr 0012 (04-1) LecNotes promotion if values are of different type in an expression a = 38.0/3; subset type will be promoted to superset type before executing ==> a = 38.0/3.0; (==> …) can safely assign values of subset type to superset type // variable declaration int a = 3; double b; … // algorithm b = a; promoting type int value to type double
Engr 0012 (04-1) LecNotes 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 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 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 ); type of value sent back by return statement values required, addresses shared
Engr 0012 (04-1) LecNotes functions double mycos( double angle ); returns type double value through return statement requires type double value to do its job void convert( double bearing, char *pface, double *pturn, char *pturndir ); returns no values through return statement requires one type double value to do its job and shares three addresses with calling function
Engr 0012 (04-1) LecNotes functions int getint1( void ); returns type int value through return statement requires no values to do its job void getint2( int *pvalue ); returns no values through return statement shares one address with calling function 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 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 agreement between declared type and placeholder agreement between value-oriented declaration and address provided in scanf
Engr 0012 (04-1) LecNotes functions void getint2( int *pvalue ) */ function purpose function needs function results */ { // begin getint2 // algorithm printf( “Please enter an integer ==> ” ); scanf( “%d”, pvalue ): } // end getint2 agreement between declared type and placeholder agreement between address-oriented declaration and address provided in scanf
Engr 0012 (04-1) LecNotes 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 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 functions examples double mycos( double angle ); prototype accel = mycos( 3.2 ); call potential = mycos( 4.0*PI*current/3.0 ); position = mycos( 2 ); force = mycos( x ); velocity = mycos( *ptime ) values can be provided by: intensity = mycos( sin(x) ); providing an actual value an arithmetic expression an (proper) variable name a function call
Engr 0012 (04-1) LecNotes functions examples void convert( double bearing, char *pface, double *pturn, char *pturndir ); prototype convert( 333, &face, &turn, &direction ); call int getint1( void ); prototype choice = getint1( ); call void getint2( int *pvalue ); prototype getint2( &choice ); call
Engr 0012 (04-1) LecNotes 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