In the realm of C programming, formal parameters are alternatively referred to as formal arguments. These formal parameters are essentially variables specified within the function definition, serving the purpose of receiving values passed by the caller function. Upon invoking a function, the actual parameters get duplicated into the formal parameters.
Syntax:
It has the following syntax:
return_type function_name(data_type parameter1, data_type parameter2, ...);
In this syntax,
- return_type: It represents the return type of the element.
- function_name: It represents the name of the function.
- data_type: It represents the type of data.
- parameter1, parameter 2: It represents the formal parameters.
Declaration and Definition of Formal Parameter
If there is a need to declare or define the formal parameter, the syntax to be used is as follows:
int add(int a, int b);
In this function definition, the provided a and b represent the formal parameters. These parameters serve the primary purpose of accepting the values of the real parameters passed during function invocations.
Formal Parameter Example
Let's consider an example to demonstrate the formal parameter in C programming.
Example
#include <stdio.h>
int calculatingArea(int length, int width)
{
// 'length' and 'width' are formal parameters
int area = length * width;
return area;
}
int main() //main function
{
int len=17;
int wid=13;
int ans=calculatingArea(len, wid);
printf("The Length is %d\n", len);
printf("The Width is %d\n", wid);
printf("Area of the Rectangle is: %d\n", ans);
return 0;
}
Output:
The Length is 17
The Width is 13
Area of the Rectangle is: 221
Explanation:
In this instance, we are showcasing the calculatingArea function which accepts two parameters, length and width, and computes the area of a rectangle by multiplying them. Within the main function, the values 17 and 13 are provided as arguments, resulting in the calculation of 221 being displayed along with the initial dimensions.
Features of Formal Parameters:
Several features of formal parameters in C are as follows:
- Formal parameters are variables that appear in the function definition and receive input values.
- When the function is invoked, they are given values based on the actual parameters.
- These parameters are only present during the function's execution and are saved in local memory.
- Formal parameters should be the same number, order, and data type as the actual parameters.
- Formal parameters inside the function act similarly to normal variables that are used in calculations or operations.
- They allow the function to handle several inputs, which makes the code more reusable and organized.
- Using formal parameters helps the function remain self-contained, which reduces dependence on global variables.
- They specify how the function will interact with data, which improves code readability and reduces errors.
Actual Parameters in C
In C programming, real parameters refer to the values provided to a function during its invocation. These values, also referred to as arguments, may consist of constants, variables, expressions, or even function invocations. Upon calling a function, the real parameters are assessed, and their values are duplicated into the respective formal parameters within the called function.
Syntax:
It has the following syntax:
function_name(val1, val2, ...);
In this particular syntax,
- function_name stands for the function's identifier.
- val1, val2 denote the concrete parameters being passed.
Declaration and Definition of Actual Parameter
In C programming, the arguments passed to a function are enclosed within parentheses and delimited by commas. For instance, take into account the subsequent function invocation:
int result = add(2, 3);
In this function invocation, the values 2 and 3 serve as the arguments, and they are transmitted to the add function which expects two formal parameters.
Actual Parameter Example
Let's consider a specific scenario to demonstrate the use of the actual parameter in the C programming language.
Example
#include <stdio.h>
int calculatingParameter(int length, int width)
{
return 2 * (length + width);
}
int main()
{
// Actual parameters.
int len=15;
int wid=7;
// Passing the actual parameters to the function.
int Peri_Meter = calculatingParameter(len, wid);
printf("The given length is = %d\n", len);
printf("The given width is = %d\n", wid);
printf("The Perimeter of a rectangle is = %d\n", Peri_Meter);
return 0;
}
Output:
The given length is = 15
The given width is = 7
The Perimeter of a rectangle is = 44
Explanation:
In this instance, we employ the function named calculatePerimeter to determine the boundary of a rectangle. It accepts formal parameters 'length' and 'width' and produces the result of 2 * (length + width). The function is supplied with the concrete values 15 and 7 from the main function, and the resultant perimeter of 44 is displayed in conjunction with the initial dimensions.
Features of Actual Parameters:
There are several features of Actual Parameters in C. Some of them are as follows:
- When we call a function, the actual parameters are the values that are supplied to it.
- They can be constants, variables, expressions, or the outcomes of other functions.
- They exist within the function call rather than the function definition.
- The actual parameters should be the same number and type as the formal ones.
- These values are copied into formal arguments and used by the function during execution.
- They represent the input data with which the function will operate.
- The actual parameters enable the same function to provide multiple outputs based on the inputs.
- They are required for effective communication between the caller and the function.
Types of Argument Passing in C
There are various methods of Passing Arguments in the C programming language. A few examples include:
1) Pass by value (Using Formal and Actual Arguments)
When using pass by value, a duplicate of the real parameter is transferred to the formal parameter when a function is invoked. Any alterations made to the formal parameters do not affect the actual parameters.
Pass by Value using Formal and Actual Parameter Illustration
Let's consider an example to demonstrate pass by value using formal and actual parameters in the C programming language:
Example
#include <stdio.h>
int addingNumbers(int a, int b)
{
//The Formal arguments
return a + b;
}
int main() //main function
{
int Num_1 = 10;
int Num_2 = 20;
int sum = addingNumbers(Num_1,Num_2);
//The Actual arguments
printf("The Sum of the two numbers is: %d\n", sum);
return 0;
}
Output:
The Sum of the two numbers is: 30
Explanation:
In this instance, we showcase the concept of pass by value in C programming. The addingNumbers function receives duplicates of the real parameters Num1 and Num2 through the formal parameters a and b. It then calculates their sum and returns the result. The initial values remain unaltered, and the returned value from the function is employed to display the outcome of 30.
2) Pass by Reference (using pointers)
In C programming, pass by reference is emulated through pointers, facilitating the transfer of the actual memory address to functions. This mechanism empowers functions to alter the initial values effectively.
Passing arguments by reference in C++ is achieved through the use of pointers for both formal and actual parameters. This allows a function to directly modify the value of a variable outside its scope. Below is an example demonstrating this concept:
return_type function_name(data_type parameter1, data_type parameter2, ...);
#include <iostream>
using namespace std;
// Function to increment a number by 1
void increment(int* num) {
(*num)++;
}
int main() {
int value = 5;
cout << "Before increment: " << value << endl;
// Passing the address of 'value' to the increment function
increment(&value);
cout << "After increment: " << value << endl;
return 0;
}
In this example, the increment function takes a pointer to an integer as its formal parameter. By passing the address of the variable value as the actual parameter, the function is able to modify the original value directly.
Let's consider a scenario to demonstrate Pass by Reference using pointers in the C programming language.
Example
#include <stdio.h>
// Function to swap two numbers using pointers
void swap(int *x, int *y)
{
// Formal arguments - pointers to original values
int temp = *x;
*x = *y;
*y = temp;
}
int main() //main function
{
int a = 14;
int b = 12;
printf("Before swapping the values are: a = %d, b = %d\n", a, b);
swap(&a, &b);
// Passing addresses - actual arguments
printf("After swapping the values are: a = %d, b = %d\n", a, b);
return 0;
}
Output:
Before swapping the values are: a = 14, b = 12
After swapping the values are: a = 12, b = 14
Explanation:
When working with C, passing parameters by pointer involves the function receiving a reference to the memory address of the original parameter. This approach enables the function to make direct changes to the value of the original parameter through dereferencing the pointer.
3) Passing parameters using a Pointer
When passing parameters by pointer in C, the function is provided with a pointer to the memory address of the original parameter. This enables the function to make changes to the value of the original parameter directly through dereferencing the pointer.
Passing parameters using a pointer Example
Let's consider an instance to demonstrate the process of passing arguments using a pointer in the C programming language.
Example
#include <stdio.h>
#include <stdlib.h>
// A function for allocating and initializing an array.
void allocate(int** q, int size)
{
*q = malloc(size * sizeof(int));
// Memory allocation for the pointer.
for (int i = 0; i < size; i++)
{
(*q)[i] = i;
//Giving values to the array elements.
}
}
int main() //main function
{
int* arr;
int size = 5;
// Passing the pointer's address allows memory allocation.
allocate(&arr, size);
printf("The allocated and initialized array is:\n");
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
// Free the allocated memory
free(arr);
return 0;
}
Output:
The allocated and initialized array is:
0 1 2 3 4
Explanation:
In this instance, memory is dynamically assigned for an array within a function by passing a pointer to another pointer (int**). Subsequently, the allocate function allocates memory and sets the values from 0 to 4.
Difference between Actual and Formal parameters in C
There exist multiple variances between real and formal parameters in C programming. A few distinctions are outlined below:
| Aspect | Parameters | Formal Parameters |
|---|---|---|
| Definition | The values are passed to a function during its call. | Variables are declared within the function declaration. |
| Purpose | It give the input data to the function. | It accepts and use the input data within the function. |
| Data types | It contains several data types, such as constants, variables, expressions, and function calls. | Variable declarations should be valid with specified data types. |
| Control | It is controlled by the caller (main function or other functions). | It is controlled by the function that is invoked. |
| Lifetime | It exists in the invoking function's scope. | It exists just during the function's execution (local scope). |
| Mutability | Actual parameter changes do not alter formal parameters (pass-by-value). | Unless pointers are used, formal parameters receive copies. |
| For example, | add(5, 10) yields 5 and 10 as actual parameters. | int add(int a, int b) yields a and b as formal arguments. |
| Passing type | It can be passed either by value or by reference. | Values can be received via copy (value) or address (reference). |
Conclusion
In summary, formal and actual parameters in C play a crucial role in controlling data exchange among functions, guaranteeing accurate value assignment, and promoting modular structure. Proficiency in their usage is essential for creating effective and sustainable C programs.
Formal and Actual Parameters FAQS
Formal parameters in C are the placeholders in a function definition that represent the data that will be passed into the function when it is called. On the other hand, actual parameters are the values or variables that are passed into the function when it is called, which will be used to fill the formal parameters during the function execution.
In the C programming language, the actual parameters refer to the values or variables passed to a function when it is called. Conversely, formal parameters are the variables declared in the function definition that receive these values.
Yes, it is permissible for the names of actual and formal parameters to match.
Yes, it is possible for them to share identical names, yet they are stored in separate memory areas. The function will make use of the formal parameter instead of the initial variable.
3) Do the parameters get evaluated prior to or following the function invocation?
Before invoking a function in C programming, the arguments passed are evaluated first. The outcome of this assessment is then passed on to the function for further processing.
4) Is it possible to pass expressions or function calls as arguments to functions?
Actual parameters can be fixed values, variables, mathematical formulas, or outcomes returned from different functions.
Do changes applied to formal parameters impact the actual parameters?
By default, modifications to formal parameters do not impact actual parameters. To modify actual variables, pointers (pass by reference) must be used.