Download presentation
Presentation is loading. Please wait.
Published byMarion Jean Carr Modified over 9 years ago
1
Engr 0012 (04-1) LecNotes 20-01 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)
2
Engr 0012 (04-1) LecNotes 20-02 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
3
Engr 0012 (04-1) LecNotes 20-03 integer division whenever values on both sides of operator are integers a = 38/3; b = 38%3; (= 2) long division is performed (= 12)
4
Engr 0012 (04-1) LecNotes 20-04 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; (==> 12.66666…) 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
5
Engr 0012 (04-1) LecNotes 20-05 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
6
Engr 0012 (04-1) LecNotes 20-06 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
7
Engr 0012 (04-1) LecNotes 20-07 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
8
Engr 0012 (04-1) LecNotes 20-08 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
9
Engr 0012 (04-1) LecNotes 20-09 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)
10
Engr 0012 (04-1) LecNotes 20-10 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
11
Engr 0012 (04-1) LecNotes 20-11 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
12
Engr 0012 (04-1) LecNotes 20-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
13
Engr 0012 (04-1) LecNotes 20-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)
14
Engr 0012 (04-1) LecNotes 20-14 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
15
Engr 0012 (04-1) LecNotes 20-15 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
16
Engr 0012 (04-1) LecNotes 20-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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.