Functions and Integer Single Dimension Array

In this section, you are going to learn

What are the calling conventions of integer single dimension array ?

Call by Value

Call by Reference

int array_name[Column];

Consider a Integer Single Dimension Array

int a[10];

Let us answer few basic questions in this array

If fun(x) is the function call, then fun(typeof(x)) is the prototype / definition

Function Call

Function Definition

Observations

fun(a[0])

void fun(int x) { }

  • Call by Value

fun(a[1])

void fun(int x) { }

  • Call by Value

fun(a[9])

void fun(int x) { }

  • Call by Value

fun(&a[0])

void fun(int *p) { }

  • Call by Reference

fun(&a[1])

void fun(int *p) { }

  • Call by Reference

fun(&a[9])

void fun(int *p) { }

  • Call by Reference

fun(*a)

void fun(int x) { }

  • Call by Value

fun(*(a + 1))

void fun(int x) { }

  • Call by Value

fun(*(a + 9))

void fun(int x) { }

  • Call by Value

fun(a)

void fun(int *p) { }

  • Call by Reference

fun(a + 1)

void fun(int *p) { }

  • Call by Reference

fun(a + 9)

void fun(int *p) { }

  • Call by Reference

fun(&a)

void fun(int (*p) [10] ) { }

  • Call by Reference

If Declaration has ONE dereference operator, and

  • Expression has ONE dereference operator [], and

  • Expression does not have &

  • then it is call by value

If Declaration has ONE dereference operators, and

  • Expression has ONE dereference operator *, and

  • Expression does not have &

  • then it is call by value

If Declaration has ONE dereference operator, and

  • Expression has ONE dereference operators [] or *, and

  • Expression has ONE &

  • then it is call by reference

  • Example : &a[0]

If Declaration has ONE dereference operator, and

  • Expression has ZERO dereference operator [ ] or *, and

  • Expression has ZERO & operator

  • then it is call by reference

  • Example : a + 1, a + 4

If Declaration has ONE dereference operator, and

  • Expression has ZERO dereference operator [ ] or *, and

  • Expression has ONE & operator

  • then it is call by reference

  • Example : &a

Let us look at examples of Call by Value

Example for Call By Value with [ ]

  • Step 1 : Define a integer array a

int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  • Step 2 : Pass an individual integer a[2] to a function. Call by Value

fun(a[2]);

Individual array elements can be accessed using [ ]

In this case a[2] is third integer in the array

a[2] is fully dereferenced and there is no & symbol in fun(a[2]). Hence this is Call By Value

  • Step 3 : Define function fun

void fun(int x)
{

}
  • Step 4 : Change value of x inside function fun

void fun(int x)
{
        x = 100;
        printf("x = %d\n", x);
}
  • See the full program below

#include <stdio.h>

void fun(int x)
{
        x = 300;
        printf("x = %d\n", x);
}

int main(void)
{
        int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        fun(a[2]);

        for (int i = 0; i < 10; i++)
        {
                printf("a[%d] = %d\n", i, a[i]);
        }

        printf("\n");

        return 0;
}
  • Output is as below

x = 300
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5
a[5] = 6
a[6] = 7
a[7] = 8
a[8] = 9
a[9] = 10

Changing value of x inside function fun DOES NOT change a[2] in array a

Example for Call By Value with *

  • Step 1 : Define a integer array a

int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  • Step 2 : Pass an individual integer *(a + 2) to a function. Call by Value

fun( *(a + 2) );

Individual array elements can be accessed using *

In this case *(a + 2) is third integer in the array

*(a + 2) is fully dereferenced and there is no & symbol in fun( *(a + 2) ). Hence this is Call By Value

  • Step 3 : Define function fun

void fun(int x)
{

}
  • Step 4 : Change value of x inside function fun

void fun(int x)
{
        x = 300;
        printf("x = %d\n", x);
}
  • See the full program below

#include <stdio.h>

void fun(int x)
{
        x = 300;

        printf("x = %d\n", x);
}

int main(void)
{
        int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        fun( *(a + 2) );

        for (int i = 0; i < 10; i++)
        {
                printf("a[%d] = %d\n", i, a[i]);
        }

        return 0;
}
  • Output is as below

x = 300
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5
a[5] = 6
a[6] = 7
a[7] = 8
a[8] = 9
a[9] = 10

