Functions in C

When we write a program to solve a larger problem, we divide that larger problem into smaller sub problems and are solved individually to make the program easier. In C, this concept is implemented using functions. Functions are used to divide a larger program into smaller subprograms such that the program becomes easy to understand and easy to implement. A function is defined as follows...

Function is a subpart of a program used to perform a specific task and is executed individually.

Every C program must contain at least one function called main(). However, a program may also contain other functions.
Every function in C has the following...

Function Declaration

The function declaration tells the compiler about function name, the data type of the return value and parameters. The function declaration is also called a function prototype. The function declaration is performed before the main function or inside the main function or any other function.

Function declaration syntax –

returnType functionName(parametersList);                                                                                                                             

In the above syntax, returnType specifies the data type of the value which is sent as a return value from the function definition. The functionName is a user-defined name used to identify the function uniquely in the program. The parametersList is the data values that are sent to the function definition.

Function Definition

The function definition provides the actual code of that function. The function definition is also known as the body of the function. The actual task of the function is implemented in the function definition. That means the actual instructions to be performed by a function are written in function definition. The actual instructions of a function are written inside the braces "{ }". The function definition is performed before the main function or after the main function.

Function definition syntax -

returnType functionName(parametersList)
{

Actual code...

}

 

Function Call

The function call tells the compiler when to execute the function definition. When a function call is executed, the execution control jumps to the function definition where the actual code gets executed and returns to the same functions call once the execution completes. The function call is performed inside the main function or any other function or inside the function itself.

Function call syntax -

functionName(parameters);

ADVANTAGES OF FUNCTIONS

Types of Functions in C

 

In C Programming Language, based on providing the function definition, functions are divided into two types. Those are as follows...

System Defined Functions

The C Programming Language provides pre-defined functions to make programming easy. These pre-defined functions are known as system defined functions. The system defined function is defined as follows...

The function whose definition is defined by the system is called as system defined function.

 

The system defined functions are also called as Library Functions or Standard Functions or Pre-Defined Functions. The implementation of system defined functions is already defined by the system.

In C, all the system defined functions are defined inside the header files like stdio.hconio.hmath.hstring.h etc., For example, the funtions printf() and scanf() are defined in the header file called stdio.h.

Whenever we use system defined functions in the program, we must include the respective header file using #include statement. For example, if we use a system defined function sqrt() in the program, we must include the header file called math.h because the function sqrt() is defined in math.h.

 

Points to be remembered

User Defined Functions

In C programming language, users can also create their own functions. The functions that are created by users are called as user defined functions. The user defined function is defined as follows...

The function whose definition is defined by the user is called as user defined function.

That means the function that is implemented by user is called as user defined function. For example, the function main is implemented by user so it is called as user defined function.

In C every user defined function must be declared and implemented. Whenever we make function call the function definition gets executed. For example, consider the following program in which we create a function called addition with two parameters and a return value.

In the concept of functions, the function call is known as "Calling Function" and the function definition is known as "Called Function".

When we make a function call, the execution control jumps from calling function to called function. After executing the called function, the execution control comes back to calling function from called function. When the control jumps from calling function to called function it may carry one or more data values called "Parameters" and while coming back it may carry a single value called "return value". That means the data values transferred from calling function to called function are called as Parameters and the data value transferred from called function to calling function is called Return value.

Based on the data flow between the calling function and called function, the functions are classified as follows...

Function without Parameters and without Return value

In this type of functions there is no data transfer between calling function and called function. Simply the execution control jumps from calling-function to called function and executes called function, and finally comes back to the calling function.

In this type of functions there is data transfer from calling-function to called function (parameters) but there is no data transfer from called function to calling-function (return value). The execution control jumps from calling-function to called function along with the parameters and executes called function, and finally comes back to the calling function.

Function without Parameters and with Return value

In this type of functions there is no data transfer from calling-function to called-function (parameters) but there is data transfer from called function to calling-function (return value). The execution control jumps from calling-function to called function and executes called function, and finally comes back to the calling function along with a return value.

Function with Parameters and with Return value

