Once you have understood how to create a function with arguments , it's imperative to understand the multiple ways in which we can pass arguments to a function that take arguments.
There are two ways by which we could pass the arguments to a function with arguments -
By Value
By Reference.
What happens when we call a function with arguments, by value?
When we call a function that has arguments, by passing it arguments by value, the values in the actual arguments passed to the
it are only copied in its formal arguments. Hence, the changes made on the values contained in the formal arguments within the function definition
does not affect the original value contained in actual arguments. Don't worry, if you have not got it yet. Let's understand this concept with an example.
Calling a function with argument, by value
In the upcoming example, we are going to create a function named add10 which adds 10 to the variable of int type passed to it, when this function is called.
/* Calling a function with argument, by value */
#include<stdio.h>
int main()
{
/* function prototype declaration*/
void add10(int);
int a = 10;
printf("The value in a is : %d \n", a);
/*calling the add10 function*/
add10(a);
printf("After the add10 function is called \n");
printf("The value in a is : %d \n", a);
return 0;
}
/* function to add 10 to the int value passed to it */
void add10(int i)
{
i = i + 10;
}
Output-
The value in a is : 10
After the add10 function is called
The value in a is : 10
Program Analysis
Declaring function prototype -
In the above mentioned example, we have created a function named add10 and have declared its prototype as -
The prototype of this function is declared with a voidreturn type, hence, this method will not return any value when it is called.
This function will be passed an int value when it is called, as we have specified int type within the parenthesis().
Advertisement
Calling the function - We have called the add10 method by passing the value of an int variable named a as an argument.
The int variable a has a value 10 and it can also be referred as "actual argument" of the add10 method, when it is called.
add10(a); /* add10 function is called */
Function definition - When the add10 function is called, it receives a copy of the value of the variable(a) passed to it
and this value is copied into
int variable named i, declared in the definition of add10 function.
/* add10 function is defined */
void add10(int i)
{
i = i + 10;
}
In the function definition, the i variable can also be referred as a "formal argument" of add10 function.
Eventually, the add10 function adds 10 to the value contained in i, which is not reflected when the value of variable a is printed again on the console after calling the add10 function.
This proves that when we call a function that has arguments, the values in the actual arguments passed to the
it are only copied in its formal arguments. Hence, the changes made on the values contained in the formal arguments within the function definition
does not affect the original value contained in actual arguments.
Another example of calling a function by value
In the upcoming example, we are going to create a function named swap_char which accepts two char arguments and tries to interchange the value contained in these two char variables
passed to it, but this swapping/interchange will not take place because we have called the function and have passed it arguments by value. Let's see the code.
/*Calling a function with arguments which swaps or interchanges the value in two char variables*/
#include<stdio.h>
int main()
{
void swap_char(char,char); /* function prototype declaration*/
char a ='x';
char b ='y';
printf("The character value in a is : %c \n", a);
printf("The character value in b is : %c \n", b);
swap_char(a,b); /*calling the swap_char function*/
printf("After the swap_char function is called \n");
printf("The character value in a is : %c \n", a);
printf("The character value in b is : %c \n", b);
return 0;
}
/* function to swap characters */
void swap_char(char c, char d)
{
char e;
e = c;
c = d;
d = e;
}
Output-
The character value in a is : x
The character value in b is : y
After the swap_char function is called
The character value in a is : x
The character value in b is : y
In the next article, we are going to explain how to call a function that has arguments by passing it arguments by reference. See you in the next tutorial!