Functions:
>>A function is
self-contained block of code that performs a particular task.
>>A function is a block
of statement that has a name and it has a property that it is reusable i.e. it
can be executed from as many different points in a C Program as required.
>>A Function is an
independent, reusable module of statements, that specified by a name.
This module (sub program) can be called by its name to do a specific task.
>>We can call the
function, for any number of times and from anywhere in the program.
>>A function can receive
many values but it can return only one value.
‘C’ functions can be
classified into two categories, namely library functions (pre-defined) and user
defined functions. Printf and scanf belong to the category of library
functions.
Standard C-Library Functions
<math.h>
abs(d) –Returns the absolute value of d.
asin(d)—Returns the sin of d.
pow(x,y)—Returns the x to the power y.
sqrt(d)—Returns the square root of d.
C language is
collection of various inbuilt functions. If we wrote a program in C then it is
evident that we have used C’s inbuilt functions. printf, scanf, clrscr etc. all
are C’s inbuilt functions.
Structure of a Function
A general form of a C function is:
<return type> FunctionName (Argument1, Argument2, Argument3……)
{
Statement1;
Statement2;
Statement3;
}
An example of function.
int sum (int x, int y)
{
int result;
result = x + y;
return (result);
}
int sum (int x, int y)
{
int result;
result = x + y;
return (result);
}
Advantages of using
functions:
There are many advantages in
using functions in a program they are:
- It makes possible top down modular programming. 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 addressed later.
- The length of the source program can be reduced by using functions at appropriate places.
- It becomes uncomplicated to locate and separate a faulty function for further study.
- A function may be used later by many other programs this means that a c programmer can use function written by others, instead of starting over from scratch.
- A function can be used to keep away from rewriting the same block of codes which we are going use two or more locations in a program. This is especially useful if the code involved is long or complicated.
Types of user defined functions:
A function may belong to any one
of the following categories:
- Functions with no arguments and no return values.
- Functions with arguments and no return values.
- Functions with arguments and return values.
- Functions that return multiple values.
Example of a simple function to add two integers.
Output
of the program:
1. Functions with no arguments and no return value.
In this function without any arguments means we cannot pass
data (values like int, char etc) to the called function. Similarly, function
with no return type does not pass back data to the calling function. It is one
of the simplest types of function in C. This type of function which does not
return any value cannot be used in an expression it can be used only as
independent statement.
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Welcome to function
in C");
printline();
printf("Function easy to
learn.");
printline();
getch();
}
void printline()
{
int i;
printf("\n");
for(i=0;i<30;i++)
{
printf("-");
}
printf("\n");
}
2.
Functions with arguments and no return value.
This type of function can accept data from calling function.
In other words, we send data to the called function from calling function but we
cannot send result data back to the calling function. It displays the result on
the terminal. But we can control the output of function by providing various
values as arguments.
|
#include<conio.h>
void add(int x, int y)
{
int result;
result = x+y;
printf("Sum of %d and %d is
%d.\n\n",x,y,result);
}
3.
Functions with arguments and return value.
This type of function can send arguments
(data) from the calling function to the called function and wait for the result
to be returned back from the called function back to the calling function. And
this type of function is mostly used in programming world because it can do two
way communications; it can accept data as arguments as well as can send back
data as return value. The data returned by the function can be used later in
our program for further calculations.
|
#include<conio.h>
void main()
{
int z;
clrscr();
z = add(952,321);
printf("Result
%d.\n\n",add(30,55));
printf("Result
%d.\n\n",z);
getch();
}
4.
Functions with no arguments but returns value.
We may need a
function which does not take any argument but only returns values to the
calling function then this type of function is useful. The best example of this
type of function is “getchar()” library function which is declared in the
header file “stdio.h”. We can declare a similar library function of own.
Example:
#include<stdio.h>
#include<conio.h>
int send( )
{
int n;
printf("Enter a no : ");
scanf("%d",&n);
return(n);
}
void main( )
{
int z;
clrscr( );
z = send( );
printf("\nYou entered :
%d.", z);
getch( );
}