Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Advanced Programming in C Passing Parameter to Function.

Similar presentations


Presentation on theme: "Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Advanced Programming in C Passing Parameter to Function."— Presentation transcript:

1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Advanced Programming in C Passing Parameter to Function

2 4-2 Introduction: Functions can be either library functions or user-defined functions. For example, printf() and scanf() are library functions, whereas message() is a user-defined function. Library function come alongwith the compiler and are present on the disk. The procedure of calling both types of function is exactly same. There can be any number of functions in a program and any function can call any other function any number of times. Program execution must begins with main() function. The order in which the functions get called and the order in which they are defined in a program need not necessarily be same.

3 4-3 Calling a function: Functions divide a program into smaller manageable chunks. It's usual to group related functions into libraries and then use a header file to make public those functions. That way, other parts of your application can call them. In this example we'll call the function pow() in. It calculates x y where both x and y are doubles and x is positive. #include int main() { double x=5.7, y=3; double answer= pow(x,y) ; printf("The result of %f to the power of %f is %f",x,y,answer) ; return 0; }

4 4-4 User defined function: main() { int a, b, c, sum; printf(“Enter any three numbers”); scanf(“%d %d %d”, &a, &b, &c); sum = calsum(a, b, c); printf(“Sum = %d “, sum); } calsum(int x, int y, int z) { int s; s= x + y + z; return (s); }

5 4-5 Return type and value: There is no restriction on the number of return statements that may be present in a function. The return statement need not always be present at the end of the called function. All function have return type. If we omit it, then by default int is assumed. So we should always specify return type. If the function does not return any thing, then void should be specified with function. Other than in a void function, a value of function’s return type must be returned. This is done with return statement. Return statement also exits the function.

6 4-6 Return type and value: main() { int a, b, c, p; a=b=c=5; p=prod(a,b,c,) printf(“p=%d”,p); } prod(int x, int y, int z) { int p; p=x*y*z; return (p); } main() { float a, b, c, p; a=b=c=1.5; p=prod(a,b,c,) printf(“p=%f”,p); } float prod(int x, int y, int z) { float p; p=x*y*z; return (p); } main() { float a, b, c; a=b=c=1.5; prod(a,b,c,) } void prod(int x, int y, int z) { float p; p=x*y*z; printf(“p=%f”,p); }

7 4-7 Function Parameters : Parameters are values passed into a function. Each parameter must have its type declared. If you are using floats instead make them doubles as C will convert them anyway and give you warnings. double getpercent(double a, double b ) { return a/b*100.0; } int main () { // Call the function double value = getpercent(10.6,90.2) ; printf("The percentage is %f", value) ; return 0; }

8 4-8 Call by value(pass-by-value): In call by value method, the called function creates a new set of variables and copies the values of arguments into them. #include void main() { int a=10; printf(“%d”,a);a=10 fun(a); printf(“%d”,a);a=10 } void fun(int x) { printf(“%d”,x)x=10 x++; printf(“%d”,x);x=11 }

9 4-9 Call by value (Explanation):

10 4-10 Example (Call by value): void swap(int x, int y) { int temp; temp = x; x = y; y = temp; printf("Swapped values are a = %d and b = %d", x, y); } void main() { int a = 7, b = 4; swap(a, b); printf("Original values are a = %d and b = %d", a, b); printf("The values after swap are a = %d and b = %d", a, b); }

11 4-11 Reference operator: The & operator int target; int &rTarg = target; rTarg is reference to integer variable (target). The reference is initialized to refer to target (alias). This is known as address operator or address – of – operator in C (pointer context).

12 4-12 Call by reference (pass – by – reference) : If data is passed by reference, a pointer to the data is copied instead of the actual variable as is done in a call by value. Because a pointer is copied, if the value at that pointers address is changed in the function, the value is also changed in main(). int a=1;void fun(int *x) fun(&a);{ defn; } We can also write like this way: int a=1;void fun(int &x) fun(a);{ defn; } Any modification done to variable a will effect outside the function also

13 4-13 Example (Call by reference): #include void main() { int a=10; printf(“%d”,a);a=10 fun(a); printf(“%d”,a);a=11 } void fun(int &x) { printf(“%d”,x)x=10 x++; printf(“%d”,x);x=11 }

14 4-14 Explanation:

15 4-15 Example: void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; printf("Swapped values are a = %d and b = %d", *x, *y); } void main() { int a = 7, b = 4; swap(&a, &b); printf("Original values are a = %d and b = %d",a,b); printf("The values after swap are a = %d and b = %d",a,b); }

16 4-16 Conclusion: Call by value => copying value of variable in another variable. So any change made in the copy will not affect the original location. Call by reference => Creating link for the parameter to the original location. Since the address is same, changes to the parameter will refer to original location and the value will be over written.

17 4-17 Test yourself: Consider the following code segment. void Mystery( int & a, int & b, int c ) { a = b + c; b = 0; c = 0; } void Print() { int x = 0, y = 1, z = 2; Mystery(x, y, z); printf(“%d%d%d”, x, y, z); Mystery(x, y, z); printf(“%d%d%d”, x, y, z); } What is output when function Print is called? A. 0 1 2 0 1 2 B. 3 0 0 3 0 0 C. 0 1 2 3 0 0 D. 3 0 2 2 0 2 E. 3 0 0 0 0 0

18 4-18 Test yourself: Consider the following program. void Sum(int a, int b, int & c) { a = b + c; b = a + c; c = a + b; } main() { int x=2, y=3; Sum(x, y, y); printf(“%d%d”, x, y); } What happens when this program is compiled and executed? A. There is a compile-time error because in the call Sum(x, y, y), variable y is passed both by value and by reference. B. There is a run-time error because in the call Sum(x, y, y), variable y is passed both by value and by reference. C. The program compiles and executes without error. The output is: 2 3 D. The program compiles and executes without error. The output is: 6 9 E. The program compiles and executes without error. The output is: 2 15


Download ppt "Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Advanced Programming in C Passing Parameter to Function."

Similar presentations


Ads by Google