Just like a variable, An array can also be passed to a function as an arguments. Passing array can be implemented by two ways:
- Passing array to a function using call by value.
- Passing array to a function using call by reference
In this tutorial we will learn passing array to a function using function call by value method. And the 2nd one passing array to a function using call by reference will discuss in the next tutorial.
- Function call by value
- Array in C
Before going further in this tutorial, you must be aware with-
Passing array to a function using call by value:
When we pass a copy of variables as an actual arguments while calling a function then this is known as function call by value. As we know that, In this type of function call, the actual parameters is copied to the formal parameters.
Example 1: Passing each elements one by one using subscript
#include <stdio.h>
#define SIZE 14
void showElements(char); //Function prototype
void main()
{
char arr[SIZE]={'w','e','l','c','o','m','e','2','p','r','o','t','e','c'};
for (int i=0; i<SIZE; i++)
{
/* Function call by passing each element one by one using subscript*/
showElements(arr[i]);
}
}
/*Function definition*/
void showElements(char c)
{
printf("%c", c);
}
Output:
w e l c o m e 2 p r o t e c
Example 2: Passing individual elements using subscript
If you want to print the specific elements of an array so you can do like this...👇👇.
#include <stdio.h>
#define SIZE 5
void showElements(int, int); //Function prototype
int main()
{
int arr[SIZE]={10, 20, 30, 40, 50};
/* Function call by passing specific elements arr[2] and arr[4] only */
showElements(arr[2], arr[4]);
return 0;
}
/*Function definition*/
void showElements(int num1, int num2)
{
printf("%d\n", num1);
printf("%d\n", num2);
}
Here, we are passing only two elements i.e. arr[2] and arr[4] at the time of function call, this way output will be display only 30 and 50.
Output:
30
50
How to pass entire array to a function as an arguments?
In the above examples, we have passed each elements one by one by using subscript. However we can also pass the entire array to a function as an arguments like this...👇👇
Note: Array name itself is equivalent to element of 1st address i.e. sum(arr) == sum(&arr[0]
Example 3: Passing entire array to a function as an arguments
/*Function definition*/
int sum(int arr[], int size){
int i, sum = 0;
for(i = 0; i < size; sum += arr[i], i++); // sum of array elements.
return sum; //here return type is int so sum is return.
}
int main()
{
int size;
printf("Enter size of array:\n ");
scanf(“%d”, &size); // Taking input of array size
int arr[size], i;
for(i = 0; i < size; i++){
scanf(“%d ”, &arr[i]) // Storing array elements
}
int result = sum(arr, size) // Passing array to a function
printf(“Sum = %d”, result)
return 0;
}
Output:
Enter size of array: 10
1 2 3 4 5 6 7 8 9 10
Sum = 55
________________________________________________________________
/* Statement for(i = 0; i < size; sum += arr[i], i++);
* can be written like this too: */
for(i = 0; i < size; i++){
sum = sum + arr[i];
}
--
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.