Download presentation
Presentation is loading. Please wait.
Published byMargery Baldwin Modified over 9 years ago
1
CP104 Introduction to Programming Modular Programming Lecture 16__ 1 Modular Programming II Functions with single output Functions with multiple outputs Concepts of pointers Multiple calls to a function with in/output parameters Scope of variables Formal output parameters as actual arguments Programming with multiple functions
2
CP104 Introduction to Programming Modular Programming Lecture 16__ 2 Example of functions with one output value #include int max(int,int); int min(int,int); Int main(void) { int a, b, c, x1, x2, x3; scanf("%d%d%d", &a, &b, &c); x1 = max(a, b); x2 = min(a, b); if (c >= x1) { x3 = x2; x2 = x1; x1 = c; } else if (c >= x2) { x3 = x2; x2 = c; } else x3 = c; printf("%d, %d, %d", x3,x2,x1); } int max(int x, int y) { int z; if (x > y) z = x; else z = y; return (z); } int min(int x, int y) { int z; if (x < y) z = x; else z = y; return (z); } When max(a,b) is called during the execution of the main program. 1.It creates two new memory locations for x and y 2.It pass the values of a and b to x and y respectively 3.Compute the max 4.Pass the max value to the main program to assign it to x1 5.Release the memory for x and y
3
CP104 Introduction to Programming Modular Programming Lecture 16__ 3 Functions with multiple outputs Problem: design a function to output the sign, whole number magnitude, and the fractional part of a floating number Analysis: the function should have the floating number num as a parameter. It should output the sign, magnitude, and fraction part. Sign is “-” if num <0, sign is “ “ if num = 0; magnitude = the absolute value of num; fraction = num – floor of num;
4
CP104 Introduction to Programming Modular Programming Lecture 16__ 4 Program That Calls a Function with Multiple Output Arguments
5
CP104 Introduction to Programming Modular Programming Lecture 16__ 5 Program That Calls a Function with Output Arguments (cont’d)
6
CP104 Introduction to Programming Modular Programming Lecture 16__ 6 Diagram of Function separate with Multiple Results Parameter Correspondence for separate(value, &sn, &whl, &fr);
7
CP104 Introduction to Programming Modular Programming Lecture 16__ 7 Concepts of Pointers The pointer of a variable means the address of the variable. It is the address of the first memory cell of the variable. –If sn is a char variable, then the address of sn is &sn. –The number of bits of a pointer varies depending on machine. For 32 bits processor, it uses four memory cells. A variable can be defined to hold the pointer of a variable. Such kind of variable is called pointer variable. –Declaration char *signp tells the compiler that signp is a pointer variable, which will contain the address of a type char variable. i.e., signp is a pointer to a char variable. For char pointer signp, the character stored in the memory cell pointed by signp can be derived by *signp The unary operator * is called an indirection operator
8
CP104 Introduction to Programming Modular Programming Lecture 16__ 8 Concepts of Pointers (cond’d) Examples char sn = ‘a’; /* declare a character variable */ char *signp; /* declare a char pointer variable */ signp = &sn; /* assign the address of sn to signp */ printf(“%c”, *signp); /* equivalent to printf(“%c”, sn); */ –A pointer variable must point to a variable of the type it declared char sn = ‘a’; /* declare a character variable */ double *d; /* declare a pointer of double variable */ d = &sn; /* this is a wrong assignment! */
9
CP104 Introduction to Programming Modular Programming Lecture 16__ 9 Example of Pointers #include main)() { int a = 100, b = 10; int *pointer1, *pointer2; pointer1 = &a; pointer2 = &b; printf(“%d, %d\n”,a, b); printf(“%d, %d\n”,*pointer1, *pointer2); } The output will be 100,10
10
CP104 Introduction to Programming Modular Programming Lecture 16__ 10 Example Input two integers a and b, compare and display them in decreasing order #include main() { int *p1, *p2, *p, a, b; scanf("%d,%d", &a,&b); p1 = &a; p2 = &b; if (a < b) { p = p1; p1 = p2; p2 = p; } printf("\na=%d, b=%d\n", a, b); printf("max = %d, min = %d\n",*p1, *p2); } Input: 5, 9 Display: a = 5, b =9 max = 9, min = 5 &a &b 5 9 p1 p2 a b p &b &a 5 9 p1 p2 a b p
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.