Thread: How to make a function and call it?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68

    How to make a function and call it?

    Hi there everyone, still teaching myself c and would like to ask a question.

    In my program i'm writing, i use a piece of code an awful lot, namely this construction:
    Code:
    for (x = 0; x < 9; x++)
    {
    	for (y = 0; y < 9; y++)
    	{
    		for (z = 0; z < 9; z++)
    		{
    		//Some code here
    		}
    	}
    }
    Is it possible to put this into a function that i can call?

    For example:

    Code:
    setupcoordinates( Some Code Here );
    I've no idea how i can go about this but would like to know as it would shorten my program by a lot of lines!!!

    Thanks everyone for your time.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Let me see if I first got your question right:
    You have several places where you have three nested loops [three loops, one inside the other]. In the center of that, you do something.

    Now, that's fairly clear. What I'm not so sure about is what you actually want to do: Do you want to have one funciton that does the loops and then does different things "in the middle"?

    Is this what you want?

    It can be done - you could pass a function pointer to such a function, which takes 3 or more parameters (x, y and z). The parameters need to always be the same type and order - but you could of course pass into the outer function some variables that are used by the inner function.

    Does that help you, or are you even more lost now? If so, you probably need to study "function pointers".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM