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. |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
#define sqr(x) x*x |
#define sqr(x) ((x) * (x)) |
||
#define greet printf(" |
#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(" |
printf("This will demonstrate function and label scopes.\n"); |
||
printf(" |
printf("All output is happening throung printf(), a function declared in the header stdio.h, which is external to this program.\n"); |
||
printf(" |
printf("Enter a number: "); |
||
scanf("%d",&x) |
if (scanf("%d", &x) != 1) |
||
⚫ | |||
switch(x%2){ |
switch (x % 2) { |
||
default: |
|||
printf("Case labels in switch statements have scope local to the switch block.\n"); |
|||
⚫ | |||
case 0: |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
case 1: printf("\nYou entered an odd number."); |
|||
⚫ | |||
goto sayhello; |
|||
case 1: |
|||
⚫ | |||
printf("You entered an odd number.\n"); |
|||
sayhello |
goto sayhello; |
||
jumpin: |
|||
⚫ | |||
⚫ | |||
⚫ | |||
printf("Since you jumped in, you will now be greeted, again!\n"); |
|||
}; |
|||
sayhello: |
|||
greet; |
|||
⚫ | |||
if (x == -1) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
goto jumpin; |
|||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
goto jumpin; |
|||
} |
|||
</lang> |
|||
scram: |
|||
⚫ | |||
return 0; |
|||
⚫ | |||
{{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 |
All output is happening throung printf(), a function declared in the header stdio.h, which is external to this program. |
||
Enter a number |
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. |
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 |
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> |