# Problem with functions

Printable View

• 05-09-2008
WTFSEAN
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.
• 05-09-2008
mike_g
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.
• 05-09-2008
Terrorist
1-even and odd are uninitialized junk integers when the function is called.
2- why are you using '==' here ?
• 05-09-2008
whiteflags
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.
• 05-09-2008
P4R4N01D
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.
• 05-10-2008
Elysia
Quote:

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.

Scanf reads input and stores that in g (misleading name, btw).
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.

And now:
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.