Printing multiple values with a function not working?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Zakuree
extends Node2D

var fN = ["Michael ", "Malcolm ", "Mason ", "Matthew ", "Marty ", "Marshall "]
var lN = ["Green", "Gatry", "Gunther", "Grey", "Geller", "Galloway"]

func genPlayers(amount):

	for n in range(amount):
		var player = {
			"name": fN[randi() % fN.size()] + lN[randi() % lN.size()],
			"age": randi()%10+18
		return player

func _ready():

As you can see here, I’m trying to randomize 15 random people, with a randomized name and age. Using genPlayers(15)I call for a randomized name and age 15 times. However, when it gets printed from print(genPlayers(15)), it only prints one person:

enter image description here

Why is this happening? If I do print(player) from inside the function, it prints all 15. But returning the player value and then printing it in the _ready() function is only printing 1.

:bust_in_silhouette: Reply From: DDoop

Return causes a function to cease operating and go out of scope after handing off the value it’s returning.

Consider wrapping your actual “generator” in a function that repeatedly calls the generator based on an int value you feed to the wrapper function.
EDIT: you would need to initialize an empty array at the top of the wrapper, then push_back each person generated to the array. Return the array.