Defining a custom function

This is a discussion on Defining a custom function within the C Programming forums, part of the General Programming Boards category; I'm trying to learn how to clean up my code, and I think the solution will be to use custom ...

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:

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.

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:

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.

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.