Function
Function is
a program with group of statements, which performs specific operations. the function
main() is special and important function in c,c++,java.
The basic
advantage of function is to reduce the size of program by calling and using them
at different position.
Syntax :
Main()
{
//void show();
Show() //function call
return 0;
}
void Show()
{
------------Function
body
**********
}
When function
is called control transfer to first statement in the function definition.
The other statements
in function definition are executed and control returns to the main program.
For writing
a function we need
A function declaration
A function
call.
A function definition.
What is function declaration ?
This refer
to function prototyping before using function it has to be declare, It is same
as declaring a variables.
A function
may be declare in int main() function or
in class.
Syntax: return type function name(argument list)
The function
prototype gives details to the compiler as number and type of arguments, return
type ,function name.
When function
is call compiler ensure that proper argument are passed and return value is
treated as correctly.
There is
major difference between prototyping between C and C++ while C++ makes the
prototyping essential ANSIC makes its optional.
What is function call ?
We can call
function directly or indirectly.
We have to
pass actual arguments to the function
The argument
which we pass should match with formal parameter which are declare into
function.
The data
type and order should also be match.
Syntax ::
function name (actual argument list)
Area(10,5);
What is function definition ?
The function
definition contains actual code of programs.
Function definition
consist of 2 parts.
i)
Function
Header
ii)
Function
body
Syntax ::
Return function
name (formal parameter)
{
-
-
- -
-
-
- - -
}
Program factorial
of number
#include<iostream>
int main()
{
Void fact (void)
fact ();
return 0;
}
Void fact()
{
int i ,num,f=1;
cout<<”Enter
the number : ”;
cin>>num;
for(i=1;i<=num;i++)
{
f = f * i ;
}
Cout<<”Factorial
is : ”<< f ;
}
Comments
Post a Comment