Programmer Coding

Array to Function in C Language

Passing Array to Function in C

There are many problems in C where it is necessary to pass multiple variables of the same type to a function. For example, consider a function that sorts 10 items in ascending order. This type of work should exceed 10 digits by default from the main function. Here, instead of declaring 10 different numbers and passing them to the function, we can declare the array, initialize it and pass it to the function. This will solve all the complexity as the work is now going on with all costs.

We know that array_name contains the address of the first element. Here, we should not forget that we only need to pass the name of the array in the function that will accept the array. The array definition is an arbitrary reference to the array specified by the array name defined as the truth parameter.

Syntax

  1. functionname(arrayname);//passing array

There are 3 ways to declare a function that takes an array as an index.

  • First way:

return_type function(type arrayname[])

Declaring blank subscript notation [] is the widely used technique.

  • Second way:

return_type function(type arrayname[SIZE])

Optionally, we can define size in subscript notation [].

  • Third way:

return_type function(type *arrayname)

Passing an array to function example

#include <stdio.h>

// Function to print elements of an array

void printArray(int arr[], int size) {

printf(“Elements of the array: “);

for (int i = 0; i < size; i++) {

printf(“%d “, arr[i]);

}

printf(“\n”);

}

int main() {

// Declaration and initialization of an array

int numbers[] = {1, 2, 3, 4, 5};

// Passing the array to the function

printArray(numbers, 5);

return 0;

}

Output

Elements of the array: 1 2 3 4 5

function to bubble sort the array

#include <stdio.h>

// Function to perform bubble sort on an array

void bubbleSort(int arr[], int size) {

for (int i = 0; i < size – 1; i++) {

for (int j = 0; j < size – i – 1; j++) {

// Swap adjacent elements if they are in the wrong order

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

}

}

}

}

// Function to print elements of an array

void printArray(int arr[], int size) {

for (int i = 0; i < size; i++) {

printf(“%d “, arr[i]);

}

printf(“\n”);

}

int main() {

int numbers[] = {64, 34, 25, 12, 22, 11, 90};

int size = sizeof(numbers) / sizeof(numbers[0]);

printf(“Original array: “);

printArray(numbers, size);

bubbleSort(numbers, size);

printf(“Sorted array: “);

printArray(numbers, size);

return 0;

}

Output

Original array: 64 34 25 12 22 11 90

Sorted array: 11 12 22 25 34 64 90

Returning array from the function

As we all know a function cannot return more than one value. However, if we try to write the returned message as return a, b, c; to return three values ​​(a, b, c), the function will return the previously specified value; this is c in our case. In some cases, we may need to return more than one value from a single function. In this case the function returns an array.

Passing back an array is similar to passing an array to a function. The name of the array returned by the function. Use the instructions below to make a pass function return an array.

Syntax

  1. int* Function_name() {
  2. //some statements;
  3. return array_type;
  4. }

Example

#include <stdio.h>

#include <stdlib.h>

// Function to perform bubble sort on an array and return the sorted array

int *bubbleSort(int arr[], int size) {

// Allocate memory for the sorted array

int *sortedArr = (int *)malloc(size * sizeof(int));

if (sortedArr == NULL) {

printf(“Memory allocation failed!\n”);

exit(1);

}

// Copy elements of the original array to the sorted array

for (int i = 0; i < size; i++) {

sortedArr[i] = arr[i];

}

// Bubble sort algorithm

for (int i = 0; i < size – 1; i++) {

for (int j = 0; j < size – i – 1; j++) {

if (sortedArr[j] > sortedArr[j + 1]) {

// Swap sortedArr[j] and sortedArr[j+1]

int temp = sortedArr[j];

sortedArr[j] = sortedArr[j + 1];

sortedArr[j + 1] = temp;

}

}

}

return sortedArr; // Return the sorted array

}

// Function to print elements of an array

void printArray(int arr[], int size) {

for (int i = 0; i < size; i++) {

printf(“%d “, arr[i]);

}

printf(“\n”);

}

int main() {

int numbers[] = {64, 34, 25, 12, 22, 11, 90};

int size = sizeof(numbers) / sizeof(numbers[0]);

printf(“Original array: “);

printArray(numbers, size);

// Call the bubbleSort function to sort the array and get the sorted array

int *sortedArray = bubbleSort(numbers, size);

printf(“Sorted array: “);

printArray(sortedArray, size);

// Free dynamically allocated memory

free(sortedArray);

return 0;

}

Output

Original array: 64 34 25 12 22 11 90

Sorted array: 11 12 22 25 34 64 90

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top