Code Scrappers

Functions

Function in C is a group of statements that together perform a task. Every program in C has at least one function, which is main(), and all the most trivial programs can define additional functions.

Our code can be divided into separate functions. The division of the code into different functions is entirely dependent on us, but logically the division usually is so each function performs a specific task.

A function declaration's job is to tell the compiler about a function's name, return type, and parameters. A function definition provides us the actual body of the function.

The standard library of C provides numerous built-in functions that your program can call. Consider an example, function strcat() to concatenate two strings, function memcpy() to copy one memory location to another location and many more functions.

A function can be known with various names like a method or a sub-routine or a procedure, etc.

Defining a Function

The generalized format of a function definition in C programming language is as follows:


return_type function_name( parameter list )

{

  body of the function

} 

A function definition comprises of a function header and a function body.
Here are all the parts of a function:

Return Type :

A function may return a value. The data type of the value that the function returns is "return_type". Some functions perform the required operations without returning a value.In such cases, the return_type is the keyword void.

Function Name :

This is the actual name of the function.The function name along with the parameter list constitute the function signature.

Parameters:

A parameter is like a placeholder.When we invoke a function,we pass a value to the parameter.This value is called as actual parameter or argument.The parameter list includes the type, order, and number of the parameters of a function.Parameters in a function are optional; that is, a function may contain no parameters.

Function Body:

The function body comprises of a collection of statements that defines the job going to be performed by the function.

The source code for a function called minimum() is as follows. This function takes in two parameters a and b and returns the minimum between the two:


  /*function to return the minimum between two numbers */
int minimum(int a, int b)
 {

      int c;

      if (a < b)

          {

            c = a;

          }

      else

          {

            c = b;

          }

      return c;
 }
 

Function Declarations

A function declaration informs the compiler about the function name.It also informs the compiler how to call the function.The definition of the actual body of the function can be done separately.

A function declaration has the following parts:


  return_type function_name ( parameter list ); 

For the above declared function minimum(), the function declaration is as follows:


  int minimum(int a, int b); 

Parameter names are not very crucial in function declaration only their type is required, so following is also valid declaration:


  int minimum(int, int); 

Function declaration is required when we define a function in one source file and then call that function in another file.

Calling a Function

While creating a C function, we need to give a definition of what the function has to do. To use a function, we should call that function to perform the defined task.

When a function is called by a program, program control is transferred to the called function. A called function performs a desired task, and when its return statement of the function is executed or when its function-ending closing brace is reached, it returns program control back to the main program.

To call a function, we just need to pass the required parameters along with function name, and if function returns a value, then we can store the returned value.

For example:


    #include <stdio.h>

    int max(int num1, int num2);  /* declaration of the function */

    void main ()

   {

        int a = 100;


        int b = 200;

        int r;

        r = max(a, b);

    printf( "Max value is : %d\n", r );

   }


       int max(int num1, int num2)

      {



      int result;

     if (num1 > num2)

         result = num1;

    else

         result = num2;

    return result;

  }

   

The max() function is kept along with main() function and compiled with the source code. While running the final code, it would produce the following result: Max value is: 200

<< Back Next >>