Have you looked into point filters? That would be much easier than writing a function.
Select the green block and the insertion grip. It asks for a destination point. Type .Y and give it the insertion point of the red block. That takes the Y coordinate of your point and lets you then specify the X and Z. Drag down the green block (the block's X is assumed) and place it.
If you have to do that a thousand times, yes, write a function. If you only have to do it once, try the filter.