0% found this document useful (0 votes)
3 views2 pages

Discussion Assignment Unit 2

The document explains the concept of functions in programming, focusing on parameters and arguments. It highlights the scope of variables, emphasizing that variables defined within a function are not accessible outside of it, and discusses the behavior of global and local variables when they share the same name. Additionally, it references a source for further reading on global and local variables in Python.

Uploaded by

sanskritisri27
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Discussion Assignment Unit 2

The document explains the concept of functions in programming, focusing on parameters and arguments. It highlights the scope of variables, emphasizing that variables defined within a function are not accessible outside of it, and discusses the behavior of global and local variables when they share the same name. Additionally, it references a source for further reading on global and local variables in Python.

Uploaded by

sanskritisri27
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Example 1:

The given image defines a sum function with a and b as its parameters. It returns the value
we get after adding a and b.

In the function definition, a and b are the parameters. They are placeholders for values that
will be defined when the function is called.

In the function call, 2 and 3 are the arguments, they are the values that are defined in the
function to process and return an output.

Example 2:

Example 3:

Trying to access a variable that exists within a function throws an error because that variable
is only defined within the scope of that function (Downey, 2015). The memory allocated to
the variable is released when the function finishes executing, which is why it is not
accessible outside the function’s block.

Example 4:

In the given screenshot, I tried to access the unique parameter outside the function block by
creating a variable with the same name. As the output shows, parameter names can be
reused outside the function as independent variables. If I had not defined a variable named
`unique_parameter` outside the function, the result would have been similar to Example 3
and would have produced a `NameError`.

Example 5:

In the output, as shown in the above screenshot, we can see that the variable (name) within
the function prints the value assigned inside the function's scope (Local name), and that
assigning a value globally to the variable yields the global value (Global name). This happens
because when a global and local variable share the same name, the local one becomes the
priority in the local scope, i.e overshadows the global variable within the scope of the
function , but the global variable remains accessible outside the function (GeeksforGeeks,
n.d.).

GeeksforGeeks. (n.d.). Global and local variables in Python. GeeksforGeeks.


[Link]

You might also like