Types of function call

ArijitDhali 897 views 14 slides Jul 03, 2021
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

The ppt describes usage of functions in c language. Showing basic use of function and determining the differences between function call by value and function call by reference using pointer. It also includes valid use in swapping two numbers in c along with different outputs. Overall its a basic not...


Slide Content

Function Call by Value & Reference By: Arijit Dhali

Contents 2

User Defined Function A user-defined function  is a  function provided by the user of a program or environment, in a context where the usual assumption is that functions are built into the program or environment.  Syntax: return_type function_name (parameter list) { Function Body return statement; } 3

Common Method of Swapping Two Integers 4 #include<stdio.h> main() { int x, y, z; printf (“\n Enter the value of x: "); scanf ("%d", &x); printf (“\n Enter the value of y: "); scanf ("%d", &y); z = x; x = y; y = z; printf ( "\ n After swap : x = %d, y = % d \n", x, y); return 0; } Using Third Variable Source Code: Storing Value Storing Value Swap using 3 rd Variable Declaring Variable

5 Output: Values Get Swapped

6 Call by Value The call by value method of passing argument to a function, copies the actual value of an argument into the formal parameter of the function. In this case, changed made to parameter inside the function had no effect on argument.

7 #include< stdio.h > void swapvalue(int x,int y); int main() { int a,b ; printf (“\n Enter two numbers: "); scanf("% d % d",&a,&b ); printf (“\n Before Swapping a=%d, b=% d \n", a,b ); swapvalue( a,b ); printf (“\n After Swapping a=%d, b=% d \n", a,b ); return 0; } void swapvalue(int x,int y) { int temp; temp=x; x=y; y=temp; return; } Source Code: Swap using 3 rd Variable Function Definition Function Declaration Declaring Variable Function Calling Passing Variable

8 Output: Values Doesn’t Swap

9 Call by Reference The Call by Reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to be parameter affect the passed argument. To pass the value by reference, argument pointers are passed to the functions just like any other value. So, accordingly you need to declare the function parameters as pointer types as in the following swap(), which exchanges the values of the two integer variables pointed to by its arguments.

10 #include<stdio.h> void swapreference(int * x,int *y); int main() { int a,b ; printf (“\n Enter two numbers: "); scanf("% d % d",&a,&b ); printf (“\n Before Swapping a=%d, b=% d", a,b ); swapreference(& a,&b ); printf (“\n After Swapping a=%d, b=%d", a,b ); return 0; } void swapreference(int *x, int *y) { int temp=0; temp=*x; *x=*y; *y=temp; return; } Source Code: Swapping address using 3 rd Variable Function Calling Passing Address Function Definition along with defining Pointer Function Declaration Declaring Variable

11 Output: Values Get Swapped

The function is called by directly passing value of variable as argument. We need to declare a general variable as function argument. Changes made in the formal parameters will not be reflected in the actual parameters. It is because formal and actual parameters are stored in different locations. Call by Value v/s Call by Reference The function is called by directly passing address of variable as argument. We need to declare pointer variable as function argument. Changes made in the formal parameters will be reflected in the actual parameters. It is because formal and actual parameters are stored in same locations. 12 Call by Value Call by Reference

13 Conclusion As the Conclusion , If we want to pass only the value of variable use ‘call by value’ approach, and if you want to see the change in the original value of the variable then use ‘call by reference’ approach.

GRACIAS 14 Hope to see you again!!!