Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  


Shop
CPP All : Functions - user defined
Concept Learning Code Sheets

Function with single parameter to evaluate an expression #2179

Writing a function with one parameter to evaluate an expression.

#include
using namespace std;
int myexp(int);
int main()
{
int n;
cout<<"Enter an Integer : ";
cin>>n;
int ex=myexp(n);
cout<<"Value of expression x^3+x^2+x "
<<"with given value x="<

Area, circumference of circle using two functions #2182

Finding and area and circumference of a circle using two separate functions for each of them

#include
using namespace std;
double area(double);
double circum(double);
double const PI=3.1416;
int main()
{
double r;
cout<<"Give radius of circle :" ;
cin>>r;
cout<<"Area of circle = "< cout<<"Circumference of circle = "< return 0;
}

double area(double rad)
{
return PI*rad*rad;
}

double circum(double rad)
{
return 2*PI*rad;
}

Sum of digits program using a function #2189

Writing function sumofdigits() to add the digits in any integer number and then using this function in the main function.

#include
using namespace std;
int sumofdigits(int);
int main()
{
int n,s;
cout<<"Input a number : " ;
cin>>n;
s=sumofdigits(n);
cout <<"Sum of Digits of "< return 0;
}

int sumofdigits(int n)
{
int s=0,rem=0;
while(n>0)
{
rem=n%10;
s+=rem;
n=n/10;
}
return s;
}

Repeated display function without any returned value – void type #2214

Function to repeat a character in display for number of times desired.

#include
using namespace std;
void repeat(char,int);
int main()
{
char c='*';
int rc=15;
repeat(c,rc);
return 0;
}

void repeat(char c,int rc)
{
for(int i=0;i cout< }

Setting default parameters in function prototype #2243

The default parameters in function becomes active when given parameter value is not passed. Here we learn this setting at the level of function prototype.

#include
using namespace std;
void repeat(char = '#',int = 20);
int main()
{
char c='@';
int rc=10;
repeat(c);cout< repeat(c,rc);cout< repeat();
return 0;
}
void repeat(char a,int rc)
{
for(int i=0;i }

Setting default parameters in function definition #2250

The default parameters in function becomes active when given parameter value is not passed. Here we learn this setting at the level of function definition.

#include
using namespace std;
void repeat(char a='#',int rc=20)
{
for(int i=0;i }

int main()
{
char c='@';
int rc=10;
repeat(c);cout< repeat(c,rc);cout< repeat();
return 0;
}

Using constant argument/parameter in a function #2255

Writing a program to find student division using the const argument for maximum marks.

#include
using namespace std;
int division(const int,int);
int main()
{
int MM=50;
int marks;
cout<<"Enter marks out of "< cin>>marks;
int div=division(MM,marks);
cout<<"Student division = ";
switch(div)
{
case 1:cout<<"First"; break;
case 2:cout<<"Second"; break;
case 3:cout<<"Third"; break;
case 0:cout<<"Did not Qualify";
}
return 0;
}
int division(const int mm,int m)
{
float p=(float (m)/mm)*100;
if(p >=60) return 1;
else if(p >=45) return 2;
else if(p >=33) return 3;
else return 0;
}

Function polymorphism/overloading based on parameter count #2258

Writing polymorphic function based on difference on number of parameters.

#include
using namespace std;
void repeat(char,int);
void repeat(char);
int main()
{
char c='@';
int rc=10;
repeat(c);
cout< repeat(c,rc);
return 0;
}
void repeat(char a)
{
for(int i=0;i<20;++i) cout< }
void repeat(char a,int rc)
{
for(int i=0;i }

Function polymorphism/overloading based on parameter data type #2263

Writing polymorphic function based on difference on parameters data type.

#include
using namespace std;
void add(int);
void add(char);
int main()
{
char c='#';
int n=10;
add(c);
add(n);
return 0;
}
void add(char a)
{
for(int i=0;i<10;++i) cout< cout< }
void add(int n)
{
int sum=0;
for(int i=1;i<=n;++i) sum+=i;
cout< }

Function in function calls using salary preparation example #2375

Using the example of salary preparation for building related functions and using function in function calls.

#include
using namespace std;
int da=30,hra=12,pf=10,tax=15;
float sal2give(int);
float salary(int);
float amt(int,int);
int main()
{
int basic;
do{
cout<<"Give Basic :";cin>>basic;
cout<<"Salary to give ="< }while(basic != 0);
return 0;
}

float sal2give(int basic)
{
float s2g=salary(basic)-amt(basic,pf)-amt(basic,tax);
return s2g;
}

float salary(int basic)
{
float sal=basic+amt(basic,da)+amt(basic,hra);
return sal;
}

float amt(int b,int p)
{
return b*(float (p)/100);
}

Multiple return paths in isPrime() function for prime number series #2449

Using a bool output function isPrime() which has multiple return paths for prime number check and then using this function to print a prime series for given number of terms

#include
using namespace std;
bool isPrime(int n)
{
if(n<2) return false;
for(int i=2;i<=n/2;i++)
if(n%i==0) return false;
return true;
}
int main( )
{
int no_of_terms=100;
int term=2;
for(int tc=1;tc<=no_of_terms;++tc)
{
while(!isPrime(term)) term++;
cout< }
return 0 ;
}
Solved Problems

Simple interest program with function. #2386

Write a c++ program to define a function si(p,r,t) with input parameters as p

Simple Interest function with default parameter values. #2661

Write output of the following program. Please do take care about the default

Trailing default parameters check #2666

A c++ program given here has some errors. Please correct the errors to make i

Function to return largest of three numbers along with all same check #2951

Write a function to return the largest of three integer numbers passed to it

Function overloading for area functions #2990

Write a c++ program using function overloading using the following prototype

Test palindrome for an integer number #3082

Write a program where a function of prototype int ispalin(int) is written, wh

function to reverse an integer #3086

Write a program to reverse the given integer number.using a separately called

Error finding in function and related code #4447

Find syntactical errors in the following program and rewrite the program afte

Quizzes

Code Sheets:11  Solved Problems:8  Quizzes:2