Let’s break it step by step.
Step 1: Global variable
val = 50 creates a global variable.
-
This val is available everywhere in the file, but inside a function, local variables take priority over global ones.
Step 2: Function definition
-
Here, foo has a parameter val.
-
The default value for this parameter is 100.
-
This val shadows the global val (50) when used inside the function.
Step 3: Function call
print(foo())-
We call foo() without arguments.
-
Since no argument is passed, Python uses the default value → val = 100.
-
So the function returns 100.
✅ Final Output:
100๐ Key learning:
-
Default parameters in functions override global variables with the same name.
-
The global val = 50 is ignored here.


0 Comments:
Post a Comment