C PROGRAMMING: PARAMETERS AND CALL TYPE

C PROGRAMMING: PARAMETERS AND CALL TYPE

parameters:

Parameters are of two types:

  1. Actual Parameter
  2. Formal Parameter

They are called “parameters” because they define the information that is passed to a function. A parameter cannot be both a formal and an actual parameter, but both formal parameters and actual parameters can be either value parameters or variable parameters.

actual parameter:

  • When a function is called, the value (expressions) that are passed in the cell are called the argument or actual parameter.
  • In a simple word, we can say that the Actual parameter is a parameter as they appear in function calls.

formal parameter:

  • Formal parameters are local variables that are assigned a value from the arguments when the function is called.
  • In a simple word, we can say that, Formal parameters are parameters as they appear in function declarations.

LET’S TRY TO MAKE IT MORE CLEAR TO YOU BY TAKING A VERY SIMPLE EXAMPLE

#include<stdio.h>
class Addition
{
  int sum(int p, int q)
{
    return p+q;
}
 void main()
{
  Addition ad=new Addition();
   int a=10, b=20;
int c=sum(a,b);
}
}

In the function main in the example given above,

  • Inside the void main (a,b) are our actual parameters when used to calls.
  • The corresponding variables (int p, int q) present in int sum inside the class addition are formal parameters because they appear in the function definition.
  • Formal parameters are always variables.
  • Actual parameter does not have to be variables, they may be any numbers, even functions or expression as actual parameters

call types in c programming:

In C programming language, we can call a function in two different ways, based on how we specify the arguments, these two ways are: Call by value and Call by reference. Let’s study each term separately.

1- call by value:-

  • When we call a function by value, it means that we are passing the value of the arguments which are copied into the formal parameter of the function.
  • This means that the original values remain unchanged and only the parameter inside the function changes.
  • It copied the value.

2- call by reference:-

  • The call by reference method of passing arguments to a C function copies the address of the arguments into the formal parameters.
  • The address of the actual arguments is copied and then assigned to the corresponding formal arguments.
  • It copied the address.
Now let's do coding, and makes both terms "CALL BY VALUE" and "CALL BY REFERENCE" more clear. 

program- call by value

The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. By default, C programming uses call by value to pass arguments. In general, it means the code within a function cannot alter the arguments used to call the function. 

#include <stdio.h>
 
/* function declaration */
void swap(int x, int y);
 
int main () {

   /* local variable definition */
   int a = 100;
   int b = 200;
 
   printf("Before swap, value of a : %d\n", a );
   printf("Before swap, value of b : %d\n", b );
 
   /* calling a function to swap the values */
   swap(a, b);
 
   printf("After swap, value of a : %d\n", a );
   printf("After swap, value of b : %d\n", b );
 
   return 0;
}

output:

Before swap, value of a :100 
Before swap, value of b :200 
After swap, value of a :100 
After swap, value of b :200

program- call by reference

The code explains how to pass address as arguments from calling function to called functions.

#include<stdio.h>
main()
{
    int a,b,c;
    clrscr();
    printf("Enter Two Values: ");
    scanf("%d %d",&a,&b);
    c=add(&a,&b);
    printf("The Sum is: %d",c);
    getch();
}
add(int *x,int *y)
{
    int z;
    z=*x+*y;
    return(z);
}
 

output:

Enter Two Values: 10 20
The Sum is: 30

difference between call bye value and call by reference:

1-CALL BY VALUE :-

  • A copy of the value is passed into the function
  • Actual and formal arguments are created at a different memory location
  • Changes made inside the function is limited to the function only. The values of the actual parameters do not change by changing the formal parameters.

2- CALL BY REFERENCE :-

  • An address of value is passed into the function
  • Actual and formal arguments are created at the same memory location
  • Changes made inside the function validate outside of the function also. The values of the actual parameters do change by changing the formal parameters.
I wish now each topic is clear to you. The more you do practice the more concepts you get cleared. So always keep practicing until you got a command on it.
In our next artcle we will discuss about "STRING IN C".
I will see you next time...
Also visit our previos articles listed below:

AND MANY MORE, SO VISIT OUR SITE…https://justhinter.com/category/programming/

Scroll to Top