Read through this article if you’d like to make your code more readable and take advantage of procedural abstraction by learning how to use function parameters.
Introduction
Last time, you learned about functions in C and how to declare and use them. This time, you’ll learn how to use function parameters. Modular programming made functions exist. As what was said in the last blog, writing code from top-to-bottom without using functions would be a disaster. Your code becomes neat and readable by employing a divide-and-conquer approach. However, the advantages of this coding method are short-lived if your data sets fly around unorganized. These data sets can further be re-used by other parts of your code, leading to more ambiguity especially if you don’t employ function parameters. Â
What are Function Parameters?
Function parameters are the elements inside the parenthesis beside a function call, declaration, or prototype. Each parameter can be passed to a function through a pass-by-value or a pass-by-reference approach. For example:
(Pass By Value Approach)
Function Prototype
int double_then_add(int num1, int num2);
You have num1 and num2 as parameters for functions double_then_add( ). These parameters are simply representations and declared as ints. They are also meant to be contained inside the function. Â
Function Declaration
int double_then_add(int num1, int num2)
{
num1 = num1 * 2;
num2 = num2 * 2;
return num1 + num2;
}
Here is the function declaration with parameters num1 and num2.Â
Function Call
result = double_then_add(num1, num2);
Here, the function call returns its value to the result variable. The parameters passed are the variables num1 and num2, but they could have different names too. Passing the arguments in this function is called pass by value. A copy of the variable is made during the function call, and this variable gets destroyed after exiting the function. In this way, variables num1 and num2 (that may be accessed outside the function) will not be altered.
(Pass By Referrence Approach)
Function Prototype
int double_then_add_ref(int *num1, int *num2);
Last time, our blogs discussed pointer concepts. Here, you’ll find a way to use them as function parameters. The parameters num1 and num2 are now integer pointers. With this, you can do a pass-by-reference approach.
Function Declaration
int double_then_add_ref(int *num1, int *num2)
{
*num1 = *num1 * 2;
*num2 = *num2 * 2;
return *num1 + *num2;
}
Here, the same operations are made just like in the double_then_add( ) function, except that the parameters are pointer variables. With this, you need to dereference to operate these variables.
Function Call
result = double_then_add_ref(&num1, &num2);
The function call is quite different from the pass-by-value approach. Here, you’ll see that the address of operator (&) is used. You pass a memory location to the pointer, effectively making a pass-by-reference. With this, you can change the contents of your function parameters. This means num1 and num2 can be modified as you run the function double_then_add_ref( ).
Practice Code
Below is an example code run for pass-by-value and pass-by-reference function paramaters.
#include
#include "freertos\FreeRTOS.h"
#include "freertos\task.h"
int num1 = 5;
int num2 = 10;
int result;
int double_then_add(int num1, int num2);
int double_then_add_ref(int *num1, int *num2);
void app_main(void)
{
printf("Run the function with param call by value\n");
result = double_then_add(num1, num2);
printf("The value of the funct is %i\n", result);
printf("num1 = %i\n", num1);
printf("num2 = %i\n\n", num2);
printf("Run the function with param call by ref\n");
result = double_then_add_ref(&num1, &num2);
printf("The value of the funct is %i\n", result);
printf("num1 = %i\n", num1);
printf("num2 = %i\n\n", num2);
while(1)
{
vTaskDelay(100);
}
}
int double_then_add(int num1, int num2)
{
num1 = num1 * 2;
num2 = num2 * 2;
return num1 + num2;
}
int double_then_add_ref(int *num1, int *num2)
{
*num1 = *num1 * 2;
*num2 = *num2 * 2;
return *num1 + *num2;
}
Run the function with param call by value
The value of the funct is 30
num1 = 5
num2 = 10
Run the function with param call by ref
The value of the funct is 30
num1 = 10
num2 = 20