Scope/Function names and labels: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed indentations.)
(→‎{{header|C}}: Fixed the worst of the C errors. This is not the competition page for the ugliest code.)
Line 88: Line 88:
Demonstrating function scope as well as goto in a C program invariably leads to code like the one below. The [http://en.wikipedia.org/wiki/Scope_(computer_science)#C Wikipedia article] is a good starting point.
Demonstrating function scope as well as goto in a C program invariably leads to code like the one below. The [http://en.wikipedia.org/wiki/Scope_(computer_science)#C Wikipedia article] is a good starting point.


<lang C>/* Abhishek Ghosh, 8th November 2013, Rotterdam */
<lang C>
#include <stdio.h>
/*Abhishek Ghosh, 8th November 2013, Rotterdam*/
#include<stdio.h>


#define sqr(x) x*x
#define sqr(x) ((x) * (x))
#define greet printf("\nHello There !");
#define greet printf("Hello There!\n")


int twice(int x)
int twice(int x)
{
{
return 2*x;
return 2 * x;
}
}


int main()
int main(void)
{
{
int x;
int x;

printf("\nThis will demonstrate function and label scopes.");
printf("This will demonstrate function and label scopes.\n");
printf("\nAll output is happening throung printf(), a function declared in the header file stdio.h, which is external to this program.");
printf("All output is happening throung printf(), a function declared in the header stdio.h, which is external to this program.\n");
printf("\nEnter a number : ");
printf("Enter a number: ");
scanf("%d",&x);
if (scanf("%d", &x) != 1)
return 0;
switch(x%2){
switch (x % 2) {
default:
default:printf("\nCase labels in switch statements have scope local to the switch block.");
printf("Case labels in switch statements have scope local to the switch block.\n");
case 0: printf("\nYou entered an even number.");
case 0:
printf("\nIt's square is %d, which was computed by a macro. It has global scope within the program file.",sqr(x));
printf("You entered an even number.\n");
break;
printf("Its square is %d, which was computed by a macro. It has global scope within the translation unit.\n", sqr(x));
case 1: printf("\nYou entered an odd number.");
break;
goto sayhello;
case 1:
jumpin: printf("\n2 times %d is %d, which was computed by a function defined in this file. It has global scope within the program file.",x,twice(x));
printf("\nSince you jumped in, you will now be greeted, again !");
printf("You entered an odd number.\n");
sayhello: greet
goto sayhello;
jumpin:
if(x==-1)goto scram;
printf("2 times %d is %d, which was computed by a function defined in this file. It has global scope within the translation unit.\n", x, twice(x));
break;
printf("Since you jumped in, you will now be greeted, again!\n");
};
sayhello:
greet;
printf("\nWe now come to goto, it's extremely powerful but it's also prone to misuse. It's use is discouraged and it wasn't even adopted by Java and later languages.");
if (x == -1)
goto scram;
if(x!=-1){
break;
x = -1; /*To break goto infinite loop.*/
}
goto jumpin;

}
printf("We now come to goto, it's extremely powerful but it's also prone to misuse. Its use is discouraged and it wasn't even adopted by Java and later languages.\n");

scram: printf("\nIf you are trying to figure out what happened, you now understand goto.");
if (x != -1) {
return 0;
x = -1; /* To break goto infinite loop. */
}
goto jumpin;
}
</lang>

scram:
printf("If you are trying to figure out what happened, you now understand goto.\n");
return 0;
}</lang>


{{out}} Example run:
{{out}} Example run:
<pre>
<pre>
This will demonstrate function and label scopes.
This will demonstrate function and label scopes.
All output is happening throung printf(), a function declared in the header file stdio.h, which is external to this program.
All output is happening throung printf(), a function declared in the header stdio.h, which is external to this program.
Enter a number : 5
Enter a number: 5


You entered an odd number.
You entered an odd number.
Hello There !
Hello There!
We now come to goto, it's extremely powerful but it's also prone to misuse. It's use is discouraged and it wasn't even adopted by Java and later languages.
We now come to goto, it's extremely powerful but it's also prone to misuse. Its use is discouraged and it wasn't even adopted by Java and later languages.
2 times -1 is -2, which was computed by a function defined in this file. It has global scope within the program file.
2 times -1 is -2, which was computed by a function defined in this file. It has global scope within the translation unit.
Since you jumped in, you will now be greeted, again !
Since you jumped in, you will now be greeted, again!
Hello There !
Hello There!
If you are trying to figure out what happened, you now understand goto.
If you are trying to figure out what happened, you now understand goto.
</pre>
</pre>