Thread: Problem with functions

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    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. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    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. #3
    Registered User
    Join Date
    May 2008
    Posts
    18
    1-even and odd are uninitialized junk integers when the function is called.
    2- why are you using '==' here ?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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. #5
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    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.
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by WTFSEAN View Post
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with functions
    By saliya in forum C Programming
    Replies: 1
    Last Post: 11-05-2007, 10:36 PM
  2. Problem with system(), execl(), etc. functions
    By nickkrym in forum C Programming
    Replies: 5
    Last Post: 05-12-2007, 08:04 AM
  3. Problem: Functions
    By Dmitri in forum C Programming
    Replies: 21
    Last Post: 11-06-2005, 10:40 AM
  4. Problem with pointers and functions
    By Kheila in forum C++ Programming
    Replies: 5
    Last Post: 10-13-2005, 12:40 PM
  5. Problem with functions
    By lizardking3 in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2003, 04:34 PM