Thread: functions

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    8

    functions

    hello everyone (again)
    for some strange reason, my compiler doesn't compile functions
    of course, it's probably my mistakes

    so i declared the function

    int blah(int blah1, int blah2);

    int main()
    {
    yeah yeah yeah;

    blah(blah1, blah2);
    return 0;
    }

    int blah(int blah1, int blah2)
    {
    the code;
    return 0;
    }

    my code (like this one) will not compile.
    the compiler says :

    passing 'int' to argument 1 of 'blah(int *, int)' lacks a cast.
    I did the functions according to tutorial, and i checked extra carefully so that there are no spelling errors.any help would be greatly appreciated

    thanks

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    Please copy and paste your actual code into the message.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    8
    well... the code is far from being complete, and don't laugh at me! i'm a n00bie

    //Program brought to you by: Derek Kwok
    //Created on July 21, 2003
    //Last Modified on July 21, 2003
    //File name: ttt.cpp

    #include <iostream.h>

    int startgame(int keepcounter[10], int userchoice);

    int main()
    {
    int tic_tac_toe[3][3], userchoice, keepcounter[10], game, counter;

    //Warm welcome message
    cout << "Welcome to Derek's Mega Project #2 - Tic Tac Toe" << endl;
    cout << "ENJOY!" << endl;

    //The tic tac toe board
    cout << endl;
    cout << endl;
    cout << " 1 | 2 | 3 " << endl;
    cout << " ---|---|--- " << endl;
    cout << " 4 | 5 | 6 " << endl;
    cout << " ---|---|--- " << endl;
    cout << " 7 | 8 | 9 " << endl;

    //Reset of the keep counter
    for (counter = 0; counter < 9; counter++)
    {
    keepcounter[counter] = 0;
    }

    startgame(keepcounter[10], userchoice);
    return 0;
    }


    //Start game function
    int startgame(int keepcounter[10], int userchoice)
    {
    cout << endl;
    cout << "Please enter the number where u would like to play at: ";
    cin >> userchoice;

    //check if that place is already played at
    if (keepcounter[userchoice] == 1)
    {
    cout << "Sorry, you can't play there." << endl;
    startgame(keepcounter[10], userchoice);
    }

    else if (keepcounter[userchoice] == 0)
    {
    keepcounter[userchoice] = 1;
    cout << "You have chosen to play at spot #" << userchoice << endl;
    }
    return 0;
    }

  4. #4
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    Someone else will surely say this, so I will get it out of the way... USE [CODE] TAGS!

  5. #5
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Code:
    //Program brought to you by: Derek Kwok
    //Created on July 21, 2003
    //Last Modified on July 21, 2003
    //File name: ttt.cpp
    
    #include <iostream.h>
    
    int startgame(int *keepcounter, int userchoice);
    
    int main()
    {
    	int tic_tac_toe[3][3], userchoice, keepcounter[10], game, counter;
    
    	//Warm welcome message
    	cout << "Welcome to Derek's Mega Project #2 - Tic Tac Toe" << endl;
    	cout << "ENJOY!" << endl;
    
    	//The tic tac toe board
    	cout << endl;
    	cout << endl;
    	cout << " 1 | 2 | 3 " << endl;
    	cout << " --+---+--- " << endl;
    	cout << " 4 | 5 | 6 " << endl;
    	cout << " --+---+--- " << endl;
    	cout << " 7 | 8 | 9 " << endl;
    
    	//Reset of the keep counter
    	for (counter = 0; counter < 9; counter++)
    	{
    		keepcounter[counter] = 0;
    	}
    
    	startgame(keepcounter, userchoice);
    	return 0;
    }
    
    
    //Start game function
    int startgame(int *keepcounter, int userchoice)
    {
    	cout << endl;
    	cout << "Please enter the number where u would like to play at: ";
    	cin >> userchoice;
    
    	//check if that place is already played at
    	if (keepcounter[userchoice] == 1)
    	{
    		cout << "Sorry, you can't play there." << endl;
    		startgame(keepcounter, userchoice);
    	}
    
    	else if (keepcounter[userchoice] == 0)
    	{
    		keepcounter[userchoice] = 1;
    		cout << "You have chosen to play at spot #" << userchoice << endl;
    	}
    	return 0;
    }
    Try not to pass arrays into a function's parameter, instead see if you can use Pointers, and check the pointer's length.

    "You can also pass an array to a function, but in this case the array is not copied, even though a pass-by-value method of passing arguments still applies. The array name is converted to a pointer, and a copy of the pointer to the beginning of the array is passed to the function. This is quite advantageous, as copying large arrays could be very time consuming." --C++ Tutorial from Wrox

    also whenever you use arrays think of them as a chain of pointers. And that using the []s derefrences the pointer but leaving them out is like using the pointer without the *.

    Which is why you cant call a function like
    startgame(myarray[10],blah);
    lets say the tenth number in myarray was 17
    what your really doing is something like:
    startgame(17,blah);
    and your not telling the function where the array is.

    But as a general note, try to avoid passing arrays into functions, it avoids confusion



    -LP
    Last edited by Lynux-Penguin; 07-21-2003 at 12:39 AM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    8
    i'm still not quite getting it
    about pointers and stuff..

    so if i were to pass an array to a function, i should use a pointer instead

    something like this?

    int blah(int *pointer);

    int main()
    {
    int x[10];
    int *pointer;

    pointer = &x;

    blah(*pointer);
    return 0;
    }

    int blah(int *pointer)
    {
    cout << *pointer;
    return 0;
    }


    would this code work?
    pointers are too confusing. -__-;;

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    8
    THANKS SO MUCH! >_<
    i understand it at last

    i can't believe i didn't get something so simple =D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM