Problem with functions
I'm going to be fairly active today in my questions as I am studying for a final. I wish I registered sooner; maybe I'd know a lot of this by now.
I guess I don't quite understand the mechanics of how the function works. I don't quite see how this points to that and that's probably why I'm making such kiddy errors.
This is the question I'm working on:
1. Write a function int isodd(int x) that returns 1 (i.e., true) if x is odd and returns 0 (i.e., false) if x is even. Put code to test the function in main().
Here is the code I wrote:
It compiles just fine, but anytime I input a number, the print function will say something along the lines of "The number you have entered is 734242342". Like I said, I'm guessing it's my misunderstanding of the way a function works.
int isodd(int x);
int g, y;
printf("Enter a number: ");
y = isodd(g);
printf("The number you have entered is %d\n", y);
int isodd(int x)
int even, odd, h;
if (x % 2 == 0)
h == even;
h == odd;
For example, in my main function, I defined two variables: g and y. I set g equal to whatever the user inputs and I utilize y as a kind of pointer (probably misusing the word) to the isodd function. I don't really understand how the value entered, g, is relayed over to the function below, and that's my guess for why this is so hard for me.
I don't know if that complicated things, but yeah, I'm just basically asking for what's wrong with my function.
All help appreciated.
You want to use a single = to set a value. IE: h = even;. As currently you are not setting a value for h the function is returning whatever randoom junk was left in its register, which is why you get the weird output. Also, even and odd are undefined too. You will waant o set them to something, or just use a constant value.
1-even and odd are uninitialized junk integers when the function is called.
2- why are you using '==' here ?
This function is just
I don't think there is a reason to make it much more difficult than that, because your only goal is to return a distinct boolean result.
/*** Returns nonzero for odd values. ***/
isodd (int value)
return value % 2 != 0;
Firstly main should be int main, it returns an int. Secondly '==' test for equality, '=' is the assignment operator. even and odd are undefined, they have not been assigned to anything yet so h could to point to anything. Also, the way you are going about solving the problem creates a longer solution than necessary and declaring three variables when you don't even need any as there is no point in storing variables if you are going to return them instantly. Anything in the same statement after the return is executed first, so you could put numerous operators, comparisons and functions in the return statement (wouldn't make it too hard to read or put loops in there though). citizen's function is the easiest and quickest way of approaching the problem.
Scanf reads input and stores that in g (misleading name, btw).
Originally Posted by WTFSEAN
Then you call isodd and pass an argument. The value you pass as argument is g.
The argument you pass is named x inside isodd.
When isodd returns, the value it returns is stored inside y and then you print it.
If it's too much for you, then you need to back a little bit and get the basics down so you understand it.
All variables contain "garbage" (that is, a random value) unless you initialize it to something. Initialize means setting it to something. What you get from isodd is junk as they say. A random value because you're just returning whatever happens to be in h.
And as they say, == is for comparing (if is a == b), and = is for assigning.