Presentation is loading. Please wait.

Presentation is loading. Please wait.

31.1.2001Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture 10 31.1.2002.

Similar presentations


Presentation on theme: "31.1.2001Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture 10 31.1.2002."— Presentation transcript:

1 31.1.2001Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture 10 31.1.2002.

2 31.1.2001Sudeshna Sarkar, IIT Kharagpur 2 Function parameters It is very often useful if a function can operate on different data values each time it is called. Such values are function (input) parameters. The function specifies its inputs as parameters in the function declaration. /* Find area of a circle with radius r */ double area (double r) { return (3.14159*r*r) ; } parameter

3 31.1.2001Sudeshna Sarkar, IIT Kharagpur 3 Arguments The function call must include a matching argument for each parameter. When the function is executed, the value of the argument is substituted for the parameter. int main (void) {... double circum;... area1 = area(circum/2.0);... } double area (double r) { return (3.14*r*r); } parameter passing

4 31.1.2001Sudeshna Sarkar, IIT Kharagpur 4 Control and data flow When a function is called: (1) control transfers to the function body (2) argument values are copied (3) the function executes (4) control and return value return to the point of call.

5 31.1.2001Sudeshna Sarkar, IIT Kharagpur 5 Control and data flow int main (void) { double x, y, z; y=6.0; x = area(y/2.0);... z = area(7.88);... return (0); } double area (double r) { return (3.14*r*r); } 3.0 13.26 7.88 194.976

6 31.1.2001Sudeshna Sarkar, IIT Kharagpur 6 Style notes Put comments above a function body to specify completely what the function does, including the significance of all the parameters. /* Find area of a circle with radius r */ double area (double r) { return (3.14159*r*r) ; }

7 31.1.2001Sudeshna Sarkar, IIT Kharagpur 7 Multiple parameters a function may have more than one parameter arguments must match parameters in number, order and type. void main () { double x, y; int z; char op;... z = operate (x, y, op);... } arguments int operate (double x, double y, char op) { switch (op) { case ‘+’ : return x+y+0.5 ; case ‘~’ : if (x>y) return x-y + 0.5; return y-x+0.5; case ‘x’ : return x*y + 0.5; default : return –1; } parameters

8 31.1.2001Sudeshna Sarkar, IIT Kharagpur 8 Rules for using functions Arguments must match parameters: in number in order in type A function can only return one value. but it might contain more than one return statement. In a function with return type T, the return expression must be of type T. A function with return type T can be used anywhere an expression of type T can be used.

9 31.1.2001Sudeshna Sarkar, IIT Kharagpur 9 Local variables A function can define its own local variables. The locals have meaning only within the function. Each execution of the function uses a new set of locals Local variables cease to exist when the function returns Parameters are also local.

10 31.1.2001Sudeshna Sarkar, IIT Kharagpur 10 Local variables /* Find the area of a circle with diameter d */ double circle_area (double d) { double radius, area; radius = d/2.0; area = 3.14*radius*radius; return (area); } parameter local variables

11 31.1.2001Sudeshna Sarkar, IIT Kharagpur 11 Defining function prototypes Functions should be declared before they are used (invoked). Instead of writing the complete function, you can use function prototypes to declare a function so that it can be used. It is a good programming style to use function prototypes.

12 31.1.2001Sudeshna Sarkar, IIT Kharagpur 12 Function prototypes int operate (double x, double y, char op) ; int operate (double, double, char); void print_average (double, int); int get_intput (void); void print_banner (void); Write a function prototype near the top of the program Can use the function anywhere thereafter.

13 31.1.2001Sudeshna Sarkar, IIT Kharagpur 13 #define TRUE 1 #define FALSE 0 /* Find if x is a factor of y */ int isFactor (int x, int y) { if (y%x == 0) return TRUE; return FALSE; } /* Rteurns true if x is a prime number */ int isPrime (int x) { int factor; for (factor=2; factor <= sqrRt(x); factor++) if (isFactor(factor, x)) return FALSE; } /* Finds all prime numbers in acertain range */ void main (void) { int i, start, limit; scanf (“%d%d”, &start,&limit); for (i=start; i<=limit; i++) { if (isPrime (i)) printf (“%d “, i); } Example :

14 31.1.2001Sudeshna Sarkar, IIT Kharagpur 14 Local variables of main : i, start, limit Parameters of isPrime : x Local variables of isPrime : factor Parameters of isFactor : x, y

15 31.1.2001Sudeshna Sarkar, IIT Kharagpur 15 Local Variables Formal parameters and variables declared in a function are local to it. cannot be accessed or used by other functions directly Allocated (created) on function entry. De-allocated (destroyed) on function return. Formal parameters initialized by copying value of actual parameter. (“Call by value”)

16 31.1.2001Sudeshna Sarkar, IIT Kharagpur 16 Call by value void printDouble (int x) { printf (“Double of %d “, x); x *= 2; printf (“is %d\n”, x) ; } void main () { int num=15; printDouble (num); printf (“ num = %d\n”, num); } Note : The parameter of a function can be a constant, variable, expression – anything that has a value. Only the value is passed to the function.


Download ppt "31.1.2001Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture 10 31.1.2002."

Similar presentations


Ads by Google