Mastering the Art of Formal and Actual Arguments in C

Where do write functions??

A program contains main() function. When a new function is written, we can write the entire function, just before the main() function

#include <stdio.h>
function definition
void main()
{
   statements;
}

Another way to write the function is to write the function prototype function and then write the definition after the main() function.

#include <stdio.h>
function prototype
void main()
{
      statements;
}

Formal arguments and actual arguments

When a function is defined, it may have some parameters. These parameters are useful to receive data from outside. They are called ‘formal arguments’. When the function is called, we should pass data to the function. This data or values represent ‘actual arguments’.

Example:void sum(int x,int y)   /* here x and y are formal arguments */
   {
      printf("\n sum=%d",x+y)
   }
   void main()
   {
      int a=5, b=10;
      sum(a,b);
   }

Passing arrays to the functionsFormal and Actual Arguments in C

It is possible to return an entire array from a function. To pass a 1D 2D array. Now, the marks obtained by several such classes in a school become an example for 3D array.3D array can be created in 2 ways. The first way is to declare the array to a function, we should declare the array as a parameter in the function, as

void myfunction(int arr[])
{
   function body;
}    

Here, myfunction() has a parameter, i.e, integer type array which can accept an int type 1D aray when called. we can call this function and pass a 1D array as:

myfunction(arr);

Similarly, we can write a function with a 2D array as its parameter, as:

void myfunction(int arr[][])
{
    function body;
}    

This function expects a 2D array when called. We can pass a 2D array, as

myfunction(arr);

If a function returns an array, we can write return statement in the function, such as:

return arr;

Here, ‘arr’ represents any type of array. It can be a 1D array, a 2D array or 3D array. The function returns the entire array by just returning the array name. When a function returns an array, we should write the array type before the function name.

/*function that accepts and returns a 1D array */
int[] myfunction(int arr[])
{
    statements;
    return arr;
}

/* function that accepts and returns a 2D array */
int[][] myfunction(int arr[][])
{
    statements;
    return arr;
}
Naveed Tawargeri
 

Hi, I'm Naveed Tawargeri, and I'm the owner and creator of this blog. I'm a Software Developer with a passion for Programming.