Introduction to Functions in C Programming Language

A function is a block of code that performs a specific task. It has a name and it is reusable. it can be executed from as many different parts in a Program as required, it can also return a value to the calling program. All executable code resides within a function. It takes in input, does something with it, then gives the answer. A c program consists of one or more functions.

A computer program cannot handle all the tasks by itself. It requests other programs like entities called functions in C. We pass information to the function called arguments which specified when the function is called. A function either can return a value or returns nothing.

A function is a subprogram that helps to reduce coding.

A Simple Example Of Function In C

#include<stdio.h>
#include <conio.h>
int adition (int, int); //Function Declaration

main()
{
int z;
z= addion(10,3); //Function Call 
printf ("The Result Is", %z);
}

int addition (int a, int b) //Function Definition
{
int r;
r=a + b;
return (r);
}

In the above example, the addition function is being called with two arguments and returning the addition to the main function.

Types or categories of Function

we can categorize functions into two library functions and user-defined functions. But function can further be categorized into the following types.

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

Library Function In C

C  provides library functions for performing some operations. These functions are present in the c library and they  are predefined for example. Sqrt() is a mathematical library function  which is used for finding the square root of any number The function scanf and printf() are input and output library function  similarly we have strcmp() and strlen() for string manipulations. Except it there are several library functions like getch(), getche(), getchar(), putch(), putchar(), gets(), puts(), clrscr(), strcat(), strcpy(), exit(), fflush() ,etc.

To use a library function we have to include some header files using the preprocessor directive #include. for example: to use input and output functions like printf() and scanf() we have to include stdio.h, for math library function we have to include math.h for string library string.h should be included.

User-Defined Function In C

A user can create their own functions for performing any specific task of the program. These types are called user-defined functions. to create and use these functions we have to know these 3 things.

  1. Function Declaration
  2. Function Definition
  3. Function Call

Function Definition

The function definition consists of the whole description and code of a function. It tells that what the function is doing and what are the input-output for that.  A function is called by simply writing the name of the function followed by the argument list inside the parenthesis.
A function definition has two parts :

  1. Function Header
  2. Function Body

Structure Of A Function In C

There are two main parts of the function. The function header and the function body.

int sum(int x, int y)
{
int ans = 0; //holds the answer that will be returned
ans = x + y; //calculate the sum
return (ans); //return the answer
}

Function Header

The first line of the above code is called Function Header.

int sum( int x, int y)

It has three parts

  1. The name of the function i.e. sum
  2. The parameters of the function enclosed in parenthesis
  3. Return value type i.e. int

Function Body

Whatever is written within { } in the above example is the body of the function.

Some Properties Of Functions

  1. Every function has a unique name. This name is used to call function from “main()” function.
  2. A function performs a specific task.
  3. A function returns a value to the calling program.

Advantages Of Using Functions In C

  1. Functions has top down programming model. In this style of programming, the high level logic of the overall problem is solved first while the details of each lower level functions is solved later.
  2. A C programmer can use function written by others
  3. Debugging is easier in function
  4. It is easier to understand the logic involved in the program
  5. Testing is easier

Why We Use Function

  1. Writing function avoids  rewriting the same code  over and over. Suppose we have a section of code that is calculating the area of triangle, if later we want to calculate the area of other triangle then we don’t need to write the code again.
  2. By using function it becomes easier to write programs in c language.

Function Declaration and Function Prototypes

C always needs declaration before use. This is also true for functions. For functions, the declaration needs to be before the first call of the function. A full declaration includes the return type and the number and type of the arguments. This is also called the function prototype.

Some other types or categories of functions:

Function without argument and without a return value.

For example: Below is a function without argument and without return values. It means it has no argument and no value is being returned by it.

Add()
{
	Int A,B,C;
	A=20;
	B=30;
	C=A+B;
}
  1. Function without argument and with the return value.

For example: Below is a function without argument but the value of C is being returned by it.

Add( )
{
	Int A,B, C;
	B=10;
	A=20;
	C=A+B;
	Return(C);
}

Function with argument and no return value.

For example: Below is a function with arguments A, and B but no value is being returned by it.

Add( int A, int B)
{
	Int  C;
	C=A+B;
	
}

Function with argument and return value.

For example: Below is a function with arguments A, and B and the value of C is being returned by it.

Add( int A, int B)
{
	Int  C;
	C=A+B;
	Return(C);
}