lesson

Separate the algorithm from its syntax

1 · Learn

The algorithm 'return the input multiplied by itself' can be expressed in different languages. Python uses def square(n): return n * n for this one-line example. JavaScript can use function square(n) { return n * n; }.

Syntax differs, but the intended input-output relationship is the same for the numeric examples here. Type conversion and edge cases can differ between languages, so do not assume all superficially similar programs behave identically.

Shared algorithmPython syntaxJavaScript syntaxTest assumptions
Key words and supplied examples.

2 · Worked example

For numeric input 4, both examples return 16. That agreement is a test case, not proof that every type of input is handled identically.

3 · Your turn

What shared numeric output should both functions return for input 0?

Check your answer

0.

4 · Apply your learning

Implement the same small numeric function in two approved environments and record equivalent and different language rules.

Adult guidance and safety

Use an approved sandbox with invented data. Never ask for real passwords, personal records or access to another person's system. These text examples need executable projects and assessed evidence in two languages; reading code alone is not practical mastery.

Practice this lesson

Lesson resources

Browse the available practice activities and worksheets for this lesson.

Open course resources
Exercise preview