Thread: Need some very nooby help.

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    6

    Need some very nooby help.

    Part of a source file I have is

    Code:
    int move_arrow(int x_arrow, int y_arrow, int COLOUR)
    {
    	int XF=XOFF,YF=YOFF;
    	int key_pressed,new_y_arrow,line;
    
    	key_pressed=getch();
    	key_pressed=getch(); 
    
    	new_y_arrow = y_arrow;
    	while (key_pressed==KEY_DOWN||KEY_UP||KEY_LEFT||KEY_RIGHT||RETURN) // replot arrow unless RETURN pressed
    	{
    		draw_arrow(x_arrow, new_y_arrow, BLACK);
    
    		/* move arrow if arrow keys pressed */
    		if (((key_pressed==KEY_DOWN) || (key_pressed==KEY_RIGHT)) && (new_y_arrow <= y_arrow+10*(NUM_HOLES-2)))
    		{
    			new_y_arrow=new_y_arrow+10;
    			line=new_y_arrow;
    		}
    		else if (((key_pressed==KEY_UP) || (key_pressed==KEY_LEFT)) && (new_y_arrow >= y_arrow+10))
    		{
    			new_y_arrow=new_y_arrow-10;
    			line=new_y_arrow;
    		}
    		else if (key_pressed==RETURN)
    		{
    			new_y_arrow=new_y_arrow;
    			line=new_y_arrow;
    			break;
    		}
    
    		draw_arrow(x_arrow, new_y_arrow, COLOUR);
    
    		/* read keyboard again */
    		key_pressed=getch();
    		key_pressed=getch();
    
    	} /* end of while loop */
    
    	return line;
    	
    }
    Does this function return a value for "line"? and how do I use this value in another function?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it does, but it also returns junk data if the loop conditions are never met, so initialize the variable.

    To take the return value of a function, you simply do:
    Code:
    int foo() { return 7; }
    int main()
    {
    	int n = foo();
    	// n contains the return value from foo
    	return 0;
    }
    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.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    A couple points...

    Quote Originally Posted by Lonners View Post
    Part of a source file I have is

    Code:
    int move_arrow(int x_arrow, int y_arrow, int COLOUR)
    {
            ....
    	return line;
    	
    }
    Does this function return a value for "line"? and how do I use this value in another function?
    It does return a line, but the function declaration says it returns an int.

    Also, line must be a global variable since it is not declared. Since it is a global, I'm not sure why you need to return it.

    Todd

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Line is a local variable, Todd.

    I don't understand why Lonners doesn't just try to print the returned value from the calling function, and THEN he'd know without wasting all this time.

    How long does it take to write one print line of code?

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by Adak View Post
    Line is a local variable, Todd.
    Yes, you are correct - I now see it crammed onto the end of the other declarations. My bad.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > while (key_pressed==KEY_DOWN||KEY_UP||KEY_LEFT||KEY_RIGH T||RETURN)
    You had the correct syntax for comparing with several keys later on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic nooby c string question
    By ilikepure in forum C Programming
    Replies: 8
    Last Post: 01-19-2008, 02:07 PM
  2. nooby question C
    By juststartedC in forum C Programming
    Replies: 9
    Last Post: 09-20-2007, 10:13 AM