1 / 10

Modular Programming II

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.

Download Presentation

Modular Programming II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. Example of functions with one output value #include <stdio.h> 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. • It creates two new memory locations for x and y • It pass the values of a and b to x and y respectively • Compute the max • Pass the max value to the main program to assign it to x1 • Release thememory for x and y

  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. Program That Calls a Function with Multiple Output Arguments

  5. Program That Calls a Function with Output Arguments (cont’d)

  6. Diagram of Function separate with Multiple Results Parameter Correspondence for separate(value, &sn, &whl, &fr);

  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., signpis 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. 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. Example of Pointers #include <stdio.h> 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 100,10

  10. p1 a &a 5 p p2 b &b 9 p1 a &b 5 p p2 b &a 9 Example • Input two integers a and b, compare and display them in decreasing order #include <stdio.h> 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

More Related