Array to Function Program in C.
Hi Everyone,
Today we will discuss about Array to Function in C. This Topic is interesting and we can use array in C functions like function call value and function call by reference. Before starting this topic please go through below topics.
https://dheeraj60.blogspot.com/2020/06/array-and-its-beneifts-in-c-program.html
https://dheeraj60.blogspot.com/2020/06/multi-dimensional-array2d-and-3d-in-c.html
https://dheeraj60.blogspot.com/2020/06/call-by-reference-function-uses-and.html
https://dheeraj60.blogspot.com/2020/06/function-call-value-uses-and-benefits.html
Let’s Discuss about Array to function. Just like variables, array can also be passed to a function as an argument as of now we used variables in functions. Passing array elements to a function is similar to passing variables to a function.
Let’s understand passing simple array in Function by below program.
#include <stdio.h>
#include <stdlib.h>
void value(int marks1, int marks2)
{
printf("%d\n", marks1);
printf("%d\n", marks2);
}
int main()
{
int markarray[] = {80, 90, 95, 85};
value(markarray[1], markarray[2]);
return 0;
}
Here we simply use value function for passing second and third element of Array. In other word we can say Passing second and third elements to function value(). Array we already discussed in previous blog so you can take reference from that blog. So output will be 90 and 95
Below is the output of above program in compiler Code block.
Now understand with different program by using passing array to function using call by method by below simple program.
#include <stdio.h>
#include <stdlib.h>
void dotc( char abc)
{
printf("\n%c ", abc);
}
int main()
{
char arr[] = {'a', 'b', 'c', 'd', 'e'};
for (int i=0; i<5; i++)
{
dotc (arr[i]);
}
return 0;
}
Here in above program we use character variable and I am passing each element one by one using subscript and function dotc . For iteration we use loop. So output will be a b c d e
Below is the output of above program in compiler code-block.
Hope you are able to understand both the program. Let’s understand how we can use array to function using call by reference method.
Note: When we pass the address of an array while calling a function then this is called function call by reference and when we pass an address as an argument, the function declaration should have a pointer as a parameter to receive the address. We already discuss this in my previous blog here we use the same in array. Below is the Program for printing value from 1 to 10 by using array and function call by reference method.
#include <stdio.h>
#include <stdlib.h>
void value( int *a)
{
printf("%d \n ", *a);
}
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int i=0; i<10; i++)
{
value (&arr[i]);
}
return 0;
}
Here value (&arr[i]) passing address of array elements. So we use function value which we declare as void value int data type and by using array and printing the value from 1 to 10.
Hope you are able to understand above programs by using Arrays in Function .
Below is the output of above program in compiler code block.
To understand more we can use 2D(two dimensional) array in C function Program.
Below is the 2D array program used by Function and taking input from user:
#include <stdio.h>
#include <stdlib.h>
void display(int a[2][2]);
int main()
{
int a[2][2];
printf(" Please enter 4 numbers:\n");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
scanf("%d", &a[i][j]);
display(a);
return 0;
}
void display(int a[2][2])
{
printf("Display the value:\n");
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
printf("%d\n", a[i][j]);
}
}
}
Here we use display function for passing two-dimensional array to a function. Hope you are able to understand this program as in previous blogs we already discussed about 2D array.
Below is the output of above Program in compiler code block.
Hope you are able to understand how to use arrays in several functions used in C program and also we use 2D array to display value by using function . Please go through this blog carefully and write some programs if any issue let me know . In next blog i will come up with new topic Structure in C programming which is important in C program. Please go through all previous blogs so that it’s easy for you to understand structure in C program.
Thanks.
Well explained Sir !
ReplyDelete