Hello,
So I was working on a HW assignment in my C programming class, and we are learning about recursion. I guess the idea itself is pretty self explanatory, a function calling itself, but how it works is where I get lost! I got the assignment to work, but I'm not sure why what I coded worked. How does the function know what to return when there's 2 numbers inputted? It's just a function with 2 parameters and no variable changing so how does x and y get altered? I understood my previous assignment where we used recursion to return x to the nth power, but this one is a little less concrete with me.
Could anyone help me understand this a little more? Any help is greatly appreciated!
#include <stdio.h>
int gcd(int x, int y)
{
// base case
if(y == 0)
return x;
return gcd(y, x % y);
}
int main(int argc, const char * argv[])
{
int x = 54;
int y = 24;
printf("GCD of %i and %i is %i\n", x, y, gcd(x,y));
return 0;
}