Computer Programming C: UNIT II: Arrays and Strings

Passing Arrays To Functions

C Programming

To process an array in large programs, you have to be able to pass them to the function, we can pass individual elements of our array to function like other variables or an entire array can be passed.

PASSING ARRAYS TO FUNCTIONS

To process an array in large programs, you have to be able to pass them to the function, we can pass individual elements of our array to function like other variables or an entire array can be passed.

An entire array can be transferred to a function as a parameter. To transfer an array to a function, the array name is enough without subscripts as an actual parameter within the function call.

The corresponding formal parameters are written in the same fashion, and must be declared as an array in formal parameters declaration.

Syntax:

void fun1(int, int[]);

main()

{

int a[10],n;

…….

fun1(n,a);

………

}

void fun1(x,ar[])

{

int x,ar[10];

…..

….

}

You can pass individual elements to a function like any other variables, as long as the array element type matches the function parameter type.


Example 1: Add and display the results of 5 numbers given from the input using arrays to function concept.

/* Program to add and display the results of 5 nos. */

#include <stdio.h>

#include <conio.h>

void add(int, int[]);

main()

{

        int a[5],i,n = 5;

        printf("Enter 5 values");

        for(i=1;i<=5;i++)

        scanf("%d", &a[i]);

        add(n,a); /* add is a function with arguments */

} /* main

void(int x,int ar[])

{

        int sum=0,i;

        for(i=1;i<=5;i++)

                sum=sum+ar[i];

        printf(Sum is %d", sum); /*

} /* void */

OUTPUT

Enter 5 values 10 20 30 40 50

Sum is...150

EXPLANATION: The main function reads the five array elements. The user‒defined function add() is called with an argument x and array ar[]. The function executes and calculate the sum and print the sum value.

Example 2: Capture and store n values into Array and print them.

/* Program to store n values into array */

#include <stdio.h>

#include <conio.h>

void main()

{

      int i, a[5]; /* Local definitions */

      for(i = 0;i<2;i++)

            a[i]=i;

      arr(a); /* function call */

} /* main */

arr(int a[])

{

      int i;

      for(i = 0;i<2;i++)

            printf("value in array %d\t",a[i]);

} /* arr() */

OUTPUT:

value in array 0

value in array 1

value in array 2

EXPLANATION: This program assigns values from 0 to 4 to an integer type array. The contents of arrays displayed in separate function by sending the array name as an argument.

 

Computer Programming C: UNIT II: Arrays and Strings : Tag: Computer Science : C Programming - Passing Arrays To Functions


Computer Programming C: UNIT II: Arrays and Strings



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation