Doubt regarding the passage of parameters

Asked

Viewed 41 times

1

In the code below, in line 5, the function foo1t calls the function foo1 passing two parameters to it, however, the function foo1 has only one parameter. Is it possible to do this or the code is wrong? If possible, I would like an explanation.

1   int foo1t(int n, int resp) {
2       if (n <= 1) {
3           return resp;
4       } else {
5           return foo1(n-1, n*resp);
6       }
7   }
8
9   int foo1(int n) {
10      return foo1t(n, 1);
11  }

1 answer

4


No, the code is wrong. Probably the inductive step should be return foo1t(n-1, n*resp);

Functions in C can be declared "no parameters", and can have a variable amount of parameters (functions variadic, as printf()), but if the parameters are declared they cannot be called with a wrong number of parameters. In the case of pointers or integers, you can even pass an integer in place of a pointer or vice versa, but the compiler will typically issue at least one warning.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.