Presentation is loading. Please wait.

Presentation is loading. Please wait.

TDBA66, VT-03, Lecture - Ch6_21 Function calls A function call implies –Every expression in the argument list is evaluated –If necessary, the value of.

Similar presentations


Presentation on theme: "TDBA66, VT-03, Lecture - Ch6_21 Function calls A function call implies –Every expression in the argument list is evaluated –If necessary, the value of."— Presentation transcript:

1 TDBA66, VT-03, Lecture - Ch6_21 Function calls A function call implies –Every expression in the argument list is evaluated –If necessary, the value of the expression is converted to the same type as the formal parameter and that value is assigned to the corresponding formal parameter defined in the function header –All arguments are sent by value, ”call-by-value” –The function body is executed –If a return -statement is executed the flow of control is given back to the calling function –If a return -statement contains an expression, that value is evaluated, converted to the return-type and that value is returned to the calling function –If no return -statement is present the control is given back when the end of the called function is reached

2 TDBA66, VT-03, Lecture - Ch6_22 Call-by-value As far as we know a function can return only one value, the value returned by the return-statement How to return more than one value? The answer is: use pointers A pointer is a memory cell that contains an address to a variable of a certain data type Two concepts: Direct value of a pointer – the address Indirect value of a pointer – value of the variable ”pointed to” Ex. 1 int n, *npt; n=82; npt=&n; /* address of n */ printf(”\nValue of n (twice): %d %d\n”, n, *npt);

3 TDBA66, VT-03, Lecture - Ch6_23 Figure 6.17 Function separate /* * Separates a number into three parts: a sign (+, -, or blank), * a whole number magnitude, and a fractional part. */ void separate(double num, /* input-value to be split*/ char *signp, /* output-sign of num*/ int *wholep, /* output-magnitude of num */ double *fracp) /* output-fractional of num*/ { double magnitude; /* local - magnitude of num */ /* Determines sign of num */ if (num < 0) *signp = '-'; else if (num == 0) *signp = ' '; else *signp = '+'; /* Finds magnitude of num (its absolute value) and separates it into whole and fractional part */ magnitude = fabs(num); *wholep = floor(magnitude); *fracp = magnitude - *wholep; }

4 TDBA66, VT-03, Lecture - Ch6_24 Figure 6.18 Program That Calls a Function with Output Arguments /* * Demonstrates the use of a function with input and output parameters. */ #include void separate(double num, char *signp, int *wholep, double *fracp); int main(void) { double value; /* input - number to analyze*/ char sn; /* output - sign of value*/ int whl; /* output - magnitude of value*/ double fr; /* output - fractional part*/ /* Gets data*/ printf("Enter a value to analyze> "); scanf("%lf", &value); /* Separates data value into three parts*/ separate(value, &sn, &whl, &fr); /* Prints results*/ printf("Parts of %.4f\n sign: %c\n", value, sn); printf(" whole number magnitude: %d\n", whl); printf(" fractional part: %.4f\n", fr); return (0); }

5 TDBA66, VT-03, Lecture - Ch6_25 Scope of a name The basic rule is that a name is reachable only inside the block where it is declared They do not exit outside the block A simple rule, but programmers tend to use the same name/identifier for different variables located in different blocks Question: which variable is refered...

6 TDBA66, VT-03, Lecture - Ch6_26 Example { int a = 2; /* a in outer block */ printf(”%d\n”, a); { int a = 5; /* a in inner block */ printf(”%d\n”, a); } printf(”%d\n”, ++a); /* back to the outer block */ } What is written?

