Hello
friends....!!!! welcome back ..!! We studied important topic of C language in
previous post which is pointer. Now I came with new post which name is functions.
Here we will discuss about basic knowledge in deep. So here we will start from definition.
So here we go.
FUNCTION:
Function is a collection of
statement known by particular name which performs some well defined task.
Types of
function:
1) User defined function
2) Library function
Functions
that are created by programmer is
called USER DEFINED FUNCTIONS. Functions which are already available in library are called as LIBRARY FUNCTIONS. Function
reduces the size of code, when it is used in program. Function called its self
is called recursion.
In user defined function, function
is defined by the programmer in the program. When the size of program is
increase, it produces difficulty to understand and maintain. When some part of
the code is repeated in the program, we can write that portion of the code as a
function. so result is function reduces the size of code and convert complex
program into easy form.
Library function is already
available inside of the library of C. They are divided into different categories
according to their uses. For example, for string process we use string.h header
file, for mathematical functions, we use math.h header file.
FUNCTION DECLARATION:
type
function name(argument); /*
syntax */
type means type of value returned. Function
name is name of user defined function. Argument means arguments
supplied to the function. If there are no arguments in the bracket then the
bracket left empty. In last function is terminated by semicolon (;) symbol.
FUNCTION DEFINITION:
type function name (arguments)
{
Statement;
}
In the
functions, there are three points to be noted. First is declaration, Second is call and
third is definition. let us learn
one by one.
In definition, function name, type, and arguments have same
meaning as declaration. But in this point one portion is added as the statement
with "{}" symbols. And main point is that semicolon (;) is not
allowed after arguments but it allows after
statement. Because function declaration is like user defined function so we
have to include semicolon (;) and function definition is like library function
so it is already in library so semicolon (;) is not necessary.
For
function call let see this program.
#include<stdio.h>
#include<conio.h>
void
printnumber() /* declaration */
main()
{
clrscr();
printnumber(); /* call */
getch();
}
void
printnumber() /* definition */
{
printf("
HELLO \n");
}
In
above program,
void printnumber();
is
declaration function. It tells the compiler that some where in the program
printnumber will be encountered.
void
printnumber()
{
printf(" Hello\n");
}
is
the definition function. When above function is called, message will be printed
on screen which is Hello.
printnumber(); is a call to the printnumber() function so the
statements in the body of function will be executed.
In above program, we added a declaration part.
But it is not necessary to write declaration part in all the program.
Declaration is not needed if the definition function is written above the call
of the function.
TYPES OF
FUNCTIONS BASED ON ITS SYNTAX:
- No arguments and no return value
- With arguments and with return value
- With arguments and no return value
Here we learnt no arguments and no
return value. When we add arguments, we need to know about formal variable and actual
variable before understanding 2nd and 3rd types.
“Variables which are used in the definition function are called formal
variables. Variable which are used in the call to the function are called as
actual variable.”
Following program is helpful to understand this topic.
In this program,
int sum(int x, int y);
is declaration of sum
function. Here x and y are formal variables because these are used in
declaration function. When a call to the function is made in main() function,
the actual parameters are passed, for example:
ans=sum(x,y);
this line calls the function ans, with variable x ,y. the variables x and
y work as actual variable.
we can not call the function as
sum(x,y); because the
function returns integer.
So guys, this is part
one of functions. Part two will be in my next post. I am sure that your concept
will be clear. Read this concept remember it and use this in the program. If there is any doubt, please feel free to mention it in the comment box.
If you are interested you may join our
facebook group new age informers.
Until
then bye bye...!!!!!
Comments
Post a Comment