Two developers implemented the same logic, one in Python and one in Java. One was surprised that a value changed inside a function also changed at the place where the function was called. The other said it does not.
Neither of them wrote the code incorrectly. Each language has its own
rules for passing values
.
This post covers the classification, data types, variable scope, parameter passing, and control structures you need when you use a programming language. The goal is not just to memorize the concepts, but to see how each language's rules show up in real code.
Programming languages are grouped into families according to how code is written.
Each family looks at problems from a different angle, so the same feature can be expressed in code that looks quite different.
Family
Characteristics
Representative languages
Procedural
Describes the execution steps one by one
C
Object-oriented
Groups data and behavior into objects
Java, C++
Functional
Centers on combining functions and on data that does not change
Haskell
Declarative
Describes the desired result and leaves the process to the system
SQL
Scripting
Runs quickly, and often decides data types while the program runs
Python, JavaScript
These families do not have sharp boundaries, so one language often has traits from several of them.
In real code,
A data type defines what kind of value something is and which operations you can perform on it.
The integer 10 and the string "10" look similar, but adding them gives different results.
is called statically typed.
A language that checks types at
run time
is called dynamically typed.
Java requires you to declare the type of a variable, so it is statically typed.
Python fixes a variable's type when a value is assigned to it, so it is dynamically typed.
๐
Dynamically typed languages are quick to write, but a wrong type can surface only while the program runs.
Check values that may change type at the point where they come in.
Scope is the region of code in which a variable can be read and written.
Global variables are visible throughout the program, while local variables are visible only inside the function or block where they are declared.
A global region contains the local region of function A and a block region, while function B cannot see the local variables of function A
Languages also differ in how they pass values into a function.
With
call by value
, the value is copied before it is passed, so changes inside the function do not affect the caller.
With
call by reference
, the location where the value is stored is passed, so changes inside the function appear in the original data.
With call by value, only the copied parameter changes and the caller's x stays the same. With call by reference, the same array changes and the caller sees the change
Python passes references to objects by value.
So a mutable object such as a list reflects changes made inside a function in the original list, while an immutable value such as an integer does not.
Languages alone do not handle every task.
A library is a collection of code that bundles frequently used features, and an API is the agreement for calling those features.
A framework provides the overall shape of the execution flow, so developers insert their code into the places it defines.
Using a language well includes
reading the documentation for these tools and following their calling rules exactly
data types, variable scope, and the rules for passing values
before you try to memorize syntax.
The reason the same code behaves differently in different languages comes down to differences in these rules.
You will meet the same concepts while studying for the Engineer Information Processing exam, subject 4, so predicting the output of code and then running it to check is good practice.