c
C Programming Tutorial : Introduction

C Functions: What is Function In C?

A function is a self-contained subprogram that is meant to do some specific, well-defined 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. All C programs that are written using functions, enable us to improve reusability, understandability, and to keep track of them.

C has been designed to make functions efficient and easy to use; C programs generally consist of many small functions rather than a few big ones. A 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. We will not go into that process here, however, since the details vary from system to system.

A function can also be referred as a method or a sub-routine or a procedure, etc.

Advantages of Using Functions

  1. Generally, a difficult problem is divided into subproblems and then solved. This divides and conquers technique is implemented in C through functions. A program can be divided into functions, each of them will perform some specific task. So, the use of C functions modularizes and divides the war of a program.
  2. When some specific code is to be used more than once and at various places in the program the use of functions avoids repetition of that code.
  3. The program becomes easily understandable, modifiable, and easy to debug and test. It Became simple to write the program and understand what work is done by each part of the program
  4. Functions can be stored in a library and reusability can be achieved.

Types of functions-

C programs have two types of functions-

  1. Library functions.
  2. User-defined functions

Library functions.

library functions are also known as built-in functions. Functions such as puts(), gets(), printf(), scanf() etc are standard library functions. These functions are presents in the C library and they are predefined

User-defined functions

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

Defining a Function

The general form of a function definition in C programming language is as follows −

Syntax -1

Return-type function-name (parameter/argument list) 
{
   body of the function/ set of the statements;
…………………………
…………………………
}
 

A function definition in C programming consists of a function header and a function body. Here are all the parts of a function −

  • Return Type− The return type is the data type of the value the function returns to the function arguments. Some functions perform the desired operations without returning a value. In this case, the return type is the keyword void.
  • Function Name− This is the actual name of the function. The function name and the parameter list together constitute the function signature.
  • Parameters/ Arguments− Parameter list contains variables names along with their data types. These arguments are a kind of inputs for the function. Parameters are optional. A function may contain no parameters.
  • Function Body− The function body contains a set of statements that define what the function does.

Example -1

/*A simple program example for function.*/
#include <stdio.h>
#include <conio.h>

int addition(int num1, int num2)     /* declaration of arguments inside of bracket - int num1, int num2*/

{
     int sum;
     /* Arguments are used here*/
     sum = num1+num2;

     /* Function return type is integer so we are returning
      * an integer value, the sum of the passed numbers.
      */
     return sum;
}

int main()
{
     int var1, var2;
     printf("Enter number 1: ");
     scanf("%d",&var1);
     printf("Enter number 2: ");
     scanf("%d",&var2);

     /* Calling the function here, the function return type
      * is integer so we need an integer variable to hold the
      * returned value of this function.
      */
     int res = addition(var1, var2);
     printf ("Output: %d", res);

     return 0;
}