Functions in c

A function is a block of code that performs a particular task. There are some situations when we need to write a particular block of code for more than once in our program. This may lead to bugs and irritation for the programmer. C language provides an approach in which you need to declare and define a group of statements once and that can be called and used whenever required. This saves both time and space.

C functions can be classified into two categories,

  • Library functions
  • User-defined functions

types of functions in C

Library functions are those functions which are already defined in C library, example printf(), scanf(), strcat() etc. You just need to include appropriate header files to use these functions. These are already declared and defined in C libraries.

User-defined functions are those functions which are defined by the user at the time of writing program. Functions are made for code reusability and for saving time and space.


Benefits of Using Functions

  1. It provides modularity to the program.
  2. It helps in code reusability. You just have to call the function by its name to use it.
  3. In case of large programs with thousands of code lines, debugging and editing becomes easier if you use functions.
  4. It makes the program more easier to design and understand.

Function declaration

General syntax of function declaration is,

return-type function-name (parameter-list) ;

Like variable and an array, a function must also be declared before its called. A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately. A function declaration consist of 4 parts.

  • return-type
  • function name
  • parameter list
  • terminating semicolon

Function definition Syntax

General syntax of function definition is,

return-type function-name (parameter-list)
{
  function-body ;
}

The first line return-type function-name(parameter) is known as function header and the statement within curly braces is called function body.

Note: While defining a function, there is no semicolon(;) after the parenthesis in the function header,unlike while declaring the function or calling the function.


return-type

There might be situations where we want to store the result that is evaluated by the function. In such cases, a function with suitable return type(int,float,char,double) is used. Return type specifies the type of value(int,float,char,double) that function is expected to return to the program calling the function.

Note: In case your function doesn't return any value, the return type should be void.


function-name

function name specifies the name of the function. The function name is any valid C identifier and therefore must follow the same rule of formation as other variables in C. Rules for an identifier can be revised here)


parameter-list

The parameter list declares the variables that will receive the data sent by the calling function in the program. They are often referred as formal parameters. These parameters are also used to send values to calling program.


function-body

The function body contains the declarations and the statements(algorithm) necessary for performing the required task. The body is enclosed within curly braces { } and consists of three parts.

  • local variable declaration(if any).
  • function statement that performs the tasks of the function.
  • a return statement that return the value evaluated by the function(if return type is void,then no return statement is required).

Functions and Arguments

Arguments are the values specified during the function call, for which the formal parameters are declared while defining the function.

functions and arguments in C


Example : Function that return some value

#include<stdio.h>
#include<conio.h>
int larger(int a,int b);    // function declaration

void main()
{
 int i,j,k;
 clrscr();
 i=99;
 j=112;
 k=larger(i,j);       // function call
 printf("%d",k);
 getch();
}

int larger(int a,int b)     // function definition
{
 if(a>b)
 return a;
 else
 return b;
}

Output:

112

While declaring the function, we have declared two parameters a and b of type int. Therefore, while calling that function, we need to pass two arguments. The two arguments passed should be received in function definition, and these received arguments are also known as formal parameters. The name of the variables while declaring, calling and defining function can be varied.


Nesting of Functions

C language also allows nesting of functions i.e to use one function inside another function's body. We must be careful while using nested functions, because it may lead to infinite nesting.

function1()
{
   function2() ;
   //statements
} 

If function2 calls function1 inside it, then in this case, it will lead to infinite nesting. They will keep calling each other and the program will never terminate. Hence we must be careful while nesting the functions.


Recursion

Recursion is a special way of nesting functions, where a function calls itself inside it. We must have certain condition to break out of the recursion, otherwise recursion occurs infinite times.

function1()
{
   function1() ;
   //statements
}

Example : Factorial of a number using Recursion

#include<stdio.h>
#include<conio.h>
int factorial(int x); //declaring the function

void main()
{
 int a,b;
 clrscr();
 printf("Enter no.");
 scanf("%d",&a);
 b=factorial(a); //calling the function named factorial
 printf("%d",b);
 getch();
}

int factorial(int x) //defining the function
{
 int r=1;
 if(x==1) return 1;
 else r=x*factorial(x-1); //recursion, since the function calls itself
 return r;
}