Parameter Passing Techniques
- To understand parameter passing techniques, we need to understand Actual parameters and Formal Parameters.
Actual Parameters:
The variables or values that are passed while calling a function are called actual parameters.
Formal Parameters:
These are the variables written/declared in function definition/prototype and receive their values when a call to that function is made.
There are two parameter passing techniques. The first technique is Call by value, and the second is Call by reference.
Call by value:
- The method of copying the value of an argument into the formal parameter is called Call by value.
- In this method, changes made to the parameter doesn’t affect the argument.
Program:
#include<stdio.h>
void f(int x, int y)
{
x += y;
printf("x=%d y=%d\n",x,y);
}
int main()
{
int a=2, b=3;
f(a,b);
printf("a=%d b=%d\n", a, b);
return 0;
}
Output:
x=5 y=3
a=2 b=3
Call by reference:
- The method of copying the address of an argument into the parameter is called Call by reference.
- This is the second way of passing arguments to a subroutine.
- Inside the subroutine, to access the actual argument used in the Call, an address is used.
- Thus, the changes made to the parameter affects the argument.
Program:
#include<stdio.h>
void swap(int *x, int *y);
int main(void)
{
int i,j;
i=10;
j=20;
printf("i and j before swapping: %d %d\n",i,j);
swap(&i,&j); /* pass the address of i and j*/
printf("i and j after swapping %d %d\n",i,j);
return 0;
}
void swap(int *x, int *y)
{
int temp;
temp = *x;//save the value at address x
*x = *y;// put y into x
*y = temp;// put x into y
}
Output:
i and j before swapping: 10 20
i and j after swapping 20 10
Who is the course for?
- This course is designed for the students, employees, software programmers who want to learn C programming from scratch to end.
- After successfully completing this course you will be able to understand how the programming works and use it in the real time environment.
Prerequisites before starting this course:
- Before entering into this course you should have the basic understanding of how computer technology works.
- If you don’t have any experience in C or any other programming language then this course is for you.
- If your intermediate level of knowledge in C programming and you want to enhance your C skills to advance level then this course is for you.
- All you need is just any of the one device mobile, laptop, or PC and internet connection.
Questions 🙵 Answers.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum
Video 48 Min + 2 Min read to complete
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum
Video 48 Min + 2 Min read to complete
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum
Video 48 Min + 2 Min read to complete
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum
Video 48 Min + 2 Min read to complete
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum
Video 48 Min + 2 Min read to complete