Presentation is loading. Please wait.

Presentation is loading. Please wait.

FUNCTION ||.

Similar presentations


Presentation on theme: "FUNCTION ||."— Presentation transcript:

1 FUNCTION ||

2 Functions with multiple results
We know that functions can only have one result (one return statement), but we can trick a function into giving multiple results by using pointer parameters. Let's have a function that takes in a double number and give back 3 “results”: a sign, a whole part and a fraction. - 3 0.1416

3 Functions with multiple results
The best thing to do next is to analyse our function by making a diagram of what goes in and of what comes out by labelling everything properly with their types. Notice the 3 arrows on the right side. Since a function can only have 1 "true" result, we will have to do something about the two "extra results". char sign double number int whole split double fraction

4 Functions with multiple results
Of course, our function can only have one true result (one return), so we'll pick one (any one will do). Let's pick the sign as the result. Since the sign is a character, our function will be a char function. The two other “results” (the whole part and the fraction will be made accessible to the main program by the pointer parameters. The diagram on the next slide shows the outcome.

5 Functions with multiple results
To get rid of the extra "results", we will pick one as the true result and the other two will be fake results. We then transfer the "fake" results to the left side but as pointers (we add a star to their names). Now our function has only one result, hence perfectly legal. double number int *whole split char sign double *fraction

6 Program--2 /* Problem: This program just demonstrates how to assign values to pointer variables. It serves no other purpose. */ #include <stdio.h> int main (void) { /* c and d are pointers to integers */ int a, b, *c, *d, e; a = 10; b = a * 3; c = &a; /* address of a goes into c */ d = &b; /* address of b goes into d */ e = *c + *d; /* *c is a and *d is b */ *d = a; d = &a; *c = *d - a % b + *c; printf ("%d hi \n",*c); printf ("Do you understand why\n"); printf ("a= %d, b= %d, e= %d ?\n", a, b, e); return (0); }

7 Passing Variables using Pointers – Call by Reference
Returning a single value using pointer /* PROGRAM # 51 */ /* CALL BY REFERENCE*/ #include <stdio.h> /* Declare function SUM2 */ void SUM2(int num1, int num2, int *ptr); main( ){ int x, y, sum; int *psum; /* pointer to sum */ psum=∑ /* get the pointer to point to sum*/ printf(“Pls enter 2 int numbers: “); scanf(“%d%d”, &x,&y); /* NOTE: [1] According to the function prototype we need to provide 3 arguments where the 1st & the 2nd arguments are regular variable and the 3rd one should be a pointer. [2] The function is VOID, i.e., it does NOT return any value to the calling function. Therefore, when we call the function we don’t assign it to any variable */ /* call the function SUM2 */ SUM2(x, y, psum); printf("%d PLUS %d is: %d", x, y, sum); } /*Function SUM2, gets two integers and adds them */ void SUM2(int num1, int num2, int *ptr){ *ptr=num1+num2; }

8 Functions with multiple results
int main (void) { double n, f; int w; char s; printf (“Enter a double number:”); scanf (“%lf”, &n); s = split (n, &w, &f); printf (“The sign is: %c\n”, s); printf (“The whole part is: %d\n”, w); printf (“The fraction is: %f\n”, f); return (0); } See that when a parameter is a pointer, you must send an address value to it.

9 Program--6 /* This function takes a real number and separates it into three parts: a sign (a character value), a whole part (an integer) and a fraction (double) */

10 Program--6 /* This function takes a real number and separates it */
/* into three parts: a sign (a character value), a */ /* whole part (an integer) and a fraction (double) */ #include <stdio.h> #include <math.h> /* the function returns the sign but the pointer parameters */ /* will modifiy the corresponding variable arguments in */ /* main program */ char separate (double number, int *w, double *f) { char s; double magnitude; /* the absolute value of the number */ /* testing if positive or negative and assignation of sign */ if (number < 0) s = '-'; else if (number == 0) s = ' '; else s = '+'; magnitude = fabs (number); *w = floor (magnitude); *f = magnitude - *w; return (s); }

11 Program--6 int main (void) { double n; /* the original number */
/* the variables that will be filled by the function */ char sign; int whole; double fraction; printf ("Enter a real number: "); scanf ("%lf", &n); /* calling the function */ sign = separate (n, &whole, &fraction); /* printing the report */ printf ("Parts of %f: \n", n); printf ("===============\n\n"); printf ("Sign is: %c \n", sign); printf ("Whole part is %d \n", whole); printf ("Fraction part is %f \n", fraction); return (0); }

12 /* PROGRAM # 56 */ /* DECOMPOSE FLOAT NUMBER INTO INT & FRAC. PARTS. */ /* VAR is the variable that the user inputs */ /* VAR_FRACTION is the fraction part */ /* VAR_INTEGER is the integer part */ #include<stdio.h> void decompose(float number, int *int_part, float *frac_part); main( ){ float VAR, VAR_FRACTION; int VAR_INTEGER; printf("Please enter a float number: "); scanf("%f", &VAR); /* Call the function decompose*/ decompose(VAR, &VAR_INTEGER, &VAR_FRACTION); printf("The float number is %f.\n", VAR); printf("It's integer part is %d.\n", VAR_INTEGER); printf("And it's fraction part is %f.\n", VAR_FRACTION); } /*Function decompose, takes a float number, decompose the number into integer part and fraction part using pointers */ void decompose(float number, int *int_part, float *frac_part){ *int_part=(int) number; *frac_part=number - *int_part; RUN: Please enter a float number: The float number is It's integer part is 123. And it's fraction part is

13 Program--7 /* Problem: This is simple addition reader/parser for 2 doubles. */ #include <stdio.h> /* function to read expression */ void readex (double *op1, double *op2) { char plus; scanf ("%lf%c%lf", op1, &plus, op2); } int main(void) double a, b, c; printf ("Enter an addition expression (eg: ): "); readex (&a, &b); c=a+b; printf("%f + %f is %f.\n",a,b,c); return(0); /* can you expand this program to cover substractions? */

14 Pointer Application – SWAP
/*SWAPPING 2 VARS USING POINTERS*/

15 /* PROGRAM # 52 */ /*SWAPPING 2 VARS USING POINTERS*/
#include <stdio.h> /* Declare functions SWAP and prt */ void SWAP(int *x, int *y); void prt(int A, int B); main( ){ int num1=5, num2=9; /* Declare pointers ptr1 and ptr2 */ int *ptr1, *ptr2; /* Initialize ptr1 to point to num1 and ptr2 to point to num2 */ ptr1=&num1; ptr2=&num2; /* Call functions ptr and SWAP */ prt(num1, num2); SWAP(ptr1, ptr2); } /* Function SWAP, swaps two integer numbers using pointers */ void SWAP(int *x, int *y){ int temp; temp=*x; *x=*y; *y=temp; } /*Function ptr, gets two integers, prints the value */ void prt(int A, int B){ printf(“The value of the 1st variable is %d AND the value of the 2nd variable is %d.\n”, A, B); }

16 fabs & abs fabs Syntax: #include <math.h>
double fabs( double arg ); The function fabs() returns the absolute value of arg. abs Syntax: #include <stdlib.h> int abs( int num ); The abs() function returns the absolute value of num.

17 Functions with multiple results
char split (double number, int *whole, double *fraction) { char sign; if (number < 0) sign = '-'; else if (number > 0) sign = '+'; sign = ' '; *whole = abs ((int)number); *fraction = fabs (number) - *whole; return (sign); }

18 Practice on - Cal1-Cal2 program given at the class on Friday


Download ppt "FUNCTION ||."

Similar presentations


Ads by Google