Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Wednesday, December 30, 2020

Passing array to function using call by value

Passing array to function using call by value | welcome2protec.com

Just like a variable, An array can also be passed to a function as an arguments. Passing array can be implemented by two ways:

  1. Passing array to a function using call by value.
  2. 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.


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]; 
}

--



Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us