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.