6.Functions
06 Sep 2019 | C++
Example 1
#include <iostream>
using namespace std;
/*functions / methods */
//parameter
void welcome(); //declaration of function, procedure
bool isNumber(string);
void enterName();
double add(double a, double b) {return a + b;}
void changeValueTo10(int a)
{
a = 10;
}
int main()
{
// welcome();
/* enterName();
enterName();
enterName();*/
char ch;
cout << "Do you want to end the program? (Y/N)" << endl;
cin >> ch;
if (ch == 'Y' || ch == 'y')
return 0;
// cout << add(4,5);
int a = 5;
changeValueTo10(a);
cout << a;
return 0;
}
void welcome()
{
cout << "HEllo, welcome in my program!! :-)" << endl;
}
bool isNumber(string tmp)
{
if (tmp[0] == '0')
return false;
for (int i = 0; i < tmp.length(); i++)
{
if (!(tmp[i] >= 48 && tmp[i] <= 57))
return false;
}
return true;
}
void enterName()
{
string tmp;
cout << "Enter the number: " << endl;
cin >> tmp; //124
if (isNumber(tmp))
cout << "Number entered properly " << endl;
else
cout << "Number wasn't entered properly " << endl;
}
Example 2
#include <iostream>
#define PI 3.14
using namespace std;
// the belows are declaration of function
void initMenu();
void menuDecision(int);
double areaCircle(double);
double areaSquare(double);
double areaRectangle(double, double);
double areaTriangle(double, double);
int main()
{
int choice;
char cont;
do
{
system("cls"); //clear screen = cls
initMenu();
cin >> choice;
menuDecision(choice);
do
{
cout << "Do you want to continue the program? (Y/N)" << endl;
cin >> cont;
} while(cont != 'y' && cont != 'Y' && cont != 'N' && cont != 'n');
} while(cont == 'y' || cont == 'Y');
return 0;
}
void initMenu()
{
cout << "Enter option:" << endl;
cout << "1. Circle" << endl;
cout << "2. Square" << endl;
cout << "3. Rectangle" << endl;
cout << "4. Triangle" << endl;
}
void menuDecision(int choice)
{
double r, a, b, h;
switch(choice)
{
case 1:
cout << "Enter the radius: " << endl;
cin >> r;
areaCircle(r);
break;
case 2:
cout << "Enter the side of a square: " << endl;
cin >> a;
areaSquare(a);
break;
case 3:
cout << "Enter the width and height of a rectangle: " << endl;
cin >> a >> b;
areaRectangle(a, b);
break;
case 4:
cout << "Enter the base and height of a triangle: " << endl;
cin >> a >> h;
areaTriangle(a, h);
break;
default:
cout << "You didn't choose any of the option from above" << endl;
}
}
double areaCircle(double r)
{
double result = PI * r * r;
cout << "The area of a circle that radius is " << r << " = " << result << endl;
return result;
}
double areaSquare(double a)
{
double result = a * a;
cout << "The area of a square that side is " << a << " = " << result << endl;
return result;
}
double areaRectangle(double a, double b)
{
double result = a * b;
cout << "The area of a rectangle that first side is " << a << " the second side is " << b << " = " << result << endl;
return result;
}
double areaTriangle(double a, double h)
{
double result = (1/2.0) * a * h;
cout << "The area of a rectangle that first side is " << a << " the second side is " << h << " = " << result << endl;
return result;
}
Example 3
#include <iostream>
using namespace std;
/* overloading functions */
int power(int, int);
double power(double, int);
int main()
{
cout << power(2, 6) << endl;
return 0;
}
/*
2 ^ 3 = 2 * 2 * 2
2 ^ 1 = 2
2 ^ 2 = 4
2 ^ 3 = 8
*/
int power(int b, int e) // b = 8, e = 1
{
int tmp = b; //tmp = 2
int i = 0;
while(i++ < e)
{
cout << tmp << " ^ " << i << " = " << b << endl;
if (i != e)
b *= tmp; //b = b * tmp;
}
return b;
}
double power(double b, int e)
{
double tmp = b; //tmp = 2
while(e-- > 1)
b *= tmp; //b = b * tmp;
return b;
}
Example 4
#include <iostream>
using namespace std;
/*scope / range of varables */
int globalVariable;
main()
{
int localVariable;
/*
cout << "value of globalVariable: " << globalVariable << endl;
cout << "value of localVariable: " << localVariable << endl;
*/
int a = 10;
/*
if (a == 10)
{
int result = a * 10;
}
cout << result << endl;*/
int nr, result = 0;
int i = 0;
for (; i < 3; i++) // 3,2,3 - will the result be 8??? NOOOOOOOOO, we have to assign 0 to the result!
{
cout << "Enter " << (i + 1) << " number" << endl;
cin >> nr;
result += nr; //result = result + nr;
}
cout << result << endl;
cout << "we added " << i << " numbers" << endl;
}
Example 5
#include <iostream>
#include <cstdlib>
using namespace std;
double minValue(double tab[]);
double maxValue(double tab[]);
int main ()
{
double tab[5];
for (int i = 0; i < 5; i++)
{
cout << "Input " << i+1 << " number: ";
cin >> tab[i];
}
cout << "The minimum value was: " << minValue(tab) << endl;
cout << "The maximum value was: " << maxValue(tab) << endl;
return 0;
}
double minValue(double tab[])
{
double minValue = tab[0];
for (int i = 1; i < 5; i++)
{
if (minValue > tab[i])
minValue = tab[i];
}
return minValue;
}
double maxValue(double tab[])
{
double maxValue = tab[0];
for (int i = 1; i < 5; i++)
{
if (maxValue < tab[i])
maxValue = tab[i];
}
return maxValue;
}
Example 6
#include <iostream>
#include <limits> // to make "cin.ignore(numeric_limits<streamsize>::max(), '\n');"r active
#define PI 3.14
using namespace std;
/*validating data
buffer - temporary array
*/
void initMenu();
void menuDecision(int);
double areaCircle(double);
double areaSquare(double);
double areaRectangle(double, double);
double areaTriangle(double, double);
bool isValid(string);
bool isValid();
int main()
{
int choice;
char cont;
do
{
system("cls"); //clear screen = cls
initMenu();
while(!(cin >> choice))
{
//cout << "state before: " << cin.rdstate() << endl;
cin.clear();
//cout << "state after: " << cin.rdstate() << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // skip the '\n' charcter left in the input buffer
// 정수를 입력받은 후에 문자열을 입력받지 않고 다음 코드로 넘어간다. 버퍼에 정수값을 누른 엔터가 남아있어 그렇다. getline을 이용하기 위해서는 정수값을 입력받은뒤 ignore을 사용한다.
system("cls");
initMenu();
cout << "You've just typed the wrong data to the input. " << endl;
}
menuDecision(choice);
do
{
cout << "Do you want to continue the program? (Y/N)" << endl;
cin >> cont; //asdfg
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} while(cont != 'y' && cont != 'Y' && cont != 'N' && cont != 'n');
} while(cont == 'y' || cont == 'Y');
return 0;
}
void initMenu()
{
cout << "Enter option:" << endl;
cout << "1. Circle" << endl;
cout << "2. Square" << endl;
cout << "3. Rectangle" << endl;
cout << "4. Triangle" << endl;
}
void menuDecision(int choice)
{
double r, a, b, h;
switch(choice)
{
case 1:
do { cout << "Enter the radius: " << endl; cin >> r; } while(!isValid());
areaCircle(r);
break;
case 2:
cout << "Enter the side of a square: " << endl;
do { cin >> a; } while(!isValid("The data is wrong, please type it again:"));
areaSquare(a);
break;
case 3:
cout << "Enter the width and height of a rectangle: " << endl;
do { cin >> a >> b; } while(!isValid("The data is wrong, please type it again:"));
areaRectangle(a, b);
break;
case 4:
cout << "Enter the base and height of a triangle: " << endl;
do { cin >> a >> h; } while(!isValid("The data is wrong, please type it again:"));
areaTriangle(a, h);
break;
default:
cout << "You didn't choose any of the option from above" << endl;
}
}
double areaCircle(double r)
{
double result = PI * r * r;
cout << "The area of a circle that radius is " << r << " = " << result << endl;
return result;
}
double areaSquare(double a)
{
double result = a * a;
cout << "The area of a square that side is " << a << " = " << result << endl;
return result;
}
double areaRectangle(double a, double b)
{
double result = a * b;
cout << "The area of a rectangle that first side is " << a << " the second side is " << b << " = " << result << endl;
return result;
}
double areaTriangle(double a, double h)
{
double result = (1/2.0) * a * h;
cout << "The area of a rectangle that first side is " << a << " the second side is " << h << " = " << result << endl;
return result;
}
bool isValid(string error_msg)
{
if (cin.rdstate()) //state is wrong when it is not equal to 0
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("cls");
initMenu();
cout << error_msg << endl;
return false;
}
return true;
}
bool isValid()
{
if (cin.rdstate()) //state is wrong when it is not equal to 0 // state is not wrong when it is equal to 0
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // skip the enter key left in the buffer
system("cls");
initMenu();
return false;
}
return true;
}
Example 1
#include <iostream>
using namespace std;
/*functions / methods */
//parameter
void welcome(); //declaration of function, procedure
bool isNumber(string);
void enterName();
double add(double a, double b) {return a + b;}
void changeValueTo10(int a)
{
a = 10;
}
int main()
{
// welcome();
/* enterName();
enterName();
enterName();*/
char ch;
cout << "Do you want to end the program? (Y/N)" << endl;
cin >> ch;
if (ch == 'Y' || ch == 'y')
return 0;
// cout << add(4,5);
int a = 5;
changeValueTo10(a);
cout << a;
return 0;
}
void welcome()
{
cout << "HEllo, welcome in my program!! :-)" << endl;
}
bool isNumber(string tmp)
{
if (tmp[0] == '0')
return false;
for (int i = 0; i < tmp.length(); i++)
{
if (!(tmp[i] >= 48 && tmp[i] <= 57))
return false;
}
return true;
}
void enterName()
{
string tmp;
cout << "Enter the number: " << endl;
cin >> tmp; //124
if (isNumber(tmp))
cout << "Number entered properly " << endl;
else
cout << "Number wasn't entered properly " << endl;
}
Example 2
#include <iostream>
#define PI 3.14
using namespace std;
// the belows are declaration of function
void initMenu();
void menuDecision(int);
double areaCircle(double);
double areaSquare(double);
double areaRectangle(double, double);
double areaTriangle(double, double);
int main()
{
int choice;
char cont;
do
{
system("cls"); //clear screen = cls
initMenu();
cin >> choice;
menuDecision(choice);
do
{
cout << "Do you want to continue the program? (Y/N)" << endl;
cin >> cont;
} while(cont != 'y' && cont != 'Y' && cont != 'N' && cont != 'n');
} while(cont == 'y' || cont == 'Y');
return 0;
}
void initMenu()
{
cout << "Enter option:" << endl;
cout << "1. Circle" << endl;
cout << "2. Square" << endl;
cout << "3. Rectangle" << endl;
cout << "4. Triangle" << endl;
}
void menuDecision(int choice)
{
double r, a, b, h;
switch(choice)
{
case 1:
cout << "Enter the radius: " << endl;
cin >> r;
areaCircle(r);
break;
case 2:
cout << "Enter the side of a square: " << endl;
cin >> a;
areaSquare(a);
break;
case 3:
cout << "Enter the width and height of a rectangle: " << endl;
cin >> a >> b;
areaRectangle(a, b);
break;
case 4:
cout << "Enter the base and height of a triangle: " << endl;
cin >> a >> h;
areaTriangle(a, h);
break;
default:
cout << "You didn't choose any of the option from above" << endl;
}
}
double areaCircle(double r)
{
double result = PI * r * r;
cout << "The area of a circle that radius is " << r << " = " << result << endl;
return result;
}
double areaSquare(double a)
{
double result = a * a;
cout << "The area of a square that side is " << a << " = " << result << endl;
return result;
}
double areaRectangle(double a, double b)
{
double result = a * b;
cout << "The area of a rectangle that first side is " << a << " the second side is " << b << " = " << result << endl;
return result;
}
double areaTriangle(double a, double h)
{
double result = (1/2.0) * a * h;
cout << "The area of a rectangle that first side is " << a << " the second side is " << h << " = " << result << endl;
return result;
}
Example 3
#include <iostream>
using namespace std;
/* overloading functions */
int power(int, int);
double power(double, int);
int main()
{
cout << power(2, 6) << endl;
return 0;
}
/*
2 ^ 3 = 2 * 2 * 2
2 ^ 1 = 2
2 ^ 2 = 4
2 ^ 3 = 8
*/
int power(int b, int e) // b = 8, e = 1
{
int tmp = b; //tmp = 2
int i = 0;
while(i++ < e)
{
cout << tmp << " ^ " << i << " = " << b << endl;
if (i != e)
b *= tmp; //b = b * tmp;
}
return b;
}
double power(double b, int e)
{
double tmp = b; //tmp = 2
while(e-- > 1)
b *= tmp; //b = b * tmp;
return b;
}
Example 4
#include <iostream>
using namespace std;
/*scope / range of varables */
int globalVariable;
main()
{
int localVariable;
/*
cout << "value of globalVariable: " << globalVariable << endl;
cout << "value of localVariable: " << localVariable << endl;
*/
int a = 10;
/*
if (a == 10)
{
int result = a * 10;
}
cout << result << endl;*/
int nr, result = 0;
int i = 0;
for (; i < 3; i++) // 3,2,3 - will the result be 8??? NOOOOOOOOO, we have to assign 0 to the result!
{
cout << "Enter " << (i + 1) << " number" << endl;
cin >> nr;
result += nr; //result = result + nr;
}
cout << result << endl;
cout << "we added " << i << " numbers" << endl;
}
Example 5
#include <iostream>
#include <cstdlib>
using namespace std;
double minValue(double tab[]);
double maxValue(double tab[]);
int main ()
{
double tab[5];
for (int i = 0; i < 5; i++)
{
cout << "Input " << i+1 << " number: ";
cin >> tab[i];
}
cout << "The minimum value was: " << minValue(tab) << endl;
cout << "The maximum value was: " << maxValue(tab) << endl;
return 0;
}
double minValue(double tab[])
{
double minValue = tab[0];
for (int i = 1; i < 5; i++)
{
if (minValue > tab[i])
minValue = tab[i];
}
return minValue;
}
double maxValue(double tab[])
{
double maxValue = tab[0];
for (int i = 1; i < 5; i++)
{
if (maxValue < tab[i])
maxValue = tab[i];
}
return maxValue;
}
Example 6
#include <iostream>
#include <limits> // to make "cin.ignore(numeric_limits<streamsize>::max(), '\n');"r active
#define PI 3.14
using namespace std;
/*validating data
buffer - temporary array
*/
void initMenu();
void menuDecision(int);
double areaCircle(double);
double areaSquare(double);
double areaRectangle(double, double);
double areaTriangle(double, double);
bool isValid(string);
bool isValid();
int main()
{
int choice;
char cont;
do
{
system("cls"); //clear screen = cls
initMenu();
while(!(cin >> choice))
{
//cout << "state before: " << cin.rdstate() << endl;
cin.clear();
//cout << "state after: " << cin.rdstate() << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // skip the '\n' charcter left in the input buffer
// 정수를 입력받은 후에 문자열을 입력받지 않고 다음 코드로 넘어간다. 버퍼에 정수값을 누른 엔터가 남아있어 그렇다. getline을 이용하기 위해서는 정수값을 입력받은뒤 ignore을 사용한다.
system("cls");
initMenu();
cout << "You've just typed the wrong data to the input. " << endl;
}
menuDecision(choice);
do
{
cout << "Do you want to continue the program? (Y/N)" << endl;
cin >> cont; //asdfg
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} while(cont != 'y' && cont != 'Y' && cont != 'N' && cont != 'n');
} while(cont == 'y' || cont == 'Y');
return 0;
}
void initMenu()
{
cout << "Enter option:" << endl;
cout << "1. Circle" << endl;
cout << "2. Square" << endl;
cout << "3. Rectangle" << endl;
cout << "4. Triangle" << endl;
}
void menuDecision(int choice)
{
double r, a, b, h;
switch(choice)
{
case 1:
do { cout << "Enter the radius: " << endl; cin >> r; } while(!isValid());
areaCircle(r);
break;
case 2:
cout << "Enter the side of a square: " << endl;
do { cin >> a; } while(!isValid("The data is wrong, please type it again:"));
areaSquare(a);
break;
case 3:
cout << "Enter the width and height of a rectangle: " << endl;
do { cin >> a >> b; } while(!isValid("The data is wrong, please type it again:"));
areaRectangle(a, b);
break;
case 4:
cout << "Enter the base and height of a triangle: " << endl;
do { cin >> a >> h; } while(!isValid("The data is wrong, please type it again:"));
areaTriangle(a, h);
break;
default:
cout << "You didn't choose any of the option from above" << endl;
}
}
double areaCircle(double r)
{
double result = PI * r * r;
cout << "The area of a circle that radius is " << r << " = " << result << endl;
return result;
}
double areaSquare(double a)
{
double result = a * a;
cout << "The area of a square that side is " << a << " = " << result << endl;
return result;
}
double areaRectangle(double a, double b)
{
double result = a * b;
cout << "The area of a rectangle that first side is " << a << " the second side is " << b << " = " << result << endl;
return result;
}
double areaTriangle(double a, double h)
{
double result = (1/2.0) * a * h;
cout << "The area of a rectangle that first side is " << a << " the second side is " << h << " = " << result << endl;
return result;
}
bool isValid(string error_msg)
{
if (cin.rdstate()) //state is wrong when it is not equal to 0
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("cls");
initMenu();
cout << error_msg << endl;
return false;
}
return true;
}
bool isValid()
{
if (cin.rdstate()) //state is wrong when it is not equal to 0 // state is not wrong when it is equal to 0
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // skip the enter key left in the buffer
system("cls");
initMenu();
return false;
}
return true;
}
Comments