Back to: C Tutorials For Beginners and Professionals
Actual and Formal Arguments in C
In this article, I will discuss Actual and Formal Arguments in C Language with Examples. Please read our previous articles, which discuss the types of user-defined functions in C language with examples.
What are Actual and Formal Arguments in C?
In the context of C programming, the terms “Actual Arguments” and “Formal Arguments” refer to the parameters used in function calls and function definitions, respectively.
Actual Arguments in C:
These are the arguments that are passed to the function when it is called. They represent the actual values or variables that you want the function to work with. These arguments are specified in the function call and are used to initialize the formal arguments of the function. For example, in the function call add(5, 3);, 5 and 3 are actual arguments.
Key Points:
- These are the arguments that are passed to a function when it is called.
- They are the actual values or expressions provided in the function call.
- For example, in the function call add(5, 10);, 5 and 10 are the actual arguments.
- They are also known as “actual parameters” or “arguments”.
Formal Arguments in C:
These are the parameters that are specified in the function definition. They act as placeholders for the values that will be passed to the function when it is called. In other words, they are the names used in the function definition to represent the values that the actual arguments will provide. For example, in the function definition int add(int a, int b) { return a + b; }, a and b are formal arguments.
Key Points:
- These are the parameters received by the function.
- They are used in the definition of the function to represent the values that will be used in the function.
- For instance, in the function definition, int add(int a, int b), a and b are formal arguments.
- They act as placeholders for the values passed to the function.
- They are also known as “formal parameters” or “parameters”.
Key Differences:
- Declaration and Definition: Formal arguments are part of the function’s definition and declaration, specifying the expected type of arguments. Actual arguments are part of the function call.
- Scope: The scope of formal arguments is within the function itself; they are not accessible outside the function. Actual arguments can be variables or expressions from the calling context.
- Assignment: When a function is called, the values of actual arguments are assigned or passed to the corresponding formal arguments.
Example to Understand Actual and Formal Arguments in C:
#include <stdio.h> int sum(int a, int b) { // 'a' and 'b' are formal arguments return a + b; } int main() { int x = 5, y = 10; int result = sum(x, y); // 'x' and 'y' are actual arguments printf("Sum is: %d\n", result); return 0; }
In this example:
- x and y are actual arguments in the main function.
- a and b are formal arguments in the sum function.
Actual and Formal Argument Examples in C Language
Let’s go through examples to illustrate actual and formal arguments in C programming.
Example: Simple Function Call
Function Definition
// Function to add two numbers int add(int num1, int num2) { return num1 + num2; }
Formal Arguments: Here, num1 and num2 are formal arguments. They are placeholders in the function definition that will hold the values passed to the function when it’s called.
Function Call
int main() { int result = add(5, 10); printf("The sum is: %d\n", result); return 0; }
Actual Arguments: In this call, add(5, 10), 5 and 10 are actual arguments. They are the real values passed to the function add.
Example: Using Variables as Actual Arguments
Function Definition
// Function to calculate the product of two numbers int multiply(int a, int b) { return a * b; }
Formal Arguments: a and b in multiply(int a, int b).
Function Call
int main() { int x = 4, y = 3; int product = multiply(x, y); printf("The product is: %d\n", product); return 0; }
Actual Arguments: Here, x and y are the actual arguments. When multiply(x, y) is called, the x and y values are passed to the function.
Example: Passing Arrays to Functions
Function Definition
// Function to sum elements of an array int sumArray(int arr[], int size) { int total = 0; for(int i = 0; i < size; i++) { total += arr[i]; } return total; }
Formal Arguments: arr[] and size in sumArray(int arr[], int size).
Function Call
int main() { int myArray[] = {1, 2, 3, 4, 5}; int total = sumArray(myArray, 5); printf("The total sum is: %d\n", total); return 0; }
Actual Arguments: myArray and 5 in the call sumArray(myArray, 5).
In these examples, the formal arguments (num1, num2, a, b, arr[], size) in the function definitions are placeholders for values. When the functions are called, the actual arguments (5, 10, x, y, myArray, 5) provide the real values that the function operates on.
Types of Argument Passing in C:
- Pass by Value: The most common method in C where a copy of the actual argument’s value is passed. Changes to the formal argument do not affect the actual argument.
- Pass by Reference: Achieved using pointers in C. The memory address of the actual argument is passed, allowing the function to modify the original variable’s value.
Example: Pass by Value (Actual and Formal Arguments)
In this example, we’ll create a simple program where we define a function that adds two numbers. The numbers passed to the function are the actual arguments, and the parameters received by the function are the formal arguments.
#include <stdio.h> // Function prototype int addNumbers(int a, int b); // 'a' and 'b' are formal arguments int main() { int num1 = 10; // First actual argument int num2 = 20; // Second actual argument // Call the function with actual arguments int sum = addNumbers(num1, num2); printf("Sum: %d\n", sum); return 0; } // Function definition int addNumbers(int a, int b) { // 'a' and 'b' are formal arguments return a + b; }
In this code:
- num1 and num2 are actual arguments.
- a and b in the function addNumbers are formal arguments.
Example: Pass by Reference (Using Pointers)
This example demonstrates the concept of passing by reference using pointers. Here, we modify the actual arguments via formal arguments.
#include <stdio.h> // Function prototype void swap(int *x, int *y); // 'x' and 'y' are formal arguments (pointers) int main() { int a = 100; // First actual argument int b = 200; // Second actual argument printf("Before swap: a = %d, b = %d\n", a, b); // Call the function with addresses of actual arguments swap(&a, &b); printf("After swap: a = %d, b = %d\n", a, b); return 0; } // Function definition void swap(int *x, int *y) { // 'x' and 'y' are formal arguments (pointers) int temp; temp = *x; *x = *y; *y = temp; }
In this code:
- a and b in main are actual arguments.
- In the swap function, x and y are formal arguments (pointers), and they point to the actual arguments a and b.
In the next article, I will discuss Function Call by Value and Call by Reference in C Language with Examples. In this article, I explain actual and formal arguments in C language with examples. I hope you enjoy this article, Actual and Formal Arguments in C Language with Examples. I would like to have your feedback. Please post your feedback, questions, or comments about this article.