Lecture 7: Variable Scope B Burlingame March 16, 2016.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Modular Programming With Functions
Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
CS1061 C Programming Lecture 9: switch Statement and Variable Scope A. O’Riordan, 2004.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.
CSCI 130 Scope of Variables Chapter 6. What is scope Parts of program which can access a variable –accessibility –visibility How long variable takes up.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
CSC204 – Programming I Lecture 4 August 28, 2002.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 8: Functions, File IO.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
1 Homework HW5 due today Review a lot of things about allocation of storage that may not have been clear when we covered them in our initial pass Introduction.
Week 6: Functions - Part 2 BJ Furman 01OCT2012. The Plan for Today Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
CSCI 171 Presentation 6 Functions and Variable Scope.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Lecture 10: Peripherals Bryan Burlingame 04 November 2015.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Lecture 7: Modular Programming (functions) B Burlingame 05 October, 2016.
Functions Course conducted by: Md.Raihan ul Masood
The Three Attributes of an Identifier
Week 6: Style and Peripherals
Chapter 7: User-Defined Functions II
C Functions -Continue…-.
Introduction to C++ Systems Programming.
Lecture 8: Variable Scope & Working with Files
The Three Attributes of an Identifier
Lecture 7: Modular Programming (functions)
User-Defined Functions
Scope, Parameter Passing, Storage Specifiers
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
Chapter 6 - Functions Outline 5.1 Introduction
Classes and Objects.
Dr Tripty Singh Tutorial for Fuctions
Chapter 7: User-Defined Functions II
Function.
ECE 103 Engineering Programming Chapter 12 More C Statements
In C Programming Language
1-6 Midterm Review.
Programming Languages and Paradigms
Compiler vs linker The compiler translates one .c file into a .o file
C Language B. DHIVYA 17PCA140 II MCA.
Function.
The Three Attributes of an Identifier
Storage classes in C In C language, each variable has a storage class which decides the following things: scope i.e where the value of the variable would.
Scope Rules.
Presentation transcript:

Lecture 7: Variable Scope B Burlingame March 16, 2016

Announcements Midterm Today – next Tuesday Homework #3 due up front Homework #4 due after break

Learning Objectives Discuss the importance of style in code Define scope and variable duration

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

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  9 different style documented

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 ) { height = * pow( time, 4.0 ) * pow( time, 3.0 ) ; velocity = (-0.48 * pow( time, 3.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); }

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 ) { height = * pow( time, 4.0 ) * pow( time, 3.0 ) ; velocity = (-0.48 * pow( time, 3.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); }

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 ) { height = * pow( time, 4.0 ) * pow( time, 3.0 ) ; velocity = (-0.48 * pow( time, 3.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); }

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’

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”)

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; }

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; }

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; }

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

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; }

References Modular Programming in C math.h /xsh/math.h.html /xsh/math.h.html e.html e.html