Changing value of x inside function fun DOES NOT change *(a + 2) in array a

Remember a[2] and *(a + 2) are one and the same

Let us look at examples of Call by Reference

Example for Call By Reference with &a[ ]

  • Step 1 : Define a integer array a

int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  • Step 2 : Pass address of an individual integer &a[2] to a function. Call by Reference

fun( &a[2] );

Address of individual array elements can be accessed using &

In this case &a[2] is the address of third integer in the array

Since we are passing address of third integer to function fun, it is called call by reference with respect to third integer

  • Step 3 : Define function fun

void fun(int *x)
{

}
  • Step 4 : Change value of *x inside function fun

void fun(int *x)
{
        *x = 300;
        printf("*x = %d\n", *x);
}
  • See the full program below

#include <stdio.h>

void fun(int *x)
{
        *x = 300;
        printf("*x = %d\n", *x);
}

int main(void)
{
        int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        fun( &a[2] );

        for (int i = 0; i < 10; i++)
        {
                printf("a[%d] = %d\n", i, a[i]);
        }

        return 0;
}
  • Output is as below

*x = 300
a[0] = 1
a[1] = 2
a[2] = 300
a[3] = 4
a[4] = 5
a[5] = 6
a[6] = 7
a[7] = 8
a[8] = 9
a[9] = 10

Changing value of *x inside function fun CHANGES a[2] in array a

Example for Call By Reference with (a + x)

  • Step 1 : Define a integer array a

int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  • Step 2 : Pass address of individual integer a + 2 to a function. Call by Reference

fun( a + 2 );

In this case a + 2 is the address of third integer in the array

Since we are passing address of third integer to function fun, it is called call by reference with respect to third integer

  • Step 3 : Define function fun

void fun(int *x)
{

}
  • Step 4 : Change value of *x inside function fun

void fun(int *x)
{
        *x = 300;

        printf("*x = %d\n", *x);
}
  • See the full program below

#include <stdio.h>

void fun(int *x)
{
        *x = 300;

        printf("*x = %d\n", *x);
}

int main(void)
{
        int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        fun( a + 2 );

        for (int i = 0; i < 10; i++)
        {
                printf("a[%d] = %d\n", i, a[i]);
        }

        return 0;
}
  • Output is as below

*x = 300
a[0] = 1
a[1] = 2
a[2] = 300
a[3] = 4
a[4] = 5
a[5] = 6
a[6] = 7
a[7] = 8
a[8] = 9
a[9] = 10

Changing value of *x inside function fun CHANGES a[2] in array a

  • Step 1 : Consider a integer array

int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  • Step 2 : Pass full array array to a function

fun( a );

Note that we are passing starting address of array

Hence function fun has read and write access to all Bytes of array

  • Step 3 : Define a function

void fun(int *x)
{
        x[0]  = 100;
        x[1]  = 200;
        x[2]  = 300;
        x[3]  = 400;
        x[4]  = 500;
        x[5]  = 600;
        x[6]  = 700;
        x[7]  = 800;
        x[8]  = 900;
        x[9]  = 1000;
}

function fun has access to all integers

  • See full program below

#include <stdio.h>

void fun(int *x)
{
        x[0]  = 100;
        x[1]  = 200;
        x[2]  = 300;
        x[3]  = 400;
        x[4]  = 500;
        x[5]  = 600;
        x[6]  = 700;
        x[7]  = 800;
        x[8]  = 900;
        x[9]  = 1000;
}

int main(void)
{
        int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        printf("----- Before call by reference -----\n");
        for (int i = 0; i < 10; i++)
        {
                printf("a[%d] = %d\n", i, a[i]);
        }

        fun(a);

        printf("----- After call by reference -----\n");
        for (int i = 0; i < 10; i++)
        {
                printf("a[%d] = %d\n", i, a[i]);
        }

        return 0;
}
  • Output is as below

----- Before call by reference -----
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5
a[5] = 6
a[6] = 7
a[7] = 8
a[8] = 9
a[9] = 10

----- After call by reference -----
a[0] = 100
a[1] = 200
a[2] = 300
a[3] = 400
a[4] = 500
a[5] = 600
a[6] = 700
a[7] = 800
a[8] = 900
a[9] = 1000
  • Step 1 : Consider a integer array

int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  • Step 2 : Pass full array by reference

fun( a + 5 );

