C Functions

A function is a self-contained block of program that performs some specific, well-defined task. A C program consists of one or more functions rather than one large main() function. printf() and scanf() are two predefined functions that we have used so far.

Functions break large complicated computing tasks into smaller and simpler ones. Separating a program into functions also aids in the maintenance and enhancement of programs by localizing the effects of changes. A C program may reside in one or more source files. Source files may be compiled separately and loaded together, along with previously compiled functions from libraries. This helps programmers to build on the existing functions by creating their own functions and tying them to the existing library.

There are basically two types of functions.

  • Library functions
  • User-defined functions

The commonly required functions written, compiled and placed in libraries are called as “Library Functions”. Some examples of library functions are printf() and scanf() etc.

The functions written by the user are termed as “User Defined Functions”. In user-defined functions, user has freedom to choose the function name, return data type and the arguments (number and type). There is no conceptual difference between the user defined and library function. The method of calling both the functions is same.

Advantages of a function sub-program (Why we need function)

As a Function is a self-contained module of some c statements having a specific objective. But the declaration and use of function depend upon its need. So due to a number of features in it, it becomes more important than the main program to solve some complex problems. Following are some advantages of functions:

  • Function makes the lengthly and complex program easy and in short forms. It means large program can be sub-divided into self-contained and convenient small modules having unique name.
  • The length of source program can be reduced by using function by using it at different places in the program accroding to the user’s requirement.
  • By using function, memory space can be properly utilized. Also less memory is required to run program if funciton is used.
  • A function can be used by many programs.
  • Function increases the execution speed of the program and makes the programming simple.
  • By using function, portability of the program is very easy.
  • Debugging (removing error) becomes very eaiser and fast using the function sub-programming.
  • It removes the redundancy i.e. avoids the repetition and saves the time and space.
  • Functions are more flexible than library functions.
  • Testing (verification and validation) is very easy by using functions.

Example Program:

/*Program to print a line of text by using the function*/
#include <stdio.h>
#include <conio.h>
void main()
{
  clrscr();
  fun();
  printf("\n I am a Good Boy \n");
  fun();   getch();
}

fun()
{
  return;
}

Output is as follows:

I am a Good Boy

Return Statement

A return statement is the last statement of a function. But in some cases, it can be used anywhere within the subprogram. When the return statement executes, the control moves to the calling statement. Whether a function sends back any value to the calling function or not, there must be the use of return statement with or without a return value. The main purpose of the return function is to communicate the result of the operation of the called function to the place where the call is made. The general syntax and the procedure used for the return statement is:

return;

or

return (expression);

or

return (variable);

Categories of Functions

Sometimes function program takes values from the main program and sometimes it does not. Similarly function sub-program sometimes-return values and sometimes it does not, return any value to the main program. we categorize the function subprogram in four sections:

  • Function with no argument and no return value.
  • Function with argument and no return value.
  • Function with no argument and return value.
  • Function with argument and return value.

Function with no argument and no return value

In this category of the function sub-program, main program will not send any argument to the function and also function subprogram will not send (return) any value to the main program. For example, the program procedure to display the processing of the function with no argument and no return value is as:

/*Function with no argument and no return value*/
#include <stdio.h>
#include <conio.h>
void main()
{
  clrscr();
  Sum();
  Sum();   getch();
}

Sum()
{
    int a,b,c;
    printf("\n Enter the Two No. for Sum :\n");
    scanf("%d%d",&a,&b);
    c= a +b;
    printf("Sum=%d", c);
}

Output is as follows:

Enter the Two No. for Sum :
10
10
Sum= 20

Function with argument and no return value

In this category of the function sub-program, main program or the calling program will send argument value(s), but called program or the function subprogram will not return any value. For example, the program procedure to display the processing of the function with argument and no return value is as:

/*Function with argument and no return value*/
#include <stdio.h>
#include <conio.h>
void main()
{
  int a,b;
  clrscr();
  Sum(int,int);
  printf("\n Enter the Two No. for Sum :\n");
  scanf("%d%d",&a,&b);
  Sum(a,b);   getch();
}

Sum(int x,int y)
{
    int sum;
    sum= x + y;
    printf("Sum=%d", sum);
}

Output is as follows:

Enter the Two No. for Sum :
10
10
Sum= 20

Function with no argument and return value

In this category of the function sub-program, main program or the calling program will not send argument value(s), but called program or the function subprogram will return any value. For example, the program procedure to display the processing of the Function with no argument and return value is as:

/*Function with no argument and return value*/
#include <stdio.h>
#include <conio.h>
void main()
{
  int c;
  int Sum();
  clrscr();
  c = Sum();     printf("Sum=%d", c);
  getch();
}

int Sum()
{
   int a,b,s;   printf("\n Enter the Two No. for Sum :\n");
  scanf("%d%d",&a,&b);
   s = c + b;
   return (s);
}

Output is as follows:

Enter the Two No. for Sum :
10
10
Sum= 20

Function with argument and return value

In this category of the function sub-program, main program or the calling program will send argument value(s), but called program or the function subprogram will return any value. For example, the program procedure to display the processing of the Function with argument and return value is as:

/*Function with argument and return value*/
#include <stdio.h>
#include <conio.h>
void main()
{
  int a,b,c;
  int Sum(int,int);
  clrscr();
  c = Sum(a,b);     printf("Sum=%d", c);
  getch();
}

int Sum(int x,int y)
{
   int z;    s = x + y;
   return (z);
}

Output is as follows:

Enter the Two No. for Sum :
10
10
Sum= 20

Recursion

When a called function, in turn, calls another function, then a process of ‘chaining’ occurs. Recursion is a special case of this process or chain, So when a function calls itself, then it is called recursion. This chain continues till a specific condition is met. If it has not any stop conditions, then it will create an indefinite loop. Recursion is also called a self-reference loop.

Recursion is used to solve problems that can’t be solved by the iterative procedure for while and do loops. It is useful to solve repetitive problems, where the input of one sub-program can be used as starting value having the previous output.

Example Program:

/*Recursion Function*/
#include <stdio.h>
#include <conio.h>
void main()
{
  int n,r;
  clrscr();
  int fact(int n);
  printf("Enter the No.");
  scanf("%d",&n);
  r = fact(n);
  printf("%d",r);
  getch();
}

int fact(int n)
{
  int f;
  if(n==1)
    return (1);
  else
    f = n * fact (n-1);
  return(f);
}

Void Statement

When a void statement is used in function program, then it returns nothing. In other words, when we want to return no value to the calling program, then void function or void statement is used. The general syntax is as:

void function-name();    /*during function declaration*/

or

void function-name();     /*during function in use*/
Related Post