×

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of arguments passed in the function.

In the function to perform the task, the necessary data is sent as parameters.

 The parameters are optional i.e., we can have the function without parameters.

Arguments are some actual values during the function call they are passed. There exist a difference between argument and parameters. Arguments are used at the time of calling the function whereas at the time of function declaration and definition parameters are used.

Parameters are of two types they are:

  • Actual parameter
  • Formal parameter

In a program, the parameters can be actual parameters and formal parameters. Below is the combined example program of argument and parameter.

Program:

#include<stdio.h>
int add(int x,int y); //function declaration
void main(){
int x=10;
int y=20;
int sum = add(x,y);//passing arguments
printf("Sum  %d" ,sum);
}
int add(int a, int b){//parameters
int c = a+b;
return c;}

Output:                                                

Sum 30

Actual parameter:

  • The simple definition of the actual parameter is parameters that are written in the function call.
  • While calling or invoking the function or method the parameters that are passed are called actual parameters.
  • Arguments are known as the actual parameters.
  • Actual parameters contain the actual value. The change in the value of formal parameters doesn’t reflect on the actual parameters but it is mostly based on the method that we use to pass the parameters.
  • Numbers, expressions, or even function calls are used as actual parameters.
  • In the actual parameters no need to mention the datatypes. Any number of variables can be passed which are separated by a comma.
  • We can pass direct values, expressions, and variables in the actual parameters.

Syntax:

<Function_name>(actual parameters);

Example:            

sum(a,b);//function calling

Formal parameter:

  • The simple definition of the Formal parameter is Parameter Written in Function Definition is Called “Formal Parameter.
  • At the time of function definition or declaration the parameters which are passed to the function are called Formal parameters.
  • Parameters are also known as Formal parameters.
  • The change in the actual parameters can be reflectedin the Formal parameter this is based on the method that we use to pass the parameters.
  • In the Formal parameters we have to mention the datatypes.we can pass any number of variables that are separated by a comma.
  • Formal parameters act like a local variable. They are defined within the function or method and exit after the function or method exits. These parameters cannot be accessed out of the function.

Syntax:

<return type><Function_name>(actual parameters);

Example:           

Int sum(int x,int y); //function declaration

  • Below is the combined example program of actual and Formal parameters.

Program:

#include<stdio.h>
int add(int x,int y); //function declaration
void main(){
int x=5;
int y=10;
int result= mul(x,y);//Actualarguments
printf("Result  %d" ,result);
}
//function defination
int mul(int a, int b)// Formal parameters
{
int c = a*b;
return c;
}

Output:                                                

Result 50

From the above example, x and y are actual parameters and they pass the actual values i.e., 5 and 10 respectively. They are in the main method and passed as parameters in the calling function. Here the formal parameters area and b which are passed as parameters in the function definition. Into the formal parameters, the value of actual parameters is copied and a specified function is performed and then returned the value to the calling function.

  • As we know that the actual and Formal parameters are different but there exist some similarities between them they are mentioned below:
  • Both parameters are related to the function.
  • Both are written within the parenthesis.
  • We can pass any number of parameters by separating them with a comma.

We have two methods for passing the data into the function they are:

  1. Call by value.
  2. Call by reference.

Depending upon the type of the value passed to them as parameters these two methods are differentiated.

Call by value:

  • Into the formal parameters in this method the value of actual parameters is copied.
  • If any changes are made in the formal parameters do not reflect on the actual parameters because in this method both parameters share different memory addresses.

Example:           

sum(a,b);

Program:

#include <stdio.h>
int add(int a){// Formal parameters
    a = a+10;
    return a;
}
int main(){
   int x=5;
  add(x);// Actual parameters
   printf("The x value is: %d\n", x);
   return 0;
}

Output:

The x value is: 5

From the above example, the value of actual parameters x=5has been copied into the formal parameters a. After the function as exit, the value of the actual parameter is the same i.e. x=5.

Call by reference:

  • By modifying the formal parameters the value of actual parameters can be changed in this method.
  • If any changes are made in the formal parameters that reflect on the actual parameters because in this method both parameters share the same memory address.

Example:           

sum(&a,&b);

Program:

#include <stdio.h>
void add(int  *a){// Formal parameters
    *a = *a+10;
}
int main(){
     int x=5;
     add(&x);// Actual parameters
     printf("Value of x is: %d",x );
   return 0;
}

Output:

Value of x is: 15

From the above example, the value of actual parameters x=5 has been copied into the formal parameters a. After the function as exit the value of the actual parameter is modified i.e. x=15.

Value of x is: 15


Related Topics

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

Math functions in C

Math functions in C: In the C programming language, the compiler allows the developer to perform mathematical operations through the header’s functions, represented by <math.h>. The <math.h> header defines various mathematical...

3 minutes read.

Do-While Loop in C

Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes in the same manner. But, there is...

3 minutes read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.

File Handling in C

File Handling is used to open, read, write, search and close file.  C files I/O functions handles data on secondary storage device. File handling function: There are varioushandling functions in C. Function Operation fopen() It is...

4 minutes read.

C –Structure

C structure is a collection of different types of data that is grouped together. It is used to represent records.Structkeyword is used to create a structure. Each element of a...

1 minute read.

How to open a C file on android mobile

A C file is a file that has a .c extension and contains code written in the C language. C is a computer programming language developed by Dennis Ritchie at...

4 minutes read.

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.