We read from the 1st line at the top of the page going down, line by line.
A computer also processes Code in the same order.
(I’m not being funny, just making a point).
Coding is a set of instructions for the computer to follow, from the top down.
Let me give an example from every day life:-
If you go to the shop to buy fresh milk, 18 eggs and brown bread, what do you do?
Go to the shop
Buy the fresh milk, 18 eggs and brown bread.
Return home (with the fresh milk, 18 eggs and brown bread).
What you are telling your function to do is:-
Go to the shop - function onSame()
Return home (with milk, eggs and bread) - return[loc, forn, ref] … but what milk, eggs and bread . . . you did not buy anything yet!!!
Buy fresh milk, 18 eggs and brown bread - const loc, const forn and const ref (I’m not going to copy all that stuff…)
When you return home, you are NO LONGER IN THE SHOP, therefore you cannot buy anything after you have RETURNED HOME. In the same way, once a Function RETURNS a value, nothing else is executed in that function. It’s like leaving the building. It cannot do any of the lines that follow the RETURN statement.
Your values are assigned in the following lines, but they do not happen, because they are After the Return statement.
Did you come right. Sorry I have not been on the forum for a while.
Can you send the code that calls these 2 functions pls.
Is the ‘onSame’ function called before the ‘duplicar’ function?
Also, the loc, forn & ref variables are not passed to the ‘onSame’ function. So it would modify the global variables of the same name, or those variables in the function that calls ‘onSame’.
Do you understand the SCOPE of variables?
If a program is like a filing cabinet( let’s name it MainCabinet), then:-
Global variables are visible to all drawers and hanging files in MainCabinet.
A function (let’s name it FnDrawer), called from MainCabinet is like a drawer in the filing cabinet.
A function’s variables are visible to all hanging files in the drawer (FnDrawer) in which it resides.
A function (let’s name it SubFunction), called from FnDrawer is visible to itself and any functions it calls. You could think of envelopes inside the hanging files, for the functions called by SubFunction.
A hanging file in the top drawer cannot access or modify variables in the 2nd drawer.
A hanging file in a drawer can only show it’s variables to the drawer by RETURNING them (which you have done in ‘onSame’). But, which variables are being changed by ‘onSame’? No parameters are being passed to it, so it is modifying the variables from it’s calling function (or global, if you made those 3 variables global).
Check the scope of your variables in your functions.
Tip: you can change the loc to loc1 in a certain function, just to clarify in your mind, which loc you are referring to (same with the other 2 variables).
loc1 = firstvalue; forn1 etc
loc2 = all that good stuff (don’t change the good stuff, okay)
loc = all that other good stuff (as required in your challenge)
In ‘duplicar’, you passed the 3 variables to it as loc, forn and ref. So no change needed there.
In ‘onSame’, nothing is passed. Just saying. I think I’m repeating myself now. I’ll let you have a go at it . . . .