Week 5 Part 2 Kyle Dewey. Overview Scope Lifetime Testing Exam #1 overview.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Week #10 Kyle Dewey. Overview Meeting times Typos Project #3 tidbits Exam Review.
Access to Names Namespaces, Scopes, Access privileges.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
B-1 Lecture 2: Problems, Algorithms, and Programs © 2000 UW CSE University of Washington Computer Programming I.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Jun 16, 2014IAT 2651 Debugging. Dialectical Materialism  Dialectical materialism is a strand of Marxism, synthesizing Hegel's dialectics, which proposes.
Variable Scope Storage Class Recursion
Week 9 Part 1 Kyle Dewey. Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2.
Unit Testing 101 Black Box v. White Box. Definition of V&V Verification - is the product correct Validation - is it the correct product.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
1 Scope Lifetime Functions (the Sequel) Chapter 8.
Week 7 Part I Kyle Dewey. Overview Code from last time Array initialization Pointers vs. arrays Structs typedef Bubble sort (if time)
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Variable Scope. When you declare a variable, that name and value is only “alive” for some parts of the program  We must declare variables before we use.
Recursion ● Recursion is a computer programming technique that allows programmers to divide problems into smaller problems of the same type. ● You can.
Advanced Programming in C
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Programming Logic and Design Seventh Edition
Week 3 Part 2 Kyle Dewey.
Friend functions, operator overloading
Introduction to Programming
Overview 4 major memory segments Key differences from Java stack
C Functions -Continue…-.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
CompSci 230 Software Construction
Functions and Procedures
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Functions Separate Compilation
Week 7 Part 2 Kyle Dewey.
Namespaces, Scopes, Access privileges
Global & Local Identifiers
Functions.
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Lesson #6 Modular Programming and Functions.
Functions Inputs Output
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Coding Design, Style, Documentation and Optimization
Object Oriented Programming COP3330 / CGS5409
Overview 4 major memory segments Key differences from Java stack
Scope, Visibility, and Lifetime
Chapter 6 - Functions Outline 5.1 Introduction
Object-Oriented Programming Using C++ Second Edition
The C Programming Language
Compound Statements A Quick Overview
Writing Functions.
Namespaces, Scopes, Access privileges
Homework Any Questions?.
Chapter 7: User-Defined Functions II
Lesson #6 Modular Programming and Functions.
Function.
Suggested self-checks: Section 7.11 #1-11
Classes, Objects and Methods
CPS120: Introduction to Computer Science
Function.
The Three Attributes of an Identifier
ITM 352 Functions.
Programming Fundamental
Copy Constructors and Overloaded Assignment
Methods and Data Passing
Scope Rules.
Presentation transcript:

Week 5 Part 2 Kyle Dewey

Overview Scope Lifetime Testing Exam #1 overview

What’s with the {... } ?

Recall Function definitions look like this: void foo() {... } Conditionals ( if ) look like this: if ( condition ) {... } while loops look like this: while ( condition ) {... }

Brackets The {... } part is significant This is called a block Blocks have special meaning to C (and to the vast majority of languages)

Blocks As we’ve already seen, blocks can be nested: void foo() { int x; for ( x = 0; x < 10; x++ ) { if ( x % 2 == 0 ) { printf( “Even: %i\n”, x ); continue; } printf( “Odd: %i\n”, x ); }

Blocks Importance of this lies in variable declaration A block nested at level N has access to variables defined at nesting levels 0.. N - 1, but not the other way around

Example void foo() { int x = 10; if ( x > 5 ) { int y = x * 4; // this block can access x } //...but this block can’t access y }

So what? This may seem obvious and/or insignificant This mechanism means that you don’t have to worry about what was defined in inner blocks, because they are inaccessible anyway

Variable Name Reusage Blocks help to prevent variable names from clashing A variable foo defined in a given block is distinct from all other variables named foo defined in other blocks

Example int x =...; if ( x < 10 ) { int y = 20; } else { int y = 30; } Distinct variables

Variable Name Reusage Consider the following code: int x =...; if ( x < 10 ) { int x = 20; } else { int x = 30; } Name ( x ) Reused

Variable Name Reusage The original variable x does not change The old definition is shadowed by the new one, not overwritten int x =...; if ( x < 10 ) { int x = 20; } else { int x = 30; }

Question What does this code print? int x = 10; if ( x == 10 ) { int x = 5; printf( “%i\n”, x ); } printf( “%i\n”, x );

Block Advantage Focus only on one block at a time, not on previous blocks Variables defined in previous blocks are shadowed

Scope Scope defines which variables can be accessed at any given point in the code Blocks manipulate the scope if ( 1 < 2 ) { int x = 5; // x is now in scope } // x is no longer in scope

Scope Example int x = 10; // x is now in scope if ( 1 < 2 ) { int x = 5; // x is in scope, but it // refers to the x = 5 definition } // x is in scope, but it refers to // the x = 10 definition

Lifetime How long a variable exists in your program is the variable’s lifetime Scope is not the same as lifetime Scope: when you can access a variable Lifetime: whether or not a variable is there

Scope vs. Lifetime A variable in scope is necessarily alive A variable that’s alive is not necessarily in scope

Example #1 int x =...; // alive and in scope if ( x < 10 ) { int y = 5; // alive and in scope... } // y is not in scope and not alive

Example #2 int x =...; // alive and in scope if ( x < 10 ) { int y = 5; // x and y are alive // and in scope if ( x < y + 5 ) { int z = 20; // x, y, z alive and in scope } // x, y alive and in scope } // x alive and in scope

Example #3 int x =...; // alive and in scope if ( x < 10 ) { int x = 5; // x =... is alive // but not in scope // x = 5 alive in scope if ( x < y + 5 ) { int x = 20; // x =... and x = 5 alive // only x = 20 is in scope } // x =... and x = 5 alive // only x = 5 in scope } // x =... alive and in scope

Example #4 void bar() { // y is alive but not in scope int z = 5; } void foo() { int y = 10; bar(); } void main() { foo(); }

Global Variables Consider the following code: int x = 10; void foobar() { printf( “%i\n”, x ); } void barfoo() { x++; }

Global Variables x is a global variable Always in scope (unless shadowed) Always alive int x = 10; void foobar() { printf( “%i\n”, x ); } void barfoo() { x++; }

Thought Question Global variables are seen as bad practice, and are usually avoided Why?

Answer Always in scope and always alive means everything in the file probably heavily relies on it Another variable to keep track of for everything in the file Can be error prone Interdependent code

Aside: “In the File” Technically a “compilation unit” In this class, a file is a compilation unit However, it’s possible to have multiple files in the same compilation unit

Testing

Recall... Testing is an important step in software development Builds confidence that code works correctly Modern software development heavily relies on testing

Testing Testing can confirm a bug exists...but it cannot confirm that bugs do not exist May not be testing for it May need additional tests

Testing Weakness int badMax( int x, int y ) { if ( x == 513 ) { return x; } else if ( x > y ) { return x; } else { return y; }

Testing Strength Code is not usually written like that The goal is not to mess up the tests Simple (compared to verification, which attempts to prove that there are no bugs)

Additional Terminology White box testing: you can see the whole code, as with: // get the max of x and y int max( int x, int y ) { if ( x > y ) { return x; } else { return y; }

Additional Terminology Black box testing: you can see only the interfaces and what they do, as with: // get the max of x and y int max( int x, int y );

Exam #1 Overview