Introduction to functions

We have mentioned earlier that one of the strengths of the C language is C functions. They are easy to define and use. We have used functions in every program.As a pointed out earlier,main is a specially recognized function in C.Every program must have a main function indicate where the program has to begin its executions. If a program is divided into functional parts, then each part is independently coded and late combined into a single unit. These independently coded programs are called subprograms that are much easier to understand, debug and test.In C,such subprogram referred to as functions.

There are two types of functions in C

  1. library functions
  2. user-defined functions

In-built Functions or library Function

These functions are provided by the system and stored in the library, therefore it is also called Library Functions.For example:- scanf,printf..

User Defined Functions

These functions have to be developed by the user at the time of writing a program

The main difference between these two categories is that library function is not required to write by us whereas user-defined functions have to be developed by the user at the time of writing a program.

C Code Example

#include <stdio.h>

	int sum(int a,int b)
                  int c;
		   return c;

void main()
         int i,j,k;
	 printf("Enter element");

	 printf("\n Sum=%d",k);

Parts of Function

  1. Function Definition
  2. Function Call
  3. Function Prototype (function declaration)

Function definition

A function definition also known as function implementation shall include the following element:-

  1. function name
  2. list of parameters
  3. function statement(body)
  4. return type

A general form of a function definition is:-

return_type function_name( parameter list )

Function Call

A function can be called by simple using the function name followed by a list of actual parameters(or argument),if any ,enclosed in parentheses.A general form of function call is:-

function_name(list of actual parameters);

When a compiler encounters a function call,the control is transferred to the function definition ,Then function is expecuted line by line and a value is return when a return statement is encountered.

Function Declaration

Like variable, all function in c program must be declared, before they are invoked.A function declaration consists of four parts:-

  1. function return_type
  2. function name
  3. list of parameter
  4. Termination semi-colon(;)

A general form of function declaration is:-

return_type function_name(list of actual parameters);


In C, When a function calls itself again and again until breakpoint not found.

Suppose we have such a condition which repeats indefinitely if not stopped, thus recursion needs to have some sort of end condition to break recursion.


		return_type function_name( parameter list )

C Code Example

#include <stdio.h>

int factorial(int x)
	{if(x <= 1) 
	   {return 1;}
   	return x * factorial(x - 1);
int main()
    	int a,result;
    	printf("Enter a  integer: ");
    	scanf("%d", &a);
    	printf("\nFactorial of %d = %d", a,result);
    	return 0;