Thread: Why doesn't this function take in any parameters?

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    3

    Question Why doesn't this function take in any parameters?

    Hello! I'm working on the game of fifteen from the CS50's Pset3. I'm very confused why the function that initializes the board doesn't take in any parameters. Why doesn't it take the size of the board (d) as its parameter?

    Code:
    /** * Initializes the game's board with tiles numbered 1 through d*d - 1
     * (i.e., fills 2D array with values but does not actually print them).
     */
    
    void init(void)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Presumably because all the variables are global.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2019
    Posts
    3
    Oh, I see... so if the variable as global there's no need to specify it as a parameter?

    Here's the whole code, maybe it'll be clearer

    Code:
    /** * fifteen.c
     *
     *
     * whereby the board's dimensions are to be d x d,
     * where d must be in [DIM_MIN,DIM_MAX]
     */
    
    
    #define _XOPEN_SOURCE 500
    
    
    #include <cs50.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    
    // constants
    #define DIM_MIN 3
    #define DIM_MAX 9
    
    
    // board
    int board[DIM_MAX][DIM_MAX];
    
    
    // dimensions
    int d;
    
    
    // saved locations of the blank tile
    int blank_row;
    int blank_col;
    
    
    // prototypes
    void clear(void);
    void greet(void);
    void init(void);
    void draw(void);
    bool move(int tile);
    bool won(void);
    void swap(int *a, int *b);
    void print_grid_row(int d);
    void print_tile(int tile);
    
    
    int main(int argc, string argv[])
    {
        // ensure proper usage
        if (argc != 2)
        {
            printf("Usage: fifteen d\n");
            return 1;
        }
    
    
        // ensure valid dimensions
        d = atoi(argv[1]);
        if (d < DIM_MIN || d > DIM_MAX)
        {
            printf("Board must be between %i x %i and %i x %i, inclusive.\n",
                DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX);
            return 2;
        }
    
    
        // open log
        FILE *file = fopen("log.txt", "w");
        if (file == NULL)
        {
            return 3;
        }
    
    
        // greet user with instructions
        greet();
    
    
        // initialize the board
        init();
    
    
        // accept moves until game is won
        while (true)
        {
            // clear the screen
            clear();
    
    
            // draw the current state of the board
            draw();
    
    
            // log the current state of the board (for testing)
            for (int i = 0; i < d; i++)
            {
                for (int j = 0; j < d; j++)
                {
                    fprintf(file, "%i", board[i][j]);
                    if (j < d - 1)
                    {
                        fprintf(file, "|");
                    }
                }
                fprintf(file, "\n");
            }
            fflush(file);
    
    
            // check for win
            if (won())
            {
                printf("ftw!\n");
                break;
            }
    
    
            // prompt for move
            printf("Tile to move: ");
            int tile = get_int();
    
    
            // quit if user inputs 0 (for testing)
            if (tile == 0)
            {
                break;
            }
    
    
            // log move (for testing)
            fprintf(file, "%i\n", tile);
            fflush(file);
    
    
            // move if possible, else report illegality
            if (!move(tile))
            {
                printf("\nIllegal move.\n");
                usleep(500000);
            }
    
    
            // sleep thread for animation's sake
            usleep(50000);
        }
    
    
        // close log
        fclose(file);
    
    
        // success
        return 0;
    }
    
    
    /**
     * Clears screen using ANSI escape sequences.
     */
    void clear(void)
    {
        printf("\033[2J");
        printf("\033[%d;%dH", 0, 0);
    }
    
    
    /**
     * Greets player.
     */
    void greet(void)
    {
        clear();
        printf("WELCOME TO GAME OF FIFTEEN\n");
        usleep(2000000);
    }
    
    
    /**
     * Initializes the game's board with tiles numbered 1 through d*d - 1
     * (i.e., fills 2D array with values but does not actually print them).
     */
    void init(void)
    {
    
    
    }
    
    
    /**
     * Prints the board in its current state.
     */
    void draw(void)
    {
    
    
    }
    
    
    /**
     * If tile borders empty space, moves tile and returns true, else
     * returns false.
     */
    bool move(int tile)
    {
        return false;
    }
    
    
    /**
     * Returns true if game is won (i.e., board is in winning configuration),
     * else false.
     */
    bool won(void)
    {
        // TODO
        return false;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by oiseau_de_coco
    Oh, I see... so if the variable as global there's no need to specify it as a parameter?
    Yes. I'm a bit disappointed at CS50 though: in this scenario it is true that the program is both trivial enough and unlikely to be reusable such that global variables are fine, but at the same time it's not a good idea to inculcate the habit of having global variables.
    Last edited by laserlight; 05-05-2019 at 05:40 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2019
    Posts
    3
    Salem, laserlight, thank you for the explanation!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-02-2016, 07:12 AM
  2. Function with from 1 to 3 parameters
    By bremenpl in forum C Programming
    Replies: 15
    Last Post: 05-31-2013, 09:36 AM
  3. Function parameters
    By wart101 in forum C++ Programming
    Replies: 2
    Last Post: 11-21-2006, 04:22 AM
  4. Function parameters?
    By Munkey01 in forum C++ Programming
    Replies: 12
    Last Post: 01-02-2003, 08:44 PM
  5. Function Parameters
    By Soopafly in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2002, 02:46 AM

Tags for this Thread