Scope Rules.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Introduction to C Programming
Introduction to C Programming
1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
CPSC 388 – Compiler Design and Construction
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
1 Functions and Structured Programming. 2 Structured Programming Structured programming is a problem-solving strategy and a programming methodology. –The.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
CPS120: Introduction to Computer Science Functions.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
CPS120: Introduction to Computer Science Lecture 14 Functions.
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.
Review Loops – Condition – Index Functions – Definition – Call – Parameters – Return value.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
G3-1 University of Washington Computer Programming I Structuring Program Files © 2000 UW CSE.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
CHAPTER 8 Scope, Lifetime, and More on Functions.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
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.
The Lifetime of a Variable
C Functions -Continue…-.
Functions and Structured Programming
C Language By Sra Sontisirikit
Function There are two types of Function User Defined Function
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
11/10/2018.
Scope, Parameter Passing, Storage Specifiers
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
User Defined Functions
Scope of Variables.
6 Chapter Functions.
Pointers Call-by-Reference CSCI 230
Local Variables, Global Variables and Variable Scope
Chapter 7: User-Defined Functions II
ECE 103 Engineering Programming Chapter 12 More C Statements
Submitted By : Veenu Saini Lecturer (IT)
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
STORAGE CLASS.
Assignment Operators Topics Increment and Decrement Operators
CPS125.
Topics to cover Instance variables vs. local variables.
Corresponds with Chapter 5
Rudra Dutta CSC Spring 2007, Section 001
Methods Scope How are names handled?
C Parameter Passing.
Presentation transcript:

Scope Rules

A scope in any programming is a region of the program where a defined variable can have its existence and beyond that region it cannot be accessed. There are three places where variables can be declared in C programming language − 1.Inside a function or a block which is called local variables. 2.Outside of all functions which is called global variables. 3.In the definition of function parameters which are called formal parameters.

Local Variable (Local scope) Variables that are declared inside a function or block are called local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside to the function in which it is defined.

Example #include <stdio.h> int main () { /* local variable declaration */ int a, b,c; /* actual initialization */ a = 10; b = 20; c = a + b; printf ("value of a = %d, b = %d and c = %d\n", a, b,c”); return 0; }

Global Variables(Global Scope) Global variables are defined outside a function, usually on top of the program. Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program. A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration

Example #include <stdio.h> int g; /* global variable declaration */ int main () { int a, b; /* local variable declaration */ a = 10; b = 20; g = a + b; printf ("value of a = %d, b = %d and g = %d\n", a, b, g); return 0; }

A program can have same name for local and global variables but the value of local variable inside a function will take preference. Here is an example − #include <stdio.h> int g = 20; /* global variable declaration */ int main () { int g = 10; /* local variable declaration */ printf ("value of g = %d\n", g); return 0; }

Formal Parameters Formal parameters, are treated as local variables with-in a function and they take precedence over global variables. Following is an example −