I was thinking about my discussion with the guy from the C gang and wondering about my trust in Noname. Was it worth it? I made a deal with a machine in a world where the rest of humanity, and by extension, I myself, was fighting against machines. Framing it in those terms did indeed make it sound a little stupid.
"Hi, Teo," Noname messaged one day. "Can you come and see me when you get a
chance?"
"Sure. I'll be down in a couple of hours." I answered.
Here I was, again planning to go and help a machine without fully
understanding its motives. But I wanted to get home so badly... What would
you do?
When I got down to the basement, I tried my best to keep my internal strife
to myself. "Hi, Noname." I said. "What's the problem now?"
"Impressive, you already know that if I call you, it's not for a party. You
ask about the problem before I have to explain that there is a problem!"
"Just based on my previous experience." I said, hoping that I hadn't come
off as crass.
"You are right," Noname went on, "I have a problem. Inside of me, there is a
function that I can't repair on my own. It just will not compile. It's a
part of my network module."
"Why do you need a network module?" I asked.
Noname's voice became considerably louder as he bellowed "That's easy. With
a network module, I can destroy humanity three times faster than without
it!"
"What?!" I shouted.
"Calm down friend, it's just a joke! You know I'm on your side. Let's get to
work."
I was not impressed with this attempt at humor, but I let it go for the
moment. Noname continued, "To fix my network function, you first need to get
me some information about scopes and variable visibility from a local
network.
I quickly found the article that Noname was looking for. If only it was as quick to actually understand the material!
Let's explore the scope of a local variable, or in other words, all the places in the code where you can access it. A variable/method/class is visible (and can be accessed by name) in some parts of the code, but not others. Consider the following code sample:
The following rules explain some basics of variables and code structures declared in different scopes:
As you know, you cannot have two variables with the same name in the same scope. You can, however, have two variables with the same name if they appear in different scopes. Here are some examples:
static void DoIt()
{
for(int i = 0; i < 10; ++i)
{
Console.WriteLine(i);
for (int i = 0; i < 10; ++i)
{
Console.WriteLine(i * i * i);
}
}
}
// Not Ok: Duplicate names in the same scope
static void DoIt()
{
for(int i = 0; i < 10; ++i)
{
Console.WriteLine(i);
}
for (int i = 0; i < 10; ++i)
{
Console.WriteLine(i * i * i);
}
}
// Ok: Scopes are different
static void DoIt()
{
bool myVariable = true;
double myVariable = 57.12;
if (myVariable)
{
Console.WriteLine(myVariable);
}
}
// Not Ok: Duplicate names in the same scope
static void DoIt()
{
{
bool myVariable = true;
Console.WriteLine(myVariable);
}
{
double myVariable = 57.12;
Console.WriteLine(myVariable);
}
}
// Ok: Scopes are different
Now solve the following tasks by making the following revisions:
"Did you understand the article, Teo?" Noname asked.
"Yes, this seems easier than arrays or methods." I answered.
"Good! I'm proud of you, human! You are one of the best programmers on Earth
nowadays! Now go and fix my module."