1. ## 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:

Code:
```#include<stdio.h>
int isodd(int x);

main()
{
int g, y;
printf("Enter a number: ");
scanf("%d", &g);
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;
else
h == odd;
return h;
}```
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.

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.

2. 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.

3. 1-even and odd are uninitialized junk integers when the function is called.
2- why are you using '==' here ?

4. This function is just
Code:
```/*** Returns nonzero for odd values. ***/
int
isodd (int value)
{
return value &#37; 2 != 0;
}```
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.

5. 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.

6. Originally Posted by WTFSEAN
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.

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.