My issue is that I keep getting the same type of error and I can not understand why. I'm fairly sure I declared and defined it before the main function.
Here is my code.
#include <stdio.h>
void functn (int x);
int functn(int x, int result){
result = (x-1)+2;
if (x <= 0){
return 0;
}
else{
return result;
}
}
int main (){
int x, y;
printf ("Enter the value of x: ");
scanf ("%d", &x);
y = f(x);
printf ("%d", y);
return 0;
}
You have a number of problems in functn. Primarily passing result. When you pass result to functn, functn receives a copy of result and the only way to get the modified value is to return the modified value. You can also pass a pointer to result and update *result in functn (similar to what you do now), that would eliminate the need to return a value, as any changes to result would be visible back in the calling function (main() here). Further, there is no need for global variables. Simply declare the variables local to main and pass as parameters, as required.
The following example declares functn (simply f below) to do both, take a pointer to result (e.g. &result) as a parameter, while also returning result, which allows you to either assign the return or not, but always have the updated value for result back in main, e.g.
#include <stdio.h>
int f (int x, int *result);
int main (void){
int x, y, result = 0;
printf ("Enter the value of x: ");
if (scanf ("%d", &x) != 1) {
fprintf (stderr, "error scanf, invalid conversion.\n");
return 1;
}
y = f (x, &result);
printf ("y = f(%d) => %d\n", x, y);
return 0;
}
int f (int x, int *result)
{
*result = (x-1)+2;
if (x <= 0)
return 0;
return *result;
}
Example Use/Output
$ ./bin/yfx
Enter the value of x: 5
y = f(5) => 6
Look things over and let me know if you have questions.
Return type of functn() different in declaration and definition. So, use
int functn(int x);
instead of
int functn(int x, int result)
Remove int result from function argument and declare inside function. like,
int functn(int x)
{
int result = 0;
result = (x-1)+2;
if (x <= 0){
return 0;
}
else{
return result;
}
}
Also, correct function call, like
y = functn(x);
instead of
y = f(x);
Related
This recursive function 'int f(int n)' should not return anything except when 'n == 1'. If we put
n = 2,3 then y = 2. When n = 4,5 then y = 3, when n = 6,7,8 then **y = 4 **and so on. Here, variable x is static. Below is the code:
#include <stdio.h>
int f(int n){
static int x=1;
int i;
if(n==1){
return 1;
}
else {
for(i = 1; i<=2; i++){
x = x + f(n-1);
printf("%d\n",x);
}
}
}
int main() {
int n = 3;
int y =f(n);
printf("y %d",y);
}
Kindly help me understand how the value of y is assigned or how f() is returning such values.
Thank you.
I was expecting 2,3,4,5,6,7. I am able to get those values implementing some other stuff but that
is not what I want to understand. I want to understand the f() is 2, when n is 2,3. How f() is returning a value without the return statement?
The function f doesn't return a value on all code paths. This means undefined behavior. It may return anything, or y may remain uninitialized.
#include<stdio.h>
const char *author ="Alexandre Santos";
int succ(int x)
{
return x+1;
}
int pred(int x)
{
return x-1;
}
int is_zero(int x)
{
return x == 0;
}
int is_pos(int x)
{
return x >= 0;
}
int sum(int x, int y)
{
return is_zero(y)? x: sum(succ(x),pred(y));
}
int twice(int x)
{
return sum(succ(x), pred(x));
}
int main(void)
{
int x;
scanf("%d", &x);
int z = twice(x);
printf("%d\n", z);
return 0;
}
I am in the first year of university and this is one of the exercises that a professor gave. I need to calculate the double of a number just using the given functions(succ, pred, is_zero, is_pos). I tried to do it and managed to come up with a solution but to be honest I don't understand how this is working. My main doubt is how the sum function is working since it uses the variable y and in this program this variable doesn't even exist/is not inserted in the input. Any tip?
In your code, you called the function twice() with the parameter x, and in the function twice() you called the function sum() with the parameters succ(x) and pred(x), the value of succ(x) is assigned to x in sum, and the value of pred(x) is assigned to y in sum.
So for example; you passed the value 10 to x in the input, we will call the function twice with a value 10, and twice(10) returns the sum of succ(10) and pred(10) which are 11 and 9, so the values 11 and 9 are passed to the function sum like this: sum(11, 9), so in the function sum, the parameter x gets the value 11, and the parameter y gets the value 9.
The format for the input is a x and sequence of composite functions. eg. 42 hfgf= and the output for that one would be 2829124.
This is what I have so far, I'm not sure how to let it understand the order in which it should compute this.
Please help.
#include <stdio.h>
int functionF(int x) {
x = x * x;
return x;
}
int functionG(int x) {
x = x + 1;
return x;
}
int functionH(int x) {
x = x - 1;
return x;
}
int main(int argc, char *argv[]) {
int n;
char function;
int result;
scanf("%d", &n);
scanf(" %c", &function);
while (function != '=') {
if (function == 'f')
functionF(n);
if (function == 'g')
functionG(n);
if (function == 'h')
functionH(n);
scanf("%c", &function);
}
printf("%d\n", result);
return 0;
}
The arguments to functions f(), g(), and h() are passed "by value," not "by reference." That means that changing the value of x in the function does not change the value of n in main().
Instead, your functions should return a value. For example:
int functionF(int x) {
return x * x;
}
Then the caller should assign the result to n in main(), like this:
if (function == 'f')
n = functionF(n);
And by the way 42 fgfg yields 3115226. You'd get 2829124 if the sequence were 42 hfgf, i.e. ((42-1)^2+1)^2
I want to create a recursive Factorial using the Pass By Reference method.
int recursiveFactorialByValue(int x){
if (x==0||x==1) return 1;
else if (x<=0) return -1;
else return x * recursiveFactorialByValue(x-1);
}
void recursiveFactorialByReference(int *x){
int minusOne = *x - 1;
int *ptr = &minusOne;
if (*x==0||*x==1) *x = 1;
else if (*x <= 0) *x = -1;
else *x * recursiveFactorialByReference(ptr); //this is where the error occurs
}
int main(){
int x, *ptr=&x, **pptr=&ptr, y;
printf("Enter a positive integer: ");
scanf("%d",*&x);
y = x;
printf("%i! = %i\n",y,recursiveFactorialByValue(x));
recursiveFactorialByReference(ptr);
printf("%i! = %i\n", y, x);
return 0;
}
I get this error:
In function 'recursiveFactorialByReference':
14:7: error: void value not ignored as it ought to be
else recursiveFactorialByReference(ptr) * *x;
I've tried different function calls like:
else *x = *x * recursiveFactorialByReference(*x-1);
else *x = *x * recursiveFactorialByReference(ptr);
and none of these work, I can't find the problem please help.
*x * recursiveFactorialByReference(ptr)
This multiplies the value pointed to by x (which is an int) with the return value of recursiveFactorialByReference (which is void), which makes no sense. Think about what your recursive function does: you pass it a pointer to an int and it replaces that int with its factorial. So your recursive part should look like this:
else
{
recursiveFactorialByReference(ptr);
*x *= *ptr;
}
Note that you still have to assign to *x. Multiplying it on its own just throws away the value.
Also you have an extra * in your scanf call. It should be
scanf("%d", &x);
Also these technically aren't "references" (that's a concept from C++). Both of your functions are passing values, the latter just happens to be passing the values of pointers.
So I had to write a program that used the Pythagorean Threes concept where if you entered a number it would give you all the combinations less than that number that would produce a correct a^2 + b^2 = c^2 output.
Not sure if I explained the assignment well, but basically I understand the logic or at least I think I do I was wondering if you guys could help me find out why I am getting this error....
For the last line of my code it gives me, "warning: control reaches end of non-void function [-Wreturn-type]," As the error any idea what I am doing wrong?
#include <stdio.h>
int main(void){
int x = 0, y = 0, z = 0, n;
int count = 0;
printf("Please Enter A Positive Integer: \n");
scanf("%d", &n);
while(z <= n){
while(y < z){
while(x < y){
if(x * x + y * y == z * z)
printf("%d: \t%d %d %d\n", ++count, x, y, z);
x += 1; }
y += 1; }
z += 1;
}
}
int main(void){
Your function header indicates that you're looking to return an int. To fix this, return a number (0 usually indicates a normal termination) at the end of your function.
To break it down a little,
int indicates the return type,
main is the method name, and
void indicates that there are no parameters for this method.
Your main function is declared to return an int, but you don't return anything.
put return 0; before the closing brace of your main.
int main( void )
{
// ... code ...
return 0;
}