In a tutorial for functions the following code was given:
// Use this in your power() function
int my_power;
// Needed by the test
int answer;
void setup() {
Serial.begin(9600);
// Should be 9
answer = power(3, 2);
Serial.println(answer);
// Should be 5
answer = power(5, 1);
Serial.println(answer);
// Should be 1
answer = power(9, 0);
Serial.println(answer);
// Should be 16384
answer = power(2, 14);
Serial.println(answer);
// Should be 0
answer = power(0, 4);
Serial.println(answer);
}
void loop() {
// Do nothing
}
int power(int x, int y) {
// Special case: if y is 0, return 1
if ( y == 0 ) {
return 1;
}
// Store x argument in the my_power variable
my_power = x;
// Count down using y. Multiply my_power by x for each iteration
while ( y > 1 ) {
my_power *= x;
y--;
}
// Return the accumulated value in my_power
return my_power;
}
There is a lot of stuff I undersand. Im going to list them for the sake of being organized.
-
Isn't the x variable the base? Why was it assigned to my power varibable?
-
I tried the test cases and plugged them in but I couldn't get them to work? Can you help me please?
-
What does the *=
mean?
Thanks!
Have you tried entering the words "c++ *=" into your internet search engine and view the results?
-
my_power is just a name... I don't think it needs to be static and the name doesn't fit the use, but it's just a name.
-
sure, see updated code below
-
it's a compound operator, x *= y is the same as x = x * y
void setup() {
Serial.begin(9600);
int answer = power(3, 2);
Serial.println(answer);
answer = power(5, 1);
Serial.println(answer);
answer = power(9, 0);
Serial.println(answer);
answer = power(2, 14);
Serial.println(answer);
answer = power(0, 4);
Serial.println(answer);
}
void loop() {
// Do nothing
}
int power(int base, int power) {
int response = base;
if ( power == 0 ) return 1;
for (int i = 1; i < power; i++) response *= base;
return response;
}
engineer123498:
- Isn't the x variable the base? Why was it assigned to my power varibable?
Because x^2 is xx and x^3 is xxx. If 'my_power' was left as 0 you would always get 0: 0x, 0xx, ...
engineer123498:
2. I tried the test cases and plugged them in but I couldn't get them to work? Can you help me please?
What answers did you get?
engineer123498:
3. What does the
*=
mean?
The expression "x *= y" is a shortcut for the expression "x = x * y". Also works with "+=", "-=", "/="... Pretty much any operator you can put between two numbers. (Not '=='. You can't use "x === y;'.)
Your sketch works fine for me. I ran it on an Arduino UNO and got these results on Serial Monitor:
9
5
1
16384
0
johnwasser:
Because x^2 is xx and x^3 is xx*x.
sp. "x2 is xx and x3 is xx*x"
same function a third way...
int power(int base, int power) {
int response = 1;
if ( power == 0 ) return 1;
for (; power !=0; --power) response *= base;
return response;
}
all of these only work if y is 0 or a positive integer.
Its not that the code doesn't work..its I just have no idea how it works.
As I said, the x variable which is the base, is assigned to my power, which confused me Could somone please walk me trough how it works? Im new to functions and this was really confusing. Thanks!
a 4th way...
void setup() {
Serial.begin(9600);
double answer = pow(3, 2);
Serial.println(answer);
answer = pow(5, 1);
Serial.println(answer);
answer = pow(9, 0);
Serial.println(answer);
answer = pow(2, 14);
Serial.println(answer);
answer = pow(0, 4);
Serial.println(answer);
}
void loop() {
// Do nothing
}
engineer123498:
Could somone please walk me trough how it works? Im new to functions and this was really confusing. Thanks!
Your version had comments next to each line in the code. Did you put those there or do you have trouble understanding those comments?
a 5th way...
int power(int base, int exponent) {
if (exponent != 0)
return (base * power(base, exponent - 1));
else
return 1;
}
Perehama:
a 5th way...
int power(int base, int exponent) {
if (exponent != 0)
return (base * power(base, exponent - 1));
else
return 1;
}
. power (3, -1);
Oops.
TheMemberFormerlyKnownAsAWOL:
. power (3, -1);
Oops.
as stated, these the OP code, 1, 2, 3 and 5 only work if y is 0 or a positive int. for negative exponents, use the avr-libc function pow() as in #4...
void setup() {
Serial.begin(9600);
double answer = pow(3, 2);
Serial.println(answer);
answer = pow(5, 1);
Serial.println(answer);
answer = pow(9, 0);
Serial.println(answer);
answer = pow(2, 14);
Serial.println(answer);
answer = pow(0, 4);
Serial.println(answer);
answer = pow(3, -1);
Serial.println(answer);
}
void loop() {
// Do nothing
}
engineer123498:
Its not that the code doesn't work..its I just have no idea how it works.
As I said, the x variable which is the base, is assigned to my power, which confused me Could somone please walk me trough how it works? Im new to functions and this was really confusing. Thanks!
To raise x to the power of y it multiplies x by itself 'y' times. It starts by saving 'x' in a variable (named 'my_power' but the name is not important). That variable is then multiplied by 'x' and stored back in the variable 'y-1' times. The repeat is done with "while (y > 1)" which repeats until 'y' is 1 or less. After the multiply, 'y' is decremented by 1: "y--;" which is a shortcut for "y -= 1;" which is a shortcut for "y = y - 1;".
Some simple examples:
If y starts as 2:
x gets saved in my_power
the while() loop runs because y (2) is greater than 1
my_power gets multiplied by x so it now contains xx
y gets decremented to 1
the while() loop ends because y (1) is not greater than 1.
my_power still contains xx
If y starts as 3:
x gets saved in my_power
the while() loop runs because y (3) is greater than 1
my_power gets multiplied by x so it now contains xx
y gets decremented to 2
the while() loop runs because y (2) is still greater than 1
my_power gets multiplied by x so it now contains xxx
y gets decremented to 1
the while() loop ends because y (1) is not greater than 1.
my_power still contains xx*x
1 Like