Download presentation
Presentation is loading. Please wait.
Published byHorace Richardson Modified over 8 years ago
1
Lecture 7: Variable Scope B Burlingame March 16, 2016
2
Announcements Midterm Today – next Tuesday Homework #3 due up front Homework #4 due after break
3
Learning Objectives Discuss the importance of style in code Define scope and variable duration
4
Programming Style The layout of program directives using white space, syntax, & punctuation to visually highlight a programmer’s intention Good Style is Consistent – A given structure always looks the same Illustrative – The intent of the code is shown by the grammar Clear – Any reasonable programmer should be able to follow the intent
5
Programming Style Why does it matter? Reduces bugs – the human eye is excellent at detecting patterns Reduces development time Allows others to understand how to approach your code Style is a fundamental part of code documentation https://en.wikipedia.org/wiki/Indent_style 9 different style documented
6
No style #include "stdio.h" #include "math.h" int main() { double time, max_height, velocity_at_apex, time_at_apex,height,velocity = 0; printf( "%4s %10s %14s\n", "time", "height (m)", "velocity (m/s)" ); printf( "%4s %10s %14s\n", "----", "----------", "--------------" ); for( time = 0.0; time < 49.0; time = time + 0.01 ) { height = -0.12 * pow( time, 4.0 ) + 12.0 * pow( time, 3.0 ) - 380.0; velocity = (-0.48 * pow( time, 3.0 ) + 36.0 * pow( time, 2.0 ); if( height > max_height ) { max_height = height; velocity_at_apex = velocity; time_at_apex = time; } } printf( "Max height %.2lf, at %.3lf hours, while moving %.3lf m/s\n",max_height, time_at_apex, velocity_at_apex); return(0); }
7
K&R Style #include "stdio.h" #include "math.h" int main() { double time = 0; double max_height = 0; double velocity_at_apex = 0; double time_at_apex = 0; double height,velocity = 0; printf( "%4s %10s %14s\n", "time", "height (m)", "velocity (m/s)" ); printf( "%4s %10s %14s\n", "----", "----------", "--------------" ); for( time = 0.0; time < 49.0; time = time + 0.01 ) { height = -0.12 * pow( time, 4.0 ) + 12.0 * pow( time, 3.0 ) - 380.0; velocity = (-0.48 * pow( time, 3.0 ) + 36.0 * pow( time, 2.0 ); if( height > max_height ) { max_height = height; velocity_at_apex = velocity; time_at_apex = time; } printf( "Max height %.2lf, at %.3lf hours, while moving %.3lf m/s\n",max_height, time_at_apex, velocity_at_apex); return(0); }
8
Allman Style/BSD Style #include "stdio.h" #include "math.h" int main() { double time = 0; double max_height = 0; double velocity_at_apex = 0; double time_at_apex = 0; double height,velocity = 0; printf( "%4s %10s %14s\n", "time", "height (m)", "velocity (m/s)" ); printf( "%4s %10s %14s\n", "----", "----------", "--------------" ); for( time = 0.0; time < 49.0; time = time + 0.01 ) { height = -0.12 * pow( time, 4.0 ) + 12.0 * pow( time, 3.0 ) - 380.0; velocity = (-0.48 * pow( time, 3.0 ) + 36.0 * pow( time, 2.0 ); if( height > max_height ) { max_height = height; velocity_at_apex = velocity; time_at_apex = time; } printf( "Max height %.2lf, at %.3lf hours, while moving %.3lf m/s\n",max_height, time_at_apex, velocity_at_apex); return(0); }
9
Identifiers and Scope Identifier The name of a variable, function, label, etc. int my_var1;/* a variable */ pow_table();/* a function */ start:/* a label */ Question: Does it make a difference where in a program an identifier is declared? YES! --> concept of ‘scope’
10
Scope of Identifiers Scope of a declaration of an identifier The region of the program that the declaration is active (i.e., can access the variable, function, label, etc.) Five types of scope: Program (global scope) File Function prototype Function Block (“between the { } scope”)
11
Scope of Identifiers - Block Scope Block (local) scope A block is a series of statements enclosed in braces { } The identifier scope is active from the point of declaration to the end of the block ( } ) Nested blocks can both declare the same variable name and not interfere ex. from Ch var_scope_block.c scope_nested_blocks.c #include double product(double x, double y); int main() { int a = 10; double var1 = 3.0, var2 = 5.0; double ans; ans = product(var1, var2); printf("var1 = %.2f\n" "var2 = %.2f\n",var1,var2); printf("var1*var2 = %g\n", ans); } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; }
12
Scope of Identifiers - Program (Global) Scope Program (global) scope if declared outside of all functions "Visible" to all functions from point of declaration Visible to functions in other source files Use only when necessary and then very carefully!! ex. from Ch var_scope.c #include int a = 10; double product(double x, double y); int main() { double var1 = 3.0, var2 = 5.0; double ans; ans = product(var1, var2); printf("var1 = %.2f\n" "var2 = %.2f\n",var1,var2); printf("var1*var2 = %g\n", ans); } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; }
13
Function scope Applies only to labels start: * goto start; Active from the beginning to the end of a function Ex. Statement labels in a switch selection structure Scope of Identifiers - Function Scope #include int main() { int user_sel; /* prompt user for entry */ /* get user entry */ switch( user_sel ) { case 1: printf("\n message..."); /* call game function1 here */ break; case 2: printf("\n message..."); /* call game function2 here */ break; default: printf("Error"); break; }
14
Storage Duration How long the identifier exists in memory Static storage class Identifier exists when program execution begins For variables: Storage allocated and variable is initialized once Retains their values throughout the execution of the program #include void just_count(void); /* proto */ int main() { int i; for(i=0;i<10;i++) { just_count(); } return 0; } void just_count(void) { static int count_a; int count_b; count_a = count_a + 1; count_b = count_b + 1; printf("count_a== %d\n", count_a); printf("count_b== %d\n", count_b); } just_count.c
15
For functions: function name exists when execution begins For variables with global scope: i.e., declared outside of all functions and uses static keyword "Visible" to all functions from point of declaration in this source file only Keeps data ‘private’ to this file only Storage Duration, cont. #include static int a = 10; double product(double x, double y); int main() { double var1 = 3.0, var2 = 5.0; double ans; ans = product(var1, var2); printf("var1 = %.2f\n" "var2 = %.2f\n",var1,var2); printf("var1*var2 = %g\n", ans); } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; }
16
References Modular Programming in C http://www.icosaedro.it/c-modules.html http://www.icosaedro.it/c-modules.html math.h http://www.opengroup.org/onlinepubs/007 908799/xsh/math.h.html http://www.opengroup.org/onlinepubs/007 908799/xsh/math.h.html http://www.cprogramming.com/tutorial/styl e.html http://www.cprogramming.com/tutorial/styl e.html
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.