C Functions

A function in C is a module of code that takes parameters in , does some computation, and returns a new piece of information based on the parameter information. Functions present a higher level of abstraction and facilitate a divide-and-conquer strategy for program decomposition.

Basically, collections of statements form a function in order to perfom some specific task.

Functions break large computing tasks into smaller ones, and enable people to build on what others have done instead of starting over from scratch.

Structure of function

A function has a name, a list of arguments which it takes when called, and the block of code it executes when called. 

The structure of a function can be broken into two parts as declaration and definition. 

function declaration

Declaration of a function tells the compiler about a function's name, return type, and parameters. Function declaration is also called function prototypes.

Function prototype enables the compiler to perform type-checking which catches many coding errors at compile time.

Basic structure for declaring a function (function prototype) is as follows:

RETURN_TYPE name_of_function ( PARAMETER_TYPE name_of_param,PARAMETER_TYPE name_of_param, ...); 

Here is some practical example for declaring functions:

int some_procedure(void);
void display_results(double num1,double num2);
double triangle_area(double side1, double side2, double side3);

function definition

A function definition provides the actual body of the function. In other words, it contains the actual workings of the function including the declarations and statements of the function algorithm.

A function may be declared multiple times in a program (provided the declarations are non-conflicting) but may be defined only once.

Syntax for function definition is as follows:

RETURN_TYPE name_of_function ( PARAMETER_TYPE name_of_param,PARAMETER_TYPE name_of_param, ...){

      //statements 

      //... 

}

Example:

/* This function takes two int arguments, and returns an int. */
int add_integers(int int1, int int2)
{
    int sum = int1 + int2;
    return sum;
}

Before digging deep into function definition, let us look at main() function.

main() function

The main() function is treated the same as every function in C. It has a return type  which is int or void and accepts inputs via command line arugments. 

 int                       // the main function will usually returns a 0 if successful 
 main()                    // this is the name, in this case: main 
 { 
     //...                 // this is the body of the function (lots of code can go here) 
 } 

The only difference of main() function with other regular function is that the main() function is "called" by the operating system when the user runs the program. Hence, the main() function is the entry point for program execution.

Return Type of a C function

Every C function must specify the type of data that is being generated. For example, the add_integers() function above returns a value of type "int". Inside the function, the line "return X;" must be found, where X is a value or variable containing a value of the given type. Here, sum is return value and is of type int.

Calling a function

Syntax for invoking a function is as follows:

variable = function_name ( args, ...);

Here is example program to illustrate function definition:

//program to illustrate the function definition
#include <stdio.h>

/* This function takes two int arguments, and returns an int. */
int add_integers(int int1, int int2)
{
    int sum = int1 + int2;
    return sum;
}

/* A function that calls add_integers() */
void caller_function(void)
{
    int a=1, b=2,result;
    result = add_integers(a,b); /* c = 3 */
    printf("Sum : %d",result);
}

int main(){
    caller_function();
    return 0;
}

Here, first main() function is called by machine when program starts to run. Then caller_function() is called from it, which again calls another function add_integers(int,int). add_integers(int,int) takes two integers and return the sum of these two integers which is displayed at caller_function(). Finally control comes back to main() which returns 0 means execution terminates successfully.

Parameters in C functions

A Parameter is the symbolic name for "data" that goes into a function. There are two ways to pass parameters in C: Pass by Value and Pass by Reference.

  • Pass by Value

    Pass by Value means that a copy of the data is made and stored by way of the name of the parameter. Any changes to the parameter have NO affect on data in the calling function.

In above mentioned program, invoking add_integers(a,b) is pass by value.

  • Pass by Reference

    reference parameter "refers" to the original data in the calling function. Thus any changes made to the parameter are ALSO MADE TO THE ORIGINAL variable.

    There are two ways to make a pass by reference parameter:

    1. ARRAYS

      Arrays are always pass by reference in C. Any change made to the parameter containing the array will change the value of the original array.

    2. The ampersand used in the function prototype.

      function ( & parameter_name )

      To make a normal parameter into a pass by reference parameter, we use the "& param" notation. The ampersand (&) is the syntax to tell C that any changes made to the parameter also modify the original variable containing the data.

The more explanation and program example is in Pointers in C chapter.

C Break, Continue and return
C Storage Classes