From 02e7a2c58cc1b26b0391455070832a5fe62f707e Mon Sep 17 00:00:00 2001 From: christinastampfer Date: Mon, 23 Feb 2026 15:30:54 +0100 Subject: [PATCH 1/2] Update functions.md New section on global versus local variables --- docs/python/functions.md | 68 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/docs/python/functions.md b/docs/python/functions.md index a334d2b2..9e19105c 100644 --- a/docs/python/functions.md +++ b/docs/python/functions.md @@ -320,6 +320,73 @@ result = square_number(5) function for each subtask and then combine them to solve the problem as a whole. +## Detour: Scope of a variable + +The scope refers to the region of a program where variables are defined and can be accessed. Local variables, such as those declared inside a function, are only available within that function. In contrast, variables defined in the main body of the program are called global variables and can be accessed in any part of the program. + +The following example demonstrates that a local variable created inside a function can be used within that function. + +```py +def greet_user(): + inside_user_name = "Max Mustermann" + print(f"Hello, {inside_user_name}!") + +greet_user() +``` + +```title=">>> Output" +Hello Max Mustermann! +``` + +The local variable `inside_user_name` exists only within the function otherwise an error occurs. + +```py +def greet_user(): + inside_user_name = "Max Mustermann" + +greet_user() +print(f"Hello, {inside_user_name}!") +``` + +```title=">>> Output" +NameError: name 'inside_user_name' is not defined +``` + +Therefore, attempting to print the local variable in the main body results in a `NameError`. +Global variables definied in the main body of the program can also be used within functions. + +```py +def greet_user(): + print(f"Hello, {outside_user_name}!") + +outside_user_name = "Maximilian Muster" +greet_user() +``` + +```title=">>> Output" +Hello Maximilian Muster! +``` + +Modifying a global variable inside a function does not overwrite the variable's value. + +```py +def greet_user(): + outside_user_name = "change name" + +outside_user_name = "Maximilian Muster" +greet_user() +print(f"Hello, {outside_user_name}!") +``` + +```title=">>> Output" +Hello Maximilian Muster! +``` + +Within the function, a new local variable with the same name is defined, but it has no effect on the value of the global variable. + +The best way to avoid mistakes is to pass data into and out of functions using arguments and return values. + + ## Recap This section introduced the concept of functions to better structure your @@ -332,3 +399,4 @@ code, make it more readable and reusable. We have covered: - Defining default values for parameters - The `#!python return` statement - How to use functions to solve smaller subtasks and structure your code +- Detour on global versus local variables From eeaa21edcd6063edcd96cc057d9a1ee569550289 Mon Sep 17 00:00:00 2001 From: christinastampfer Date: Tue, 24 Feb 2026 09:35:57 +0100 Subject: [PATCH 2/2] Update docs/python/functions.md Co-authored-by: Jakob Klotz --- docs/python/functions.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/python/functions.md b/docs/python/functions.md index 9e19105c..b6ff232a 100644 --- a/docs/python/functions.md +++ b/docs/python/functions.md @@ -384,7 +384,21 @@ Hello Maximilian Muster! Within the function, a new local variable with the same name is defined, but it has no effect on the value of the global variable. -The best way to avoid mistakes is to pass data into and out of functions using arguments and return values. +???+ tip + + The best way to avoid mistakes is to pass data into and out of functions + using arguments and return values. + + ```py + def greet_user(user_name): # accept an argument + greeting = f"Hello, {user_name}!" + + return greeting # explicit return + + # pass data in, without a global variable + result = greet_user("Maximilian Muster") + print(result) + ``` ## Recap