| Previous | Home | Next |
Function return multiple values using Array in C Programming language
# Defining a Function
Following is the source code for a function called add(). This function takes two parameters a and b and returns the addition of them:
/* Simple value addition program using array as argument */
add (int arr[], int n)
{
int i, sum=0; /* local variable declaration */
for (i=0;i<n; i++)
sum+=arr [i];
getch();
return sum;
}
* Return type is int, Function name is add and Parameters are arr[], n
# Function Declarations
For the above defined function add(), following is the function declaration:
add (int arr[], int n);
# Calling a Function
To call a function, you simply need to pass the required parameters along with function name, and if function returns a value, then you can store returned value.
#include <stdio.h>
#include <conio.h>
/* function declaration */
int add (int arr[], int n);
main()
{
int a[5]={2,4,,6,8,10};
int b[8]={1,3,5,7,9,11,13,15};
int c[10]={1,2,3,4,5,6,7,8,9,10};
clrscr();
printf ("sum of the array a : %d \n",add(a,5));
printf ("sum of the array b : %d \n",add(b,8));
printf ("sum of the array c : %d \n",add(c,10));
}
/* Simple value addition program using array as argument */
add (int arr[], int n)
{
int i, sum=0; /* local variable declaration */
for (i=0;i<n; i++)
sum+=arr [i];
getch();
return sum;
}
Output :
sum of array a : 30
sum of array b : 64
sum of array c : 55
| Previous | Home | Next |