Function In C Language
Types of functions
1) Predefined standard library functions
Such as
puts(),gets(),printf(),scanf(). These
are the functions which already have a definition in header files (.h files
like stdio.h), so we just call them whenever there is a need to use them.
2 )
User Defined functions
The functions that we create in a program are
known as user defined functions.
Why we need functions in C
Functions are used because of following reasons –
a) To improve the readability of code.
b) Improves the reusability of the code, same function can be used in any
program rather than writing the same code from scratch.
c) Debugging of the code would be easier if you use functions, as errors are
easy to be traced.
d) Reduces the size of the code, duplicate set of statements are replaced
by function calls.
Syntax of a function
return_type
function_name (argument list)
{
Set of
statements-block of code
}
Return_type:
Return type can be of any data type such as int, double,
char, void, short etc. Don’t worry you will understand these terms better once
you go through the examples below.
Function_name:
It can be anything, however it is advised to have a meaningful
name for the functions so that it would be easy to understand the purpose of
function just by seeing it’s name.
Argument list:
Argument list contains variables names along with their data
types. These arguments are kind of inputs for the function. For example – A
function which is used to add two integer variables, will be having two integer
argument.
Block of code:
Set of C statements, which will be executed whenever a call will
be made to the function.
Program:
#include<stdio.h>
#include<conio.h>
int add(int num1,int
num2)
{
int sum;
sum=num1+num2;
return sum;
}
int main()
{
No comments:
Post a Comment