Function in C – Declaration and Definition

Written by

studymite

Functions In C

Function in C is a self-contained block of statements that performs some task. Every c program has at least one function that is the main().

If a C program has more than one function than one of those function must be main() (you can't skip main as execution always begins with main() ).

There are two different thing one is declaration of a function and other one is definition of a function.
So, let's understand them:

Defining a function in c :

return_type func_name(parameters) { body; }

    </pre>

return_type: Suppose, we created a function of adding two numbers, after adding the numbers, function return their sum, this is where the return type is used. return_type is the data type of the value that function returns. In some cases, the function does the desired task without returning any value, than in this case return_type is void.

func_name: You can name your function with any name except the reserved words in C/Keywords.

Parameters: Suppose, the function we created for adding two numbers, needs two numbers to add. We can pass these two numbers as parameters.

Example :

 int add(16,24)
   {
      body;
   }

Here, 16 and 24 are parameters.

body: Body of function contains number of statements, which are executed when the function is called.

Function declaration :

Function declaration is used to call a function.

The syntax of function declation is as follows:

    return_type func_name(parameters);

Example :

    int max(int num1, int num2);
Function in C &#8211; Declaration and Definition