Hey guys, what is up. In this particular post, we will be talking about what are parameters and arguments? Also, we will be talking about actual parameters and formal parameters.

Updated on 1/10/2021

Parameters

The parameter is the variable that is defined at the time of function declaration or definition. These variables are utilized to get the arguments that are passed at the time of the function call.

There are two types of parameters –

Actual parameters

The parameters that are passed while calling or invoking the function/method are called actual parameters. There is no such need to indicate data type in actual parameters.

FOR EXAMPLE – sum (x=10, y=20);     // calling

Formal parameters

The parameters which are passed to the function at the time of function definition/ declaration are called formal parameters. The data type of the accepting values should be defined. The extent of formal arguments is local to the function definition where they are utilized.

FOR EXAMPLE – sum (int a, int b);     // definition

                              {

                              }

PROGRAM

// C code to explain Parameters

#include <stdio.h>

// a and b are the PARAMETERS

int demo(int a, int b)

{

    // returning the multiplication

    return a * b;

}

int main()

{

    int num1 = 5, num2 = 8, res;

    // demo() is called with

    // num1 & num2 as ARGUMENTS.

    res = demo(num1, num2);

    // Displaying the result

    printf(“The multiplication is %d”, res);

    return 0;

}

OUTPUT

actual parameters and formal parameters-1 output

Arguments

An argument contains the values that are passed inside a function when the function is called. These values are the source of the function that needs the arguments at the time of execution. There can be an infinite number of arguments. These values are assigned to the variables at the time of function definition when it is called. 

There are two types of arguments –

Actual arguments

The arguments which we pass in a function call are called actual arguments. These variables are defined in the calling function.

FOR EXAMPLE – sum (10, 23);    // calling

Formal arguments

Formal arguments are the arguments used at a time function declaration. The scope of formal arguments is only to the function definition. In which they are used. After the function returns the value these arguments get deleted. Formal arguments are copies of actual arguments. Changes in formal arguments are not returned back in the actual arguments.

FOR EXAMPLE – int sum (int n)

                              {

                              }

PROGRAM

// C code to explain Arguments

#include <stdio.h>

// sum: Function definition

int sum(int a, int b)

{

// returning the addition

return a + b;

}

int main()

{

int num1 = 54, num2 = 39, res;

// sum() is called with

// num1 & num2 as ARGUMENTS.

res = sum(num1, num2);

// Displaying the result

printf(“The sum is %d”, res);

return 0;

}

OUTPUT

actual parameters and formal parameters-2 output

CALL BY VALUE AND CALL BY REFERENCE

PROGRAM

Question:- Write a program to swap 2 numbers using functions.

#include <stdio.h>  

void swap(int , int);   

int main()  

{  

    int m = 10;  

    int n = 20;   

    printf(” m = %d, n = %d\n”,m,n);

    swap(m, n);  

printf(” After swap: m = %d, n = %d\n”,m,n);

}  

void swap (int a, int b)  

{  

    int temp;   

    temp = a;  

    a=b;  

    b=temp;  

    printf(“After swapping values: a = %d, b = %d\n”,a,b); // Formal parameters, a = 20, b = 10   

}  

OUTPUT

actual parameters and formal parameters-4 output

PROGRAM

Question:- Write a program to swap values of 2 variables using call by reference method

#include <stdio.h>  

void swapping(int *, int *);   

int main()  

{  

    int p= 54;  

    int q = 89;   

    printf(“p = %d, q = %d “,p,q); 

    swapping(&p, &q);  

   printf(” After swap: p = %d, q = %d “,p,q); 

}  

void swapping (int *a, int *b)  

{  

    int temp;   

    temp = *a;  

    *a=*b;  

    *b=temp;  

    printf(“After swapping values: a = %d, b = %d\n”,*a,*b); // Formal parameters, a = 89, b = 54   

}  

OUTPUT

output

In the upcoming post, you will be getting information related to Samsung Galaxy A21 Simple and what is recursion in c. If you haven’t seen our previous post on What is a function in c programming then go and see our previous posts.


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *