Thread: Defining a custom function

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    14

    Defining a custom function

    I'm trying to learn how to clean up my code, and I think the solution will be to use custom functions. My current project is a text-based adventure game. Right now I'm trying to get the room system working. Here is my movement code - this code is nested in a switch case that is alphabetized. I'd like the switch case to only contain refrences to custom function calls to help keep it smallish in size and easy to manage. This is what the movement code for going in the direction "down" looks like:

    Code:
    case 'D':
    	case 'd':
    			if( strcmp( input, "Down") == 0 ||
    				strcmp( input, "down") == 0 ||
    				strcmp( input, "D") == 0 ||
    				strcmp( input, "d") == 0 ){
    						if( ableGoD == 1 ){
    							printf( "You head down.\n");
    							floor--;
    							printf("Your current cords are %d,%d. Your current floor is %d\n", cords[0], cords[1], floor );
    						}
    						else
    							printf( "The way is blocked.\n");
    			}
    		else
    			printf("I don't understand Your current cords are %d,%d. Your current floor is %d\n", cords[0], cords[1], floor );
    		
    		break;
    I'd like to be able to trim it down to something like this:

    Code:
    case 'D':
    	case 'd':
    			if( strcmp( input, "Down") == 0 ||
    				strcmp( input, "down") == 0 ||
    				strcmp( input, "D") == 0 ||
    				strcmp( input, "d") == 0 ){
    				        call movedown function
    			}
    		else
    			printf("I don't understand Your current cords are %d,%d. Your current floor is %d\n", cords[0], cords[1], floor );
    		
    		break;
    In otherwords, I'd like to remove all movement code from my switch case, and keep the actual code in another part of the program to improve readability and keep things neat. Now, keep in mind I've only been programming for a few weeks, but I've read up in my book something that sounds near what I need to do, but I can't get it work. I believe I need to define something like this at the beggining of my program:

    int movedown( blahblah); /* function prototype */

    And then build upon that outside of my main function with it's own code. However, every attempt I've made at this has failed, and the examples given in my book are not parallel enough for me to understand them very clear.

    Could someone out there tell me if I'm headed in the right direction, or possibly post to some code that would be of help to me? I'm not asking the answer be handed to me, just some hints as to if I'm even on the right track.

    Another of my ideas is this - instead of having the same error messages posted over and over, I'd like to store all of my error messages in one part of the program, in it's own function. I can't figure out how to do this, either, though. I would need the main function to send a value to the error function, and have the error function read this value and send back the correct error, correct? I haven't figured out how to do that, either, but I feel that it could be accomplished similar to my first problem.




    Edit: I think an even better idea for the movement code would be this: Instead of having a single function for each direction, simply do a call to a "move" function, which then takes a look at which direction the character is trying to head in, and once it deduces that, send the correct course of action back to the main function. Having the movement code all in a single function would cut down on code used, and improve readability etc.
    Last edited by Nalif; 10-01-2006 at 12:10 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So what part can't you figure out? How to write your own functions? How to pass them arguments? What to pass them? I'm not sure where you're having trouble.
    Code:
    /* in something.c */
    #include "do.h"
    ...
    lower( arg ); /* write a function to squash the case of the argument */
    switch( arg[0] )
    {
        case 'd':
            if( strcmp( "down", arg ) == 0 || strcmp( "d", arg ) == 0 )
                domove( who, dir_down );
            ...
    Then, your "do.h" header has all the prototypes for all of your actions (thinks you can do).
    Code:
    int domove( SOMETHING *who, int dir );
    int dojump( ... );
    int doget( ... );
    int dosleep( ... );
    Then your do.c file has all of the code for the actual functions.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    14
    I guess creating custom functions are above me as of yet, and I'd better just stay away from them completely. I don't understand them, and understand even less the examples you posted. I'll just keep reading my book and trying out other examples, because at this point, I don't even know how to explain to you what I don't understand. This is the current code that I'm messing with:

    Code:
    case 'N':
    	case 'n':
    		if( strcmp( input, "North" ) == 0 || 
    			strcmp( input, "north" ) == 0 ||
    			strcmp( input, "N" ) == 0 ||
    			strcmp( input, "n" ) == 0 ){
    					int	movenorth(); /* this is the custom function I'm trying to call*/
    		}
    		else
    			printf("I don't understand. Your current cords are %d,%d. Your current floor is %d\n", cords[0], cords[1], floor );
    		break;
    That's the code that calls the function I'm trying to define. I'm defining the function not in another file, as in your example, but after my main function has ended. This is valid as well, right?

    Here's the custom function code.

    Code:
    int movenorth() {
    		  if( ableGoN == 1){
    							printf("You head North.");
    							cords[0]++;
    							printf("Your current cords are %d,%d. Your current floor is %d\n", cords[0], cords[1], floor );
    							goto roomdesc;
    						}
    						else
    							printf("The way is blocked.\n");
    
    						return 0;
    }
    Likely it's not even close to what I need to be doing. My code is working as is right now, and creating custom functions are above me for now. I'll just keep working on the aspects that I do know, and learn more about this on my own, as I'll likely just get people mad at me for having no idea what I'm doing.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you know how to make any functions at all?
    Code:
    void fun( void )
    {
        printf( "A function called 'fun', returns nothing (void), takes no arguments (void)\n" );
    }
    Now you can do the same thing, but perhaps change the arguments to take your string. Rename it to "move", and now you have a move function. Call this with something like: "move( arg );" for every time in your list of commands that you require movement. Then inside your "move" function, take the command you have passed it, and see what way you need to go. Or, instead of passing the command itself, pass a number which represents what way you want to move.

    If you don't know how to write functions, spend some time writing small programs with different functions that take different types of arguments. You mention a book, so read the chapter on making functions.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  5. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM