Definition: A function can be a set of or group of some lines or a single line of code that are designed to perform any specific task.
Syntax:
Function
Declaration: or function prototype
Return_type
name_of_function(arguments);
void
sum(); //with no
return type and without argument
int
sum(int a,int b); //with return type
and with arguments
Note:
Return type may be void, int, float
or double as your programs requirement .
Name of function : as you wish you
can use any name to your function
Arguments : Are the parameter
values that are given to any function as input to it. As in above example of
int sum(int a,int b);
Function
Definition :
void sum() int
sum(int a,int b)
{ {
Int a,b,c; int
c;
a=5; c=a+b;
b=6; return
c;
c=a+b; }
cout<<”Sum=”<<c;
}
(A) (B)
Means defining the actual function of any function is called Function
Definition.
In above example (A) noting is given as input to function sum.
Where as in (B) the values of a and b
are given by the user of external program that call the function.
Function Call :
Calling any function means, using any function in your program.
(A)
Sum(); //for
function without return type
(B)
Variable=sum(a,b); //
for function with return type, variable to
//catch the value thrown by the return .
Types of
Function:
1.
Built in functions: Pre stored by the designers
of software package.
Ex.
pow(), sqrt() defined in math.h header
file.
2.
User Defined : Designed by any user.
They
are created by user for their own use.
Note: Actual
value
Actual values: Values given at the time of
function call are called actual values.
Example:
//Program
for function without return type and
without arguments
#include<iostream.h>
#include<conio.h>
void main()
{
void sum(); //Function Declaration
cout<<”Sum
of two number:”;
sum(); //Function
call
getch();
}
void sum() //Function
Definition
{
int a,b,c;
a=5;
b=6;
c=a+b;
cout<<c;
}
---------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
Example:
//Program
for function with return type and with
arguments
#include<iostream.h>
#include<conio.h>
void main()
{
int total,a,b;
int sum(int
,int ); //Function
declaration
cout<<”\nEnter
first number:”;
cin>>a;
cout<<”\nEnter
Second number:”;
cin>>b;
cout<<”Sum
of two number:”;
total=sum(a,b);
//Function
call
cout<<total;
getch();
}
int sum(int a,int b) //Function Definition
{
int c;
c=a+b;
return c;
}
-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
No comments:
Post a Comment