Functions in C Programming Language

Functions in C Language – To make programming simple and easy to debug, we break a larger program into smaller subprograms which perform ‘well defined tasks’. These subprograms are called functions.

Functions are very important tools for Modular Programming, where we break large programs into small subprograms or modules (functions in case of C). The use of functions reduces complexity and makes programming simple and easy to understand.

Definition of a Function

A function is a self- contained block of executable code that can be called from any other function. In many programs, a set of statements are to be executed repeatedly at various places in the program and may with different sets of data, the idea of functions comes to mind.

You keep those repeating statements in a function and call them as and when required. When a function is called, the control transfers to the called function, which will be executed, and then transfers the control back to the calling function (to the statement following the function call).

Let us see an example as shown below

/* Program to illustrate a function*/

#include <stdio.h>

main ()

{

void sample( );

printf(“\n You are in main”);

}

void sample( )

{

printf(“\n You are in sample”);

}

 

OUTPUT

You are in sample
You are in main

Here we are calling a function sample ( ) through main( ) i.e. control of execution transfers from main( ) to sample( ) , which means main( ) is suspended for some time and sample( ) is executed.

After its execution the control returns back to main( ), at the statement following function call and the execution of main( ) is resumed.

The syntax of a function

return datatype function_name (list of arguments)
{

datatype declaration of the arguments;

executable statements;

return (expression);

}

where,

• return data type is the same as the data type of the variable that is returned by the function using the return statement.
• a function_name is formed in the same way as variable names/identifiers are formed.
• the list of arguments or parameters are valid variable names as shown below, separated by commas: (data type1 var1, data type2 var2,…….. data type n var n)

for example (int x, float y, char z).

• arguments give the values which are passed from the calling function.
• the body of the function contains executable statements.
• the return statement returns a single value to the calling function.

Let us write a simple function that calculates the square of an integer

/*Program to calculate the square of a given integer*/

/* square( ) function */

{

int square (int no)   /*passing of argument */

int result ;   /* local variable to function square */

result = no*no;

return (result); /* returns an integer value */

}

/*It will be called from main()as follows */

main( )

{

int n ,sq; /* local variable to function main */

printf (“Enter a number to calculate square value”);

scanf(“%d”,&amp;amp;amp;n);

sq=square(n); /* function call with parameter passing */

printf (”\nSquare of the number is : %d”, sq);

} /* program ends */

 

OUTPUT

Enter a number to calculate square value : 4
Square of the number is : 16

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: