C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Functions and Arrays

C Programming

In this tutorial we will learn how to pass and use arrays in functions in C programming language.

We know that when we want to store data of same data type under one variable name we take help of arrays.

Function declaration to accept one dimensional array

To accept one dimensional array our function declaration will look like the following.

returnType functionName(type arr[], type size);

Example:

float findAverage(int arr[], int size);

In the above function declaration we have a function by the name findAverage. The first parameter is of type int and takes one dimensional integer array. The second parameter of int type is for the size of the array i.e., number of elements. The return type of this function is set to float so, it will return a floating point value.

Passing one dimensional array to function

To pass a one dimensional array we simply write the name of the array variable as the function argument.

For example, if we want to store marks of a student in 5 papers then we can create an integer array of size 5 and type int and give it a name lets say, score.

int score[5] = {90, 80, 70, 75, 85};

Note! In the above example we are assuming that the total marks of each paper is 100 and score will be an integer value.

Now lets say, we want to find the average score of the student by creating a function findAverage() which takes an integer array and returns a floating point value.

In the following example we are passing a one dimensional array of type int and by the name score to the findAverage() function.

int score[5] = {90, 80, 70, 75, 85};
int papers = 5;
float avg = findAverage(score, papers);

The complete code is given below.

#include <stdio.h>

float findAverage(int [], int);

int main(void) {
  int score[5] = {90, 80, 70, 75, 85};
  int papers = 5;
  float avg = findAverage(score, papers);
  printf("Average: %f\n", avg);
  return 0;
}

float findAverage(int arr[], int size) {
  // variables
  int
    i;
  float
    sum = 0,
    avg = 0;

  // find total
  for (i = 0; i < size; i++) {
    sum += arr[i];
  }
  
  // find average
  avg = sum / size;
  
  // return average
  return avg;
}

Output:

Average: 80.000000

Function declaration to accept two dimensional array

To accept two dimensional array our function declaration will look like the following.

returnType functionName(type arr[][C], type rows, type cols);

Example:

void printAverage(int arr[][C], int rows, int cols);

In the above example we have a function by the name printAverage. The first parameter is a two dimensional array as we are using two square brackets representing the number of rows and columns.

It is important to specify the size of the second dimension i.e., number of columns. So, in the above function declaration we are passing a constant C.

The second and third parameters int rows and int cols represents the number of rows and columns in the array arr respectively.

The above function returns no value so, the return type is set to void.

Passing two dimensional array to function

To pass a two dimensional array to a function all we have to do is write the array name.

Lets say, a student gives 3 tests daily for 5 days and we want to print the average score of each day.

So, we will first create a two dimensional array lets say score and it will have 5 rows denoting the 5 days and 3 columns denoting the 3 tests.

int score[5][3] = {
  {60, 70, 80},
  {90, 50, 70},
  {80, 75, 75},
  {90, 85, 81},
  {60, 75, 80}
};

So, if we want to pass this two dimensional array score to a function printAverage we have to write the following.

printArray(score, ROWS, COLS);

Complete code will look like the following.

#include <stdio.h>

void printAverage(int [][3], int, int);

int main(void) {
  // variables
  int
    ROWS = 5,
    COLS = 3;
    
  int score[5][3] = {
    {60, 70, 80},
    {90, 50, 70},
    {80, 75, 75},
    {90, 85, 81},
    {60, 75, 80}
  };
  
  printAverage(score, ROWS, COLS);
  
  return 0;
}

void printAverage(int arr[][3], int rows, int cols) {
  // variables
  int
    r, c;
  float
    sum, avg;

  // find average and print it
  for (r = 0; r < rows; r++) {
    sum = 0;
    for (c = 0; c < cols; c++) {
      sum += arr[r][c];
    }
    avg = sum / cols;
    printf("Average on Day #%d = %f\n", (r + 1), avg);
  }
}

Output:

Average on Day #1 = 70.000000
Average on Day #2 = 70.000000
Average on Day #3 = 76.666664
Average on Day #4 = 85.333336
Average on Day #5 = 71.666664