I have this code in C language, but I'm trying to understand it very well. Could someone explain to me how the "int exponent" function works because I don't understand much of the logic. Thank you
Perform a function that raises a number to any exponent.
#include<stdio.h>
#include<math.h>
int exponente();
int main(){
int num;
int ex, res;
printf("Digite un numero cualquiera:");
scanf("%i", &num);
printf("Digite el exponente el cual desea elevar el numero previo:");
scanf("%i", &ex);
res=exponente(num,ex);
printf("\n El numero escogido %i , elevado al exponente escogido es: %i \n", num, res);
system("pause");
return 0;
}
int exponente(int x, int y){
int z=1 , i;
for(i=1; i<=y; i++){
z=z*x;
}
return (z);
}
Microsoft documentation for C: Documentation C
It is a function that returns an integer and also receives two integers
int x, int y
, it goes through a for that iterates as many times as the value 'y' indicates, within this for 'z' is reassigned being equal to its current number multiplied by the value x. Ex:with
x=5,y=3
:Then return that z value
Perhaps it is not understood because it has bad programming practices, that is, the code should be able to explain itself and not use variable names like "z", or "a":