Programming foundations

Stage 1 · Lesson 4 of 9

Functions, parameters and scope

Your first language

A packages a piece of logic under a name. It takes inputs as parameters and hands a result back with return. Printing inside a function is not the same as returning.

A function without return gives back , which is why total = add(2, 3) can end up empty. Variables created inside a function are : they exist only there and disappear when it returns. Where a name can be seen is called its scope.

Reading a global variable from inside a function works. Assigning to one, though, creates a new local unless you write global, which you should rarely need. Learn default arguments (a value used when the caller leaves one out) and (passed by name).

Then learn the classic trap of , such as def add(item, items=[]): that one list is shared between every call. Small functions that return values are what make testing possible later.

A question to check yourself, an AI practice session and 3 cheat sheet entries come when you add the roadmap to your account. Add it

Resources for this stage

Start with· follow one of these

Practise

  • Small exercises in your language track, with free feedback from a human mentor

Keep handy

  • The official tour: read the matching chapter after the course covers a topic

  • Quick syntax lookups and an online compiler for testing a snippet on a shared computer

Mark this stage as not done?