Calling a function within a function

This is a discussion on Calling a function within a function within the C++ Programming forums, part of the General Programming Boards category; Hi, Im trying to call a function within a function but the 2nd function isnt returning anything. The code compiles ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    38

    Calling a function within a function

    Hi,

    Im trying to call a function within a function but the 2nd function isnt returning anything.
    The code compiles fine.

    Code:
    void playerMove(char playerInput,int playerRow,int playerCol,int theGrid[][10])
    {
    	getPlayerPos(playerRow,playerCol,theGrid);
    }
    its the function getPlayerPos that isnt returning any values.but if I call the getPlayerPos outside of playerMove it works.
    Code:
    int getPlayerPos(int& playerRow,int &playerCol,int theGrid[][10])
    {
    	for(int row=0;row<10;row++){
    		for(int col=0;col<10;col++){
    			if(theGrid[row][col]==1){
    				playerRow=row;
    				playerCol=col;
    				return playerRow,playerCol;
    			}
    		}
    	}
    }
    Any ideas?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    1) You cannot return two files - only one.
    2) If the if statement is never true, the function returns nothing resulting in undefined behavior.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Code:
    void func1 (int &a) {
      func2 (a);
    }
    
    void func2 (int &a) {
      a = 3;
    }
    pass the arguments to 'playerMove' as references aswell if you want to be able to change their values.

    Also:
    Code:
    return playerRow,playerCol; /* this is the same as 'return playerCol;' */

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    38
    Ok I understand you both.I see where I was goin wrong.

    I know that there is always going to be a 1 value in the array(well there should be!)
    I take it that its a bad practice to to leave that if statement open ended should I always qaulify it with an else statement?

    thanks for the speedy reply's!

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    You should have a return at the end of the function to cover the cases if no match was found.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    38
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM
  5. Calling a user-defined function in C++
    By brianptodd in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2002, 12:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21