functions in c

Functions in c

A function is a self container or subprogram that perform some specific task. C programs consists of one or more then one functions. Everything will be happen inside the function in c programming.
If a program has only a function that is main() function. The main() function first executes when the program starts, and all other function called directly or indirectly from the main() function.

Advantage of using Function

1. There generally difficult problem is divided into sub problems and solve. The c program is divided into number of functions and each function performs specific task that means the program or work divide in modules. 2. And when the specific code is used more then once and use at different places we use functions. 3. The program becomes easy understandable, modifiable and the easy to debug and testing. it becomes easy to write and understand what function or parts of program do work. 4 .The function can be stored in library or in header file and reusable.

The C programs have to type of functions

1. Library functions
2. User-defined functions

Library functions

C has the facility to provides library function to performing operations.The libraries functions are present in c libraries and they are predefined. for example printf() is library function, its is used from print any string in the console which is stored in < stdio.h > header file. scanf () are used for input value from the user it is also stored in stdio.h headerfile. similar we have functions strlen() and strcmp() for string manipulations. To use any library functions we have to include corresponding headerfile using the preprocessor directive that is #include. if we want to use printf() and scanf() library function, we will include stdio.h

let us take example to find the square root of number using sqrt() function

/*find the square root of number using sqrt() function*/
#include<stdio.h>
#include<math.h>

int main()
{
double num,s;
printf("Enter a number to find square root");
scanf("%lf",&num);
s=sqrt(num);
printf("Square root is = %2lf",s);
return 0;
}

User defined functions

The programmers can create or make their own function for doing any specific task of the program. These functions are called or known as user defined functions.So to create user defined function we should know three things .

1. function declartion
2. function defination
3. function calling

take the simple example of function which includes function declaration, function definition and function calling after this, discuss the all three parts of function

/* program to print the table using function*/
#include<stdio.h>
void print_table(); /*function declartion*/
int main()
{
print_table(); /*function calling*/
return 0;
}
void print_table() /*function defination*/
{
int num,i,tab;
printf("Enter the number");
scanf("%d",&num);
for(i=1;i<=10;i++)
{
tab=num*i;
printf("\n%d",tab);
}
}

Function declaration

A function declaration is used to give the information about function to the compiler. so this can check the function calling so the function calling needs the information about the called function. but if definition of the function is given before the calling function then there is not need to declare the function.

note: if we write the definition of any function before the main function then no need to declare that function.

/* program to find the difference of two numbers */
#include<stdio.h>
void
subtract(int a, int b)
{
int sub;
sub=a-b;
printf("The difference of %d and %d=%d",a,b,sub);
}
int main()
{
int x,y;
printf("Enter the two number to subtraction");
scanf("%d%d",&x,&y);
subtract(x,y);
return 0;
}

Here the definition of sub() function is written before the main(), so main() knows everything of the function sub(). but at all time generally main() is placed at the top of the program and other functions are placed after it then in this case function declaration is required. when declare the function that informs the compiler about the following three things: –

1. Name of the function
2. Number of arguments and types of arguments received by the function and with their order
3. Type of value return by the function

SR No. C function Syntax
1 Declartion Return_type function_name(Arguments_list)
2 function Calling function_name(Arguments_list)
3 function definition Return_type function_name(Arguments_list){ function_body}