Why am i getting the same result when i switched my agrument?

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
#include <iostream>

using namespace std;

int pow(int a, int b){
    int numb=a;
    while (b>0){
          cout << numb<<endl;
          numb = numb * a;
          b--;
          }
          }

template <class t>
t func(t a, t b){
         return (a>b?pow(a,b):pow(b,a));
         }

int main(){
    int a=2, b=3;
    
    cout << func(a,b)<<endl;
    cout << func(b,a)<<endl;
    
    system("pause");
    return 0 ;
}


The pow function is for the power operation, a^b, but i am not very sure if i wrote it correctly... In the int main(), I tole the compiler to print out 2 result of the func template with a and b switching their place. But I don't understand why both of them return the same value, and where the 2686724 come from in the result.

The "a>b?<1>:<2>" mean if a larger then b, command 1 will be execute and if a<b, command 2 will be execute right?

Thanks in advance.
Function int pow() should return a value.

Just before the closing brace of that function, you need to add the missing code:
 
    return numb;
Just get it, got a bit miss concept back then.
Last edited on
Topic archived. No new replies allowed.