Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
Problem Statement - Simple Interest function with default parameter values.
Write output of the following program. Please do take care about the default parameters given while calling the function.
TC++ #2661
Solution
Run Output
600
500
1000
3000
float interest(float p,int t=1,float r= 0.10);
This function declaration will set default time t to 1 and rate r to 0.10 (10%). Principal amount will be taken from the value passed to the function.
float x=1000,z=0.20; int y=3; cout<<interest(x,y,z)<<endl;
Here all the parameters shall be passed from values initialised before it. Output as per calculation given will be 600.
cout<<interest(5000)<<endl;
Here principal has been passed as 5000 but t and r are skipped so they will come from the default value as per declaration which is t=1 and r=0.10. So the calculation will give 500.
cout<<interest(5000,2)<<endl;
Here principal amount is 5000, t is passed as 2 years but rate will be default 0.10. So calculation will give 1000.
cout<<interest(5000,2,0.3)<<endl;
Here all the values have been passed, thus default values will not come into picture. The calculation as per actual parameters will be 3000.
float interest(float p,int t,float r) {return (p*r*t);}
The function simply return the multiplication of pr ant t.This is basically the value of simple interest.