Friday, January 24, 2014

C++ Program to Find The Sum for the Given Variables using Function with Default Arguments

with 0 Comment
A FUNCTION WITH DEFAULT ARGUMENTS
AIM:
  • To write a C++ program to find the sum for the given variables using function with default arguments.
ALGORITHM:
1) Start the program.
2) Declare the variables and functions.
3) Give the values for two arguments in the function declaration itself.
4) Call function sum() with three values such that it takes one default arguments.
5) Call function sum() with two values such that it takes two default arguments.
6) Call function sum() with one values such that it takes three default arguments
5) Inside the function sum(), calculate the total.
6) Return the value to the main() function.
7) Display the result.
8) Stop the program.

SOURCE CODE:

#include<iostream.h>
#include<conio.h>
void main()
{
float sum(float a,int b=10,int c=15,int d=20);
int a=2,b=3,c=4,d=5;
clrscr();
cout<<"\nsum="<<sum(0);
cout<<"\nsum="<<sum(a,b,c,d);
cout<<"\nsum="<<sum(a,b,c);
cout<<"\nsum="<<sum(a,b);
cout<<"\nsum="<<sum(a);
cout<<"\nsum="<<sum(b,c,d);
getch();
}
float sum(float i, int j, int k, int l)
{
return(i+j+k+l);
}

Output:
sum=45
sum=14
sum=29
sum=40
sum=47
sum=32

RESULT:
  • Thus, the given program for function with default arguments has been written and executed successfully.

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive