Download presentation
Presentation is loading. Please wait.
1
Engr 0012 (04-1) LecNotes 21-01
2
functions & parameter/argument lists
formal parameters are found in prototype and definition 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\n a = %d, b = %d ", a, b ); } // end main //************************************** void fcn1( int b, int a ) { // begin fcn1 b = b*a; a = a+b; printf( "\n\nIn fcn1:" " a = %d, b = %d ", a, b ); } // end fcn1 listed singly with data type actual parameters actual parameters are found in calling statment listed singly w/o data type both formal & actual parameters are value parameters values transferred from calling statement to function for use original values cannot be altered by function Engr 0012 (04-1) LecNotes 21-02
3
functions & parameter/argument lists
memory map/trace 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" } // end main //************************************** void fcn1( int b, int a ) { // begin fcn1 b = b*a; a = a+b; printf( "\n\nIn fcn1:" " a = %d, b = %d ", a, b ); } // end fcn1 IN main - before call to fcn1 a = 5, b = 7 In fcn1: a = 42, b = 35 IN main - after call to fcn1 Engr 0012 (04-1) LecNotes 21-03
4
functions & parameter/argument lists
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" } // end main //************************************** void fcn2( int *pb, int *pa ) { // begin fcn2 (*pb) = (*pb)*(*pa); (*pa) = (*pa)+(*pb); printf( "\n\nIn fcn2," " a = %d, b = %d ", *pa, *pb ); } // end fcn2 listed singly with data type actual parameters actual parameters are found in calling statment listed singly w/o data type formal & actual parameters are address or pointer parameters addresses transferred from calling statement to function for use original values can be altered by function Engr 0012 (04-1) LecNotes 21-04
5
functions & parameter/argument lists
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," } // end main //************************************** void fcn2( int *pb, int *pa ) { // begin fcn2 (*pb) = (*pb)*(*pa); (*pa) = (*pa)+(*pb); printf( "\n\nIn fcn2," " a = %d, b = %d ", *pa, *pb ); } // end fcn2 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
6
functions & parameter/argument lists
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", } // end main void fcn3( int b, int a, int *pq, int *pr ) { // begin fcn3 int h = 2; *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 & actual parameters are both value and address or pointer parameters formal parameters actual parameters both values and addresses transferred 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
7
functions & parameter/argument lists
memory map/trace 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", } // end main void fcn3( int b, int a, int *pq, int *pr ) { // begin fcn3 int h = 2; *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 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.