Click here to Skip to main content
15,891,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys

this might be too basic, but i really find it hard to understand about return in function(i kinda understand it in a way, but for some cases its confusing), lets say i make a function to to generate a 2D array with randomized elements, and then i need to print and use that function in the main, what should i return? given there is no parameters that i throw from the main, i just want to use this function in the main

Here is my code:

What I have tried:

public class Main {

    public static void main(String[] args) {
        Main main = new Main();


        System.out.println(main.map());

    }

    public int map(){
        int[][] grid = new int[10][10];
        int i=0;
        int j=0;
        int max=0;

        for(i =0; i<grid.length; i++){
            for(j = 0; j<grid[i].length; j++){
                grid[i][j] = ((int)Math.random()*10);
                System.out.print(grid[i][j]);
            }
            System.out.println("");
        }

        /*ignore this part
        if(max > grid[i][j])
        {
            max = grid[i][j];
        }
        System.out.println("");

        for(i=0; i<grid.length; i++){
            for(j=0; j<grid.length; j++){
                if(grid[i][j] == max){
                    int a = grid[i][0];
                    int b = grid[0][i];
                    System.out.println("["+a+"]"+"["+b+"]");
                }
            }
        }
        */
        return 0;
    }

}
Posted
Updated 31-Oct-19 22:30pm

1 solution

YOu've defined the function map as returning an integer value - so that is all it can pass back to the caller when it finishes. If you want it to return more data, you need to change the way it's declared and return a different datatype.

Think of like this: you go to the cinema and then want $15 per person. You hand them a $20 note, and they return $5 and let you in to see the movie.
That's a fucntion returning an integer:
Java
public int pay movie(int payment, int persons) {
   return payment - (persons * 15);
   }
But if you go to the supermarket, you expect the shop to let you leave with a bag of groceries and your change - and that doesn't fit in an integer value at all, so you need to return a class instance that holds an array of products, and an integer change value.

If you want your map function to pass a grid back to what called it, then you need it to return an array:
Java
public int[][] map() {
   ...
   return grid; 
   }
And change your code that calls it to handle a different return type.
 
Share this answer
 
Comments
chandra liquidzey rapier 1-Nov-19 5:00am    
Okay, thanks for the explanation, i didnt know you can even make a function for 2Darray like that,
but now the problem is, if i were to return the grid that i had randomized the element inside a for loop
how do i return that grid, with the elements that was filled in that loop? because when i return grid[i][j] or grid, its only zero because i can't use the i and j outside the loop
OriginalGriff 1-Nov-19 5:14am    
No you can't - but if you return the whole array to the caller, then it can use it's own i and j to access the array in any way it needs to.
Think about it: "map" doesn't know what the caller is going to do with the grid, so it has no idea what i or j values it should return anyway, and there is no point in creating and populating a grid inside the function if you don't pass the whole thing back to the caller when it's done.

What I would do is make the map function take two integer parameters which specify the x and y size of the grid (instead of using the fixed sizes (10, 10) you do) and return the whole grid that size for the caller to do what they will with.

Make sense?
chandra liquidzey rapier 1-Nov-19 5:26am    
I kinda understand, but i dont know where to start :(
and i dont know how to pass it, maybe you can make an example?
OriginalGriff 1-Nov-19 5:32am    
public int[][] map(int x, int y) {
   ...
   return grid;
   }


And then
int[][] grid = map(10, 20);
chandra liquidzey rapier 1-Nov-19 5:38am    
i wanted to pass my code here but it doesnt let me because a bit too long

soo what i did is according to you, i made the function like this

public int map(int[][] grid, int i, int j)

i defined the grid[][] size based on i and j, that i declared in the main, is that the same thing?
but it still doesnt print out the grid with randomized elements that was being for looped in the function

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900