Stage 3 · Lesson 8 of 9
What happens when you pass a value to a function
This connects memory to code you write every day. When you pass a number to a , the function gets its own copy. When you pass a list or object in Python, JavaScript, Java or C#, the function gets a copy of the reference, so both names point to the same heap object.
This is sometimes called , and two names for one object is called . Appending inside the function changes the caller's list. But assigning a new list to the parameter does not affect the caller at all.
C++ copies the whole object by default, unless you pass a reference or a . Knowing which case applies explains most "my data changed" and "my change disappeared" bugs. It is also one of the things the milestone asks you to write up.
A question to check yourself, an AI practice session and 2 cheat sheet entries come when you add the roadmap to your account. Add it
Resources for this stage
Start with· follow one of these
- CourseFreeCS50: Introduction to Computer Science
Harvard University · Beginner
Weeks 0, 1, 2 and 4 teach C, arrays and memory; the difficulty is the point
- VideoFreeCrash Course Computer Science
CrashCourse · Beginner
Short free videos from logic gates to operating systems; episodes 1 to 20
Keep handy
Also worth your time
- What Happens WhenGo deeper
Everything from a key press to a rendered page; read it after this stage