C Tutorial

Define C

Define Programming

Programming Approach

First C Program

TurboC Shortcut keys

Compiler vs Interpreter

C Variable

C Keywords

C Data Types

C Comments

C Operators

Hierarchy of Operators

Ex: Arithmetic Operator

C Formatting Output

C Escape Sequence

C if statement

Ex: If statement

C Switch statement

Ex: Switch

Increment / Decrement

C Loops

Ex: Loops

C Nesting Of Loops

Ex: Nested Loops

C Jumping Statements

C Exit(), Gotoxy()

C Arrays 1D

Ex: Arrays 1D

C Arrays 2D

Ex: Arrays 2D

C Sorting

ASCII Value

Character I/O Function

Ex: Character

C String Functions

Ex: Strings

Array of Strings

C Math Functions

User-defined Function

Exercise Function

Local, Reference variable

Function Calling types

Array Passing Function

Recursion Function

Ex: Recursion Function

Constant Variable

Storage Class

C Header Files

C Preprocessor

C Pointers

Pointer to Pointer

C Structures

Exercise Structure

C Typedef

C Enumeration

C File Handling

Ex: File Handling

Command Line Argument

MCQ

Question-Answer

Page Stats

Visitor: 549

C - Passing Array to Function

We can create functions that receives array as argument. To pass array in function, we need to write the array name in the function call.

There are 3 ways to declare function that receives array as argument.

  1. First way:
  2. return_type function_name(datatype arrayname[]);
    Declaring blank subscript notation [] is the widely used technique.
  3. Second way:
  4. return_type function_name(datatype arrayname[SIZE]);
    We can define size in subscript notation [].
  5. Third way:
  6. return_type function_name(datatype *arrayname);
    You can also use the concept of pointer. In pointer chapter, we will learn about it.

Example 1: Passing array to function.

int minarray(int arr[], int size)
{
    int min=arr[0], i=0;
    for(i=1;i<size;i++)
    {
        if(min>arr[i])
        {
            min=arr[i];
        }
    }//end of for
    return min;
}//end of function

void main()
{    
    int i=0, min=0;  
    int numbers[]={4,5,7,3,8,9}; //declaration of array
    clrscr();    
    min=minarray(numbers,6); //passing array with size
    printf("Minimum number is %d \n",min);
    getch();
}
Minimum number is 3

Example 2: WAP for passing Array Elements to function argument.

void display(int n);
void main()
{
    int i;
	int a[] = {10, 20, 30, 40, 50};
    clrscr();
    for(i=0; i<5; i++)
	{
        display(a[i]);
	}
    getch();
}
void display(int n)
{
    printf(" %d ", n);
}
10 20 30 40 50

Example 3: WAP for passing 2D Array to function argument.

Example 4: WAP for passing string to function argument.

void display(char []);
void main()
{
    char s[]="Programming";
    clrscr();
    display(s);
    getch();
}
void display(char str[])
{
    puts(str);
}
Programming

Example 5: WAP for passing array of string to function argument.

void display(char [][10]);
void main()
{
    char s[][10] = {"C", "C++", "Java", "Python", "PHP"};
    clrscr();
    display(s);
    getch();
}
void display(char str[][10])
{
    int i;
	printf("Popular Computer Languages are:");
	for(i=0;i<4;i++)
	{
        puts(str[i]);
	}
}
Popular Computer Languages are:
C
C++
Java
Python
PHP

Exercise on Array to function

  1. WAP to return array using function.
  2. WAP to find length of string using function (passing string).