In this type of functions there is data transfer from calling-function to called-function (parameters) and also from called function to calling-function (return value). The execution control jumps from calling-function to called function along with parameters and executes called function, and finally comes back to the calling function along with a return value.

 

Points to be remembered

Scope of Variable in C

When we declare a variable in a program, it cannot be accessed against the scope rules. Variables can be accessed based on their scope. The scope of a variable decides the portion of a program in which the variable can be accessed. The scope of the variable is defined as follows...

Scope of a variable is the portion of the program where a defined variable can be accessed.

The variable scope defines the visibility of variable in the program. Scope of a variable depends on the position of variable declaration.

In C programming language, a variable can be declared in three different positions and they are as follows...

Before the function definition (Global Declaration)

Declaring a variable before the function definition (outside the function definition) is called global declaration. The variable declared using global declaration is called global variable. The global variable can be accessed by all the functions that are defined after the global declaration. That means the global variable can be accessed anywhere in the program after its declaration. The global variable scope is said to be file scope.

Inside the function or block (Local Declaration)

Declaring a variable inside the function or block is called local declaration. The variable declared using local declaration is called local variable. The local variable can be accessed only by the function or block in which it is declared. That means the local variable can be accessed only inside the function or block in which it is declared.

In the function definition parameters (Formal Parameters)

The variables declared in function definition as parameters have a local variable scope. These variables behave like local variables in the function. They can be accessed inside the function but not outside the function.

Recursive Functions in C

In C programming language, function calls can be made from the main() function, other functions or from the same function itself. The recursive function is defined as follows...

A function called by itself is called recursive function.

The recursive functions should be used very carefully because, when a function called by itself it enters into the infinite loop. And when a function enters into the infinite loop, the function execution never gets completed. We should define the condition to exit from the function call so that the recursive function gets terminated.

When a function is called by itself, the first call remains under execution till the last call gets invoked. Every time when a function call is invoked, the function returns the execution control to the previous function call.

Storage Class

 

A storage class defines the scope (visibility) and life-time of variables and/or functions within a C Program. These specifiers precede the type that they modify.

There are the following storage classes, which can be used in a C Program

·        auto

·        register

·        static

·        extern

 

1) The Auto Storage Class

 

The auto storage class is the default storage class for all local variables.

 

{   

int mount;  

auto int month;

}

 

The example above defines two variables with the same storage class, auto can only be used within functions, i.e., local variables.

 

2) The register Storage Class

 

The register storage class is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and can't have the unary '&' operator applied to it (as it does not have a memory location).

 

{ 

  register int  miles;                          

}

 

The register should only be used for variables that require quick access such as counters. It should also be noted that defining 'register' does not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register depending on hardware and implementation restrictions.

 

3) The static Storage Class

 

The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls.

The static modifier may also be applied to global variables. When this is done, it causes that variable's scope to be restricted to the file in which it is declared.

In C programming, when static is used on a class data member, it causes only one copy of that member to be shared by all objects of its class.

 

#include <stdio.h>

 

/* function declaration */ void func(void);

  static int count = 5; /* global variable */

  main()

{

   while(count--)

   {

      func();

   }

   return 0;

}

/* function definition */ void func( void )

{    static int i = 5; /* local static variable */    i++;

 

   printf("i is %d and count is %d\n", i, count); }

 

You may not understand this example at this time because I have used function and global variables, which I have not explained so far. So for now, let us proceed even if you do not understand it completely.

 

4) The extern Storage Class

 

The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern', the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined.

When you have multiple files and you define a global variable or function, which will be used in other files also, then extern will be used in another file to give reference of defined variable or function. Just for understanding, extern is used to declare a global variable or function in another file.

The extern modifier is most commonly used when there are two or more files sharing the same global variables or functions as explained below.

 

First File: main.c

 

 

Second File: write.c

 

#include <stdio.h>

extern int count;

void write_extern(void)

{    count = 5;    printf("count is %d\n", count);}

 

Here, extern keyword is being used to declare count in the second file where as it has its definition in the first file, main.c. Now, compile these two files as follows:

 

 $gcc main.c write.c

This will produce a.out executable program.