Note that we are passing part of the array by reference

In this case, we are passing address of 6th integer

Hence function fun has read and write access to Bytes 5, 6, 7, 8, 9 in forward direction

Hence function fun has read and write access to Bytes 0, 1, 2, 3, 4 in backward direction

  • Step 3 : Define a function

void fun(int *x)
{
        x[-5] = 100; // Same as a[0]
        x[-4] = 200; // Same as a[1]
        x[-3] = 300; // Same as a[2]
        x[-2] = 400; // Same as a[3]
        x[-1] = 500; // Same as a[4]
        x[0]  = 600; // Same as a[5]
        x[1]  = 700; // Same as a[6]
        x[2]  = 800; // Same as a[7]
        x[3]  = 900; // Same as a[8]
        x[4]  = 1000; // Same as a[9]
}

Note the relative access mechanism used inside function fun

  • See full program below

#include <stdio.h>

void fun(int *x)
{
        x[-5] = 100; // Same as a[0]
        x[-4] = 200; // Same as a[1]
        x[-3] = 300; // Same as a[2]
        x[-2] = 400; // Same as a[3]
        x[-1] = 500; // Same as a[4]
        x[0]  = 600; // Same as a[5]
        x[1]  = 700; // Same as a[6]
        x[2]  = 800; // Same as a[7]
        x[3]  = 900; // Same as a[8]
        x[4]  = 1000; // Same as a[9]
}

int main(void)
{
        int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        printf("----- Before call by reference -----\n");

        for (int i = 0; i < 10; i++)
        {
                printf("a[%d] = %d\n", i, a[i]);
        }

        fun( a + 5 );

        printf("----- After call by reference -----\n");

        for (int i = 0; i < 10; i++)
        {
                printf("a[%d] = %d\n", i, a[i]);
        }

        return 0;
}
  • Output is as below

----- Before call by reference -----
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5
a[5] = 6
a[6] = 7
a[7] = 8
a[8] = 9
a[9] = 10

----- After call by reference -----
a[0] = 100
a[1] = 200
a[2] = 300
a[3] = 400
a[4] = 500
a[5] = 600
a[6] = 700
a[7] = 800
a[8] = 900
a[9] = 1000
  • Step 1 : Consider a integer array

int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  • Step 2 : Pass the address of array a to function fun

fun(&a);
  • Step 3 : Define the function fun

void fun( int (*ptr)[10] )
{

}

Note that int (*ptr)[10] is pointer to an array of 10 integers

Which means incrementing ptr will increment by 10 Bytes and decrementing ptr will decrement by 10 Bytes

  • Step 4 : Access individual integers inside function fun

void fun( int (*ptr)[10] )
{
        //Change individual integers
        (*ptr)[0] = 100;
        (*ptr)[1] = 200;
        (*ptr)[2] = 300;
        (*ptr)[3] = 400;
        (*ptr)[4] = 500;
        (*ptr)[5] = 600;
        (*ptr)[6] = 700;
        (*ptr)[7] = 800;
        (*ptr)[8] = 900;
        (*ptr)[9] = 1000;
}
  • See the full program below

#include <stdio.h>

void fun( int (*ptr)[10] )
{
        //Change individual integers
        (*ptr)[0] = 100;
        (*ptr)[1] = 200;
        (*ptr)[2] = 300;
        (*ptr)[3] = 400;
        (*ptr)[4] = 500;
        (*ptr)[5] = 600;
        (*ptr)[6] = 700;
        (*ptr)[7] = 800;
        (*ptr)[8] = 900;
        (*ptr)[9] = 1000;
}

int main(void)
{
        int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        printf("----- Before call by reference -----\n");

        for (int i = 0; i < 10; i++)
        {
                printf("a[%d] = %d\n", i, a[i]);
        }

        fun( &a );

        printf("----- After call by reference -----\n");

        for (int i = 0; i < 10; i++)
        {
                printf("a[%d] = %d\n", i, a[i]);
        }

        return 0;
}
  • Output is as below

----- Before call by reference -----
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5
a[5] = 6
a[6] = 7
a[7] = 8
a[8] = 9
a[9] = 10

----- After call by reference -----
a[0] = 100
a[1] = 200
a[2] = 300
a[3] = 400
a[4] = 500
a[5] = 600
a[6] = 700
a[7] = 800
a[8] = 900
a[9] = 1000