int age;
double temperature;
char Name[20] = "Maria\0";
By doing this you are declaring what are called global variables. These can be accessed by any function declared in the same file of program code and ,if certain conventions are followed, also by functions declared in other files. At first glance this seems like a good idea, as it easy to pass data between your functions, but it has pitfalls. Without going over the code with a fine tooth comb it's impossible for someone reading your code to know if a particular function alters a particular global.
One way to deal with this is to declare local variables:
int AverageThem(int a, int b)
{
int c;
c = (a + b)/2;
return c;
}
In this pointless snippet of code a,b, and c are all local variables
in AverageThem. Outside of AverageThem those particular storage locations are not associated with the names a,b, and c or the type int. In fact a,b, and c are recreated every time the computer executes the AverageThem function. In C you can declare local variables in every block. A block is anything between two curly "{}" brackets so
main()
{
int a = 2;
{ int a = 3;
printf("%d\n",a);
}
printf("%d\n",a);
}
First prints a "3" and then a "2."
Now suppose you have a different function:
GreetUser(char *Greeting)
{
static char CurrentGreeting[50];
if (Greeting != NULL) {
strcpy(CurrentGreeting,Greeting);
}
puts(CurrentGreeting);
}
Because this function will behave badly if Greeting is longer than fifty characters or is not terminated by a NULL character don't copy it. However, it illustrates the use of the keyword static what static does is cause the value stored in the string CurrentGreeting to persist between in invocations of GreetUser and only change if a non-null pointer is past to the function.
What is this pointer thingy which hasn't been mentioned before? It's a variable which, instead of storing a numerical, character, or object value, stores the location of that value. In C an array is equivalent to a constant pointer. While pointers are useful, and are used a lot C programs, I intend to use C examples to illustrate programming with C-like languages in general so I won't go into C pointers in detail. Puts and strcpy are simply C library functions. They, again, have certain problems and would probably not be used by a contemporary programmer, I'm just using them here for simplicity's sake.
Now object oriented languages have a different approach to deciding who can see information and who can't. While they still have local,global,and static variables. The creator of an object gets to pick what fields and methods are visible to other code. To do this a C++ or Java programmer uses the keywords private,public, and protected.
For example in my last post I created class PetRecord
public class PetRecord {
Date Birthday;
String Name;
String OwnerLastName;
Date RabiesDue;
public void UpdateRabiesDue(Calendar LastShot);
public String GetOwner() {return OwnerLastName;}
public void SetOwner(String NewOwner);
};
The public modifier means that any method could contain a call GetOwner and the other methods I defined. It doesn't whether that method is part of class PetRecord or not. The data fields, however aren't declared public. Therefore they can only be seen by methods that are part of the same Java package as PetRecord. If you don't want anything outside of PetRecord to see them a better choice might be to declare them private.
I have no static methods or fields in the version of PetRecord above. Let's add one.
public class PetRecord {
Date Birthday;
String Name;
String OwnerLastName;
static integer PetCount = 0;
Date RabiesDue;
public void UpdateRabiesDue(Calendar LastShot);
public String GetOwner() {return OwnerLastName;}
public void SetOwner(String NewOwner);
public PetRecord(String theOwner,String theName) {
Owner = theOwner;
Name = theName;
PetCount = PetCount + 1;
}
};
The method I added, called a constructor initializes a PetRecord at the time one is created. The variable PetCount is also incremented. Why do this? Because PetCount is a static variable there is one PetCount shared between all instances of the PetCount class. By incrementing it, we can keep a count of the number of pets we have.
There are also a protected access specifier in Java and C++, but it isn't used as much as public or private.
In my next post I'm going to back up from the micro-level stuff I've been discussing and talk about how you can actually get programs to compile and produce output.
No comments:
Post a Comment