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?

Prerequisites before starting this course:

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

More Courses

You might also be interested in these courses