Functions
Functions
Function
When we classify our program into various functions, then our program becomes more readable and easier to understand.
Ques: Write a program in C++ to find the area of rectangle by using a function named as fun().
Solution:
void main()
{
fun(); //calling of function fun
}
void fun()
{
int l,b,area;
cin>>l;
cin>>b;
area = l*b;
cout<<area;
}
Output:
10
20
200
Passing arguments in function
The advantage of passing arguments or parameters in a function is that there is a communication of data from calling function to called function.
Ques: Write a program in C++ to find the area of rectangle by passing arguments in a function named as fun(int,int).
Solution:
void main()
{
{
fun(10,20); //10 and 20 are arguments of function fun
}
void fun(int l,int b)
{
int area;
area = l*b;
cout<<area;
}
Output:200
Comments
Post a Comment