7 TDBA66, VT-03, Lecture - Ch6_27 Parallel and nested blocks { int a, b; { /* inner block 1 */ float b;... /* int a is known, but not int b */ } { /* inner block 2 */ float a;... /* int b is known but not int a.*/ /* nothing in inner block 1 is known */ }

8 TDBA66, VT-03, Lecture - Ch6_28 Figure 6.20 Outline of Program for Studying Scope of Names #define MAX 950 #define LIMIT 200 void one(int anarg, double second); /* prototype 1 */ int fun_two(int one, char anarg); /* prototype 2 */ int main(void) { int localvar;... } /* end main */ void one(int anarg, double second) /* header 1 */ { int onelocal; /* local 1 */... } /* end one */ int fun_two(int one, char anarg) /* header 2 */ { int localvar; /* local 2 */... } /* end fun_two */

9 TDBA66, VT-03, Lecture - Ch6_29 Figure 6.21 Function to Scan a Common Fraction /* * Get a valid fraction * A valid fraction is of this form: integer/positive * integer */ void scan_fraction(int *nump, /* output - numerator */ int *denomp) /* output - denominator */ { char slash; /* local - character between numerator & denominator */ int status; /* status code returned by scanf indicating number of valid values obtained */ int error; /* flag indicating whether or not an error has been detected in current input */ char discard; /* unprocessed character from input line */ do { /* No errors detected yet */ error = 0; /* Get a fraction from the user */ printf(" Enter a common fraction as two integers separated by "); printf ("a slash\nand press \n> "); status = scanf("%d%c%d", nump, &slash, denomp);

10 TDBA66, VT-03, Lecture - Ch6_210 /* Validate the fraction */ if (status < 3) { error = 1; printf(" Input invalid--please read directions carefully\n "); } else if (slash != '/') { error = 1; printf(" Input invalid--separate numerator and denominator"); printf(" by a slash (/)\n"); } else if (*denomp <= 0) { error = 1; printf(" Input invalid--denominator must be positive\n"); } /* Discard carriage return and extra input characters */ do { scanf("%c", &discard);} while (discard != '\n'); } while (error); } Fig. 6.21 cont.

11 TDBA66, VT-03, Lecture - Ch6_211 Assertions The header-file assert.h contains the macro assert(). It is good programming style to use assert() in order to check input parameters in the beginning of a function #include int div_a_by_b(int a, int b) { assert(b != 0); return a/b; } int main() { div_a_by_b(2, 0); /* assertion will fail! */ }

12 TDBA66, VT-03, Lecture - Ch6_212 Recursion A function is recursive if it calls itself, directly or indirectly All functions can be called recursively In its simplest form recursion is simple to understand #include int main(void) { printf(” The universe is never ending! ”); main(); return 0; /* will never execute! */ }

13 TDBA66, VT-03, Lecture - Ch6_213 Example A function that sums the first n integers int sum(int n) { assert (n > 0); if (n <= 1) return n; else return (n + sum(n-1)); } Call: sum(1)1 Call: sum(2)2 + sum(1) Call: sum(3)3 + sum(2)  3 + 2 + sum(1) Call: sum(4)4 + sum(3)  4+3+sum(2)  4+3+2+sum(1)

14 TDBA66, VT-03, Lecture - Ch6_214 factorial.c Mathematically the definition of factorial is as follows 0! = 1, n! = n(n-1)... 3*2*1 for n > 0 or equivalently 0! = 1 n! = n((n-1)!) for n > 0 A recursive factorial-function int factorial(int n) { assert(n >= 0); if (n <= 1) return 1; else return (n* factorial(n-1)); }

15 TDBA66, VT-03, Lecture - Ch6_215 Iterative version An iterative factorial-function int factorial(int n) { assert(n >= 0); int product = 1; for (; n > 1; --n) product *= n; return product; }

16 TDBA66, VT-03, Lecture - Ch6_216 wrt_backwards.c #include void wrt_it(void); int main(void) { printf(”Input a line: ”); wrt_it(); printf(”\n\n”); return 0; } void wrt_it(void) { int c; if ((c = getchar()) != ’\n’) wrt_it(); putchar(c); }

17 TDBA66, VT-03, Lecture - Ch6_217 Recursion vs. iteration recursion might be ineffective due to many function calls recursion has some pros –if the algorithm is inherently recursive it is simple to implement in a recursive way –easier to understand!? And also some cons - for a beginner programmer recursion might seem to be difficult

18 TDBA66, VT-03, Lecture - Ch6_218 Stubs and drivers A stub is used in place of a function that is not ready yet - in order to test main where function calls are done - it returns faked values for the outputs A driver is used to test a certain function before this function is included in the current project as a ”fool-prof function” - a driver is a main that calls the function with all possible values as input

19 TDBA66, VT-03, Lecture - Ch6_219 Figure 6.33 Stub for Function multiply_fractions /* ***** STUB ***** * Multiplies fractions represented by pairs of integers. * Product of n1/d1 and n2/d2 is stored in variables pointed * to by n_ansp and d_ansp. Result is not reduced. */ void multiply_fractions(int n1, int d1, /* input – 1:st fraction */ int n2, int d2, /* input – 2:nd fraction */ int *n_ansp, /* output -*/ int *d_ansp) /* product of two fractions */ { /* Displays trace message*/ printf("\nEntering multiply fractions with\n"); printf("n1 = %d, d1 = %d, n2 = %d, d2 = %d\n", n1, d1, n2, d2); /* Defines output arguments*/ *n_ansp = 1; *d_ansp = 1; }

20 TDBA66, VT-03, Lecture - Ch6_220 Figure 6.34 Driver for Function scan_fraction /* Driver for scan_fraction */ int main(void) { int num, denom; printf("To quit, enter a fraction with a zero numerator\n"); for (scan_fraction(&num, &denom); num != 0; scan_fraction(&num, &denom)) printf("Fraction is %d/%d\n", num, denom); return (0); }


Download ppt "TDBA66, VT-03, Lecture - Ch6_21 Function calls A function call implies –Every expression in the argument list is evaluated –If necessary, the value of."

Similar presentations


Ads by Google