Q:

Skip fragment of code C++ SDL2

I'm writing a function in C++ with the SDL2 library, and I'm stuck with a small problem. The function deals with everything related to the graphics. I want to use it once to create the window, surface and so on ( graphics(0,0) ), and that everytime I use it after that it just updates the value of x and y, and updates the window surface. How can I do it?  I've tried everything I could think of, but in order to update the window surface I need to create the window again. Thanks in advance.
void graficos(int var1,int var2){
    int x = 0, y = 0; //declare the variables that will determine position of rectangle
    x += var1; y += var2; //declare the variables that will modify x & y
    //delcare graphics and load them
    SDL_Window * window = SDL_CreateWindow("window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
    SDL_Surface * surface = SDL_GetWindowSurface(window);
    SDL_Surface * texture = SDL_LoadBMP("texture.bmp");
    SDL_Rect rectangle = { x, y, 50, 50 };
    //render graphics
    SDL_BlitSurface(texture, NULL, surface, &rectangle);
    //update window
    SDL_UpdateWindowSurface(window);
    }

A:

First. You can break your function into two: init and graficos like this:
void init( SDL_Window * &window, SDL_Surface * &texture ) {
    window = SDL_CreateWindow("window",
        SDL_WINDOWPOS_UNDEFINED,
        SDL_WINDOWPOS_UNDEFINED,
        640,
        480,
        0
    );
    texture = SDL_LoadBMP("texture.bmp");
}

void graficos( SDL_Window * window, SDL_Surface * texture, int var1, int var2 ) {
// You can delete these lines and use var1 and var2 directly
    /*    int x = 0, y = 0; //declare the variables that will determine position of rectangle
    x += var1; y += var2; //declare the variables that will modify x & y
    */
    SDL_Surface * surface = SDL_GetWindowSurface(window);
    SDL_Rect rectangle = { var1, var2, 50, 50 };
    //render graphics
    SDL_BlitSurface(texture, NULL, surface, &rectangle);
    //update window
    SDL_UpdateWindowSurface(window);
}

And here is the usage:
// somewhere in global place
SDL_Window * window;
SDL_Surface * texture;

// somewhere in initialization of programm
init( window, texture ); // call it only ones
...

graficos( window, texture, newX, newY ); // call it every time you need

Second. You can make window and texture as static like this:
void graficos(int var1,int var2){
    /*        int x = 0, y = 0; //declare the variables that will determine position of rectangle
    x += var1; y += var2; //declare the variables that will modify x & y
    */
    //delcare graphics and load them
// ---v---
    static SDL_Window * window = SDL_CreateWindow("window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
// ---^---
    SDL_Surface * surface = SDL_GetWindowSurface(window);
// ---v---
    static SDL_Surface * texture = SDL_LoadBMP("texture.bmp");
// ---^---
    SDL_Rect rectangle = { x, y, 50, 50 };
    //render graphics
    SDL_BlitSurface(texture, NULL, surface, &rectangle);
    //update window
    SDL_UpdateWindowSurface(window);
}

If there will be a voiting, then my voice would be for first :)

