70 likes | 155 Views
Engr 0012 (04-1) LecNotes 21-01. ca20a.cpp. formal parameters are found in prototype and definition. // prototypes void fcn1( int b, int a ); main() { // begin main // variable declaration int a = 5, b = 7; // algorithm printf( "<br>IN main - Before call to fcn1"
E N D
ca20a.cpp formal parameters are found in prototype and definition // prototypes void fcn1( int b, int a ); main() { // begin main // variable declaration int a = 5, b = 7; // algorithm printf( "\nIN main - Before call to fcn1" "\n a = %d, b = %d ", a, b ); fcn1( a, b ); printf( "\nIN main - After call to fcn1" "\n\n a = %d, b = %d ", a, b ); } // end main //************************************** void fcn1( int b, int a ) { // begin fcn1 // algorithm b = b*a; a = a+b; printf( "\n\nIn fcn1:" " a = %d, b = %d ", a, b ); } // end fcn1 formal parameters actual parameters are found in calling statment actual parameters formal parameters functions & parameter/argument lists listed singly with data type listed singly w/o data type both formal & actual parameters are value parameters valuestransferred from calling statement to function for use original values cannot be altered by function Engr 0012 (04-1) LecNotes 21-02
ca20a.cpp // prototypes void fcn1( int b, int a ); main() { // begin main // variable declaration int a = 5, b = 7; // algorithm printf( "\nIN main - Before call to fcn1" "\n a = %d, b = %d ", a, b ); fcn1( a, b ); printf( "\nIN main - After call to fcn1" "\n a = %d, b = %d ", a, b ); } // end main //************************************** void fcn1( int b, int a ) { // begin fcn1 // algorithm b = b*a; a = a+b; printf( "\n\nIn fcn1:" " a = %d, b = %d ", a, b ); } // end fcn1 functions & parameter/argument lists memory map/trace IN main - before call to fcn1 a = 5, b = 7 In fcn1: a = 42, b = 35 IN main - after call to fcn1 a = 5, b = 7 Engr 0012 (04-1) LecNotes 21-03
formal parameters are found in prototype and definition ca20b.cpp // prototypes void fcn2( int *pb, int *pa ); main() { // begin main // variable declaration int a = 5, b = 7; // algorithm printf( "\nIn main - before call to fcn2" "\n a = %d, b = %d ", a, b ); fcn2( &a, &b ); printf( "\n\nIn main - after call to fcn2" "\n a = %d, b = %d ", a, b ); } // end main //************************************** void fcn2( int *pb, int *pa ) { // begin fcn2 // algorithm (*pb) = (*pb)*(*pa); (*pa) = (*pa)+(*pb); printf( "\n\nIn fcn2," " a = %d, b = %d ", *pa, *pb ); } // end fcn2 formal parameters actual parameters are found in calling statment actual parameters formal parameters functions & parameter/argument lists listed singly with data type listed singly w/o data type formal & actual parameters are address or pointer parameters addressestransferred from calling statement to function for use original values can be altered by function Engr 0012 (04-1) LecNotes 21-04
ca20b.cpp // prototypes void fcn2( int *pb, int *pa ); main() { // begin main // variable declaration int a = 5, b = 7; // algorithm printf( "\nBefore call," " a = %d, b = %d ", a, b ); fcn2( &a, &b ); printf( "\n\nAfter call," " a = %d, b = %d ", a, b ); } // end main //************************************** void fcn2( int *pb, int *pa ) { // begin fcn2 // algorithm (*pb) = (*pb)*(*pa); (*pa) = (*pa)+(*pb); printf( "\n\nIn fcn2," " a = %d, b = %d ", *pa, *pb ); } // end fcn2 functions & parameter/argument lists memory map/trace In main - before call to fcn2 a = 5, b = 7 In fcn2: a = 42, b = 35 In main – after call to fcn2 a = 35, b = 42 Engr 0012 (04-1) LecNotes 21-05
LecNotes21a.cpp formal & actual parameters are both value and address or pointer parameters void fcn3( int x, int y, int *pq, int *pr ); //************************************************ main() { // begin main // variable declaration int a = 5, b = 7, c, d; // algorithm printf( "\nBefore call: " " a = %d, b = %d, c = %d, d = %d", a, b, c, d ); fcn3( a, b, &c, &d ); printf( "\n\nAfter call: : "\n a = %d, b = %d, c = %d, d = %d", a, b, c, d ); } // end main //************************************************ void fcn3( int b, int a, int *pq, int *pr ) { // begin fcn3 // variable declaration int h = 2; // algorithm *pq = h*(a + b); *pr = a*b/h; printf( "\n\nIn fcn3: " " a = %d, b = %d, *pq = %d, *pr = %d ", a, b, *pq, *pr ); } // end fcn3 formal parameters actual parameters formal parameters functions & parameter/argument lists both values and addressestransferred from calling statement to function for use only values referred to by address parameters can be altered by function Engr 0012 (04-1) LecNotes 21-06
LecNotes21a.cpp void fcn3( int x, int y, int *pq, int *pr ); //************************************************ main() { // begin main // variable declaration int a = 5, b = 7, c, d; // algorithm printf( "\nBefore call: " " a = %d, b = %d, c = %d, d = %d", a, b, c, d ); fcn3( a, b, &c, &d ); printf( "\n\nAfter call: : "\n a = %d, b = %d, c = %d, d = %d", a, b, c, d ); } // end main //************************************************ void fcn3( int b, int a, int *pq, int *pr ) { // begin fcn3 // variable declaration int h = 2; // algorithm *pq = h*(a + b); *pr = a*b/h; printf( "\n\nIn fcn3: " " a = %d, b = %d, *pq = %d, *pr = %d ", a, b, *pq, *pr ); } // end fcn3 functions & parameter/argument lists memory map/trace Before call: a = 5, b = 7, c = ??, d = ?? In fcn3: a = 7, b = 5, *pq = 24, *pr = 17 After call: a = 5, b = 7, c = 24, d = 17 Engr 0012 (04-1) LecNotes 21-07