Functions

closed account (iAk3T05o)
How would you use functions in a calculator program?
I got the jumping into c++ book and i've met functions again. This time, i kinda understand it (this is the 5th book i'm using to learn c++), last problem was vectors so i decided i needed a new book (this was after 2 failed functions learning and 1 failed array learning).
In this book, functions comes before arrays and the first time i met functions was in the cplusplus.com pdf, then in programming principles book which i just switched from and i had no idea what so ever what it did/what they were talking about, so i changed books. Now i've met it again and this book explains better.
I was writing calculator program and it doesn't seem right, it does everything right but it's like 2/3x more code than doing everything in int main().
I'm posting via mobile and the code is like 100 lines and it'll take a while to write that.
How would you use functions in a simple (+, -, /, *) calculator program?
Thanks.
closed account (iAk3T05o)
Ok.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
 
#include <iostream>
#include <string>

double calc_add (double a, double B)/>
{ 
return a+b;
}

double calc_minus(double a, double B)/>
{
return a-b;
}

double calc_multiply(double a, double B)/>
{ 
return a*b;
}

double calc_divide(double a, double B)/>
{
return a/b;
}

void calculator ()
{

double x = 0;
double y = 0;
char unit = ' ';
std::string QUIT;

do
{

std::cout <<"CALCULATOR \n\n";

std::cout <<"Enter operation: ";
std::cin >> x >> unit >> y;

switch (unit)
{
case '+':
std::cout << calc_add(x, y);
break;

case '-':
std::cout << calc_minus(x, y);
break;

case '*':
std::cout << calc_multiply(x, y);
break;

case '/':
std::cout << calc_divide(x,y);
break;

}

std::cout << "\n\nDo you want to quit: ";
std::cin >> QUIT;

if (QUIT == "No")
{
system ("cls")
)

}

while (QUIT == "No");
}

int main() 
{
calculator();

return 0; 
}  

Managed to get it.
No indentation (no tab key on mobile).
closed account (iAk3T05o)
Is that code right?
closed account (iAk3T05o)
Please help.
In your function declarations, it looks like you've gotten confused with XML or something. Why do you have the /> at the end of each? Apart from that, it looks fine, though you don't really need the functions in something as simple as that (as you noted).
closed account (iAk3T05o)
It's not confusion, it's the phone.
Can you list some programs that would benefit from functions. I need something to practice on.
Thanks
Anything that would require using something multiple times, or it makes sense to seperate out the function for documentation reasons, should be a function. In this case, a function isn't necessary because you already have the '+', '-', '*' and '/' operators to do everything you need. However, if you had a formula or algorithm that you wanted to do for everything in say, an array, you should use a function there.

For example, you could write a program that reads in an array, and then have a function that works out the square root of every value in the array, by having a sin function which you pass each element in the array to seperately. Then the program just adds the values together (or something) and prints them. Try that.

For your reference, sin is defined as:
sin x = x - (x^3)/3! + (x^5)/5! - (x^7)/7! + ...


For this, you will need to do a power function as well (not too hard, just loop multiplying the number) and a factorial function, where n! is defined as
n! = n * (n-1)!;    1! = 1;    0! = 1

So you can use a recursive function (function that calls itself), but don't need to. Do a recursive function anyway, just because.

In case you are wondering why I picked this, I did it just then, and felt like giving it to you. Sorry its a bit hard!
Last edited on
closed account (iAk3T05o)
And confusing. I haven't yet gotten to arrays.
Is that my code c style. If it is, could you list what parts are.
No, its completely unrelated to your code. Here, rather than the array, how about you just use a single value. I'll give you a bit of an example to start you off, I'll do the power function for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

int power (int value, int power) {
    int temp = value;
    for (int i = 1; i < power; ++i)
        temp *= value;
    return temp;
}

int factorial (int value) {
    // function
    // remember, factorial(n) = n * factorial(n-1)
    // that should make it easier
}

// sin function here, look at the formula to do it
// uses the factorial and power functions above

int main() {
    int num;
    std::cout << "Enter a number: ";
    std::cin >> num;

    std::cout << "sin(" << num << ") = " << sin(num) << std::endl;
    return 0;
}
Topic archived. No new replies allowed.