Jump to content

? how do i add functions to an existing program?

- - - - -

  • Please log in to reply
2 replies to this topic

#1
marval

marval

    Newbie

  • Members
  • Pip
  • 1 posts
a year ago i took a beginning programming course & i remember the basics, declaring variables, defining the parameters.....but for the life of me i forget the order of where i can add on new functions to expand on a program. the insertion of the { } screws me up.

Here's the (very simple) initial code i have I was able to create a program that makes a circle. Now i want to add in a function that adds in a horizontal line and eventually a vertical line.


#include <allegro.h>




void docircle(BITMAP *bmp, int x, int y, int color)



{

    putpixel(bmp, x, y, color);

    putpixel(bmp, x+1, y+1, color);

    rest(1); //replaced sleep()

}




int main(void)


{

    int x,y,x1,x2,radius;

    int red,green,blue,color;

    int ret;

    

    //initialize Allegro

    allegro_init(); 

    

    //initialize the keyboard

    install_keyboard(); 

    install_timer();


    //initialize video mode to 640x480

    ret = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);

    if (ret != 0) {

        allegro_message(allegro_error);

        return 1;

    }


    //display message

    textprintf_ex(screen, font, 80, 8, 35, -1,

        "Do you like my object?  Hit ESC to exit the window.", 

        SCREEN_W, SCREEN_H);


    //wait for keypress

    while(!key[KEY_ESC])

    {

        //set a location and size for the circle

        x = 100; //(SCREEN_W-10);

        y = 80; //(SCREEN_H-120);

        radius = 10;


        //set a random color

        red = rand() % 255;

        green = rand() % 255;

        blue = rand() % 255;

        color = makecol(red,green,blue);

        

		   //draw the circle  

       do_circle(screen, x, y, radius, color, *docircle);



    }


void hline(BITMAP *bmp, int x1, int y, int x2, int color)


{

   

	

	//set random location for horizontal line

		x1 = 10 + rand()%(SCREEN_W-20);

		y = 10 + rand()%(SCREEN_H-20);

		x2 = 10 + rand()%(SCREEN_W-20);


        //set a random color

        red = rand() % 255;

        green = rand() % 255;

        blue = rand() % 255;

        color = makecol(red,green,blue);


		//draw the line

		hline(screen, x1, y, x2, color);

}


    //end program

    allegro_exit();

    return 0;


}

END_OF_MAIN()

What am i doing wrong in regards to the placement of my functions? What goes where so that my variables are specific to each function?

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You appear to be defining a function within another function. You can define hline almost anywhere in the file, such as above or below, as long as it is its own function.

If you wish to define the function code after main, you must define forward declarations (function signatures) for the compiler to recognize, a simple example would be this:

void hline(BITMAP *bmp, int x1, int y, int x2, int color);

int main() {
   hline(.....); //this can be called before its code is defined, due to the above "declaration"
}
//END_OF_MAIN() would be placed here if using allegro

//this is the definition:
void hline(BITMAP *bmp, int x, int y, int color) {
{
     //hline's definition here, with its own scope
}

You may notice the top declaration ends in a semicolon, this is purely to inform the compiler that a function with that structure will be called in main() before it is actually defined.

You can place your forward declarations in a header file to avoid cluttering the .c source if you wish, or avoid them all together and define hline() above main(), before it is called so the compiler understands its structure.

Some confusion appears to be brought on by the END_OF_MAIN() macro as well, it should be placed after the closing brace of main() and is purely Allegro's function, it has nothing to do with C and should be ignored other than being at the end of main(){}

You may wish to review C syntax and basic structure to grasp a little more understanding on what can be done to extend your program.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
masky007

masky007

    Newbie

  • Members
  • PipPip
  • 19 posts

//here goes your includes


// here you can declare and define a function like this 

void SomeFunction()

{

  something you want it to do;

} 


//another way you can do this is by using pototype function like this:

void SomeFunction2(); // note that here it ends with ";" and now i will define the function bellow the main {} :see bellow


[B]//here starts main()

[/B]

//here you can call a function like this:

         SomeFunction(); // this will call and initialize the function 

         SomeFunction2(); // this calls the function prototype from bellow main

[B]//here ends main()[/B]


//defining function 

void SomeFunction2 // note i do not end it with ";" here

{

something you want to do;

// i can call other functions when within a function like this just as i call any other function in main ex:

 SomeFunction(); // now when i will call SomeFunction2 in main it will initialize everything and than it will come to SomeFunction() than it will initialize that function and wen that ends it will contunie and eventually return control to the main when this function scope ends. 

}//now i will call the function in the main



// note that if you want to call function in a function there is no problem at all just make sure you pass the variables either by reference or use of pointers if you need to change them in the original function


I wanted to show you what you can make so you would understand and that's why i didn't give you a concrete example regarding your code.
I hope this helps you remember how functions work.. just keep in mind if you want to be able to change variable lets say in main() trough a function you should pass the variable by reference or use pointers. (try to understand every function as a different entity.. it parameters exist only in their scopes {} so when the function ends every variable ceases to exist in that function.. but when you pass a variable by reference "&" instead by value(which makes a copy) you give something like read/write direct access to that variable in main

This may was not your main question, but i tried to lay down everything about functions (at least what i know- though i am pretty new ;) )




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users