Scope: Portion of the program in which the identifier can be referenced. Various types of scope, my examples are of block scope and global scope.

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.
For(int i = 1; i
PHYS707: Special Topics C++ Lectures Lecture 3. Summary of Today’s lecture: 1.Functions (call-by-referencing) 2.Arrays 3.Pointers 4.More Arrays! 5.More.
Functions Prototypes, parameter passing, return values, activation frams.
AB 11 22 33 44 55 66 77 88 99 10  20  19  18  17  16  15  14  13  12  11  21  22  23  24  25  26  27  28.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
Scope and Casting. Scope Region of the program where a particular name can be referenced Formal parameters and local variables –can be accessed from within.
C workshop Yuli Kaplunovsky - Today - Introduction to C Recommended book: The C programming Language / Kernighan & Ritchie.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
Functions Pass by Value Pass by Reference IC 210.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
Learning C really fast A power point for Jay. RobotC vs C RobotC is slightly different than C We will cover standard C Then RobotC differences.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
Program style. Global area Place comment s at beginning of the program: 1.purpose 2.author 3.the date the program was written 4.the data and description.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
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.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
4.3 Functions. Functions Last class we talked about the idea and organization of a function. Today we talk about how to program them.
Pointers Value, Address, and Pointer. Values and Addresses int x, y, z; y x z values of x,
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.
Review (before the 1 st test): while (conditions) { statements; } while loop: if/else if/else statements: if (conditions) { statements; } else if (different.
Some remarks before we get started… Last time we learned about header files – in particular the math.h header file. You know that if you include math.h.
FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types.
Array of pointers We can have an array whose members are pointers, in this example pointers-to-int. int* data[3]; int i; int x = 5; int y = 89; int z =
CE-2810 Dr. Mark L. Hornick 1 “Classes” in C. CS-280 Dr. Mark L. Hornick 2 A struct is a complex datatype that can consist of Primitive datatypes Ints,
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
南亚和印度.
Dynamic memory allocation and Intraprogram Communication.
Test 2 Review Outline.
C Functions -Continue…-.
PHP 5 Syntax.
C Language By Sra Sontisirikit
Review of C… The basics of C scanf/printf if/elseif statements
Static vs Dynamic Scope
Dynamic memory allocation and Intraprogram Communication
Dynamic memory allocation and Intraprogram Communication
Scope, Parameter Passing, Storage Specifiers
Lecture 11 C Parameters Richard Gesick.
Scope of Variables.
Lec 6.
CS150 Introduction to Computer Science 1
Observing how the machine acts
CSCE 206 Lab Structured Programming in C
Chapter 7: User-Defined Functions II
Lab6 PROGRAMMING 1 metheds chapter5.
CS1201: Programming Language 2
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:

Scope: Portion of the program in which the identifier can be referenced. Various types of scope, my examples are of block scope and global scope.

#include int main(void) { int i = 10; printf(“i = %d\n”, i); if (i < 20) { int i = 10; i = i + 10; printf(“i = %d\n”, i); } printf(“i = %d\n”, i); } 1. i = 10 i = 20 i = i = 10 i = i = 10 i = i = 10 i = 10 i = 20

#include int main(void) { int i = 10; printf(“i = %d\n”, i); { int i = 10; i = i + 10; printf(“i = %d\n”, i); } printf(“i = %d\n”, i); } 1. i = 10 i = 20 i = i = 10 i = i = 10 i = i = 10 i = 10 i = 20

Output? #include int main(void) { int i = 10; printf(“i = %d\n”, i); { i = i + 10; printf(“i = %d\n”, i); } printf(“i = %d\n”, i); } 1. i = 10 i = 20 i = i = 10 i = i = 10 i = i = 10 i = 10 i = 20

What is the output? #include int doSomething(int, int); int main(void) { int a = 5, b =2, c; c = doSomething(a,b); printf(“In Main: a=%d, b=%d, c=%d\n", a, b, c); return 0; } int doSomething(int a, int b) { a = 10; b = 12; printf("In Function: a=%d, b=%d\n", a, b); return a+b; } 1. In Function: a=10, b=12 In Main: a=5, b=2, c=22 2. In Function: a=10, b=12 In Main: a=10, b=12, c=22 3. In Function: a=5, b=2 In Main: a=5, b=2, c=22 4. In Function: a=10, b=12 In Main: a=5, b=2, c=7

What is the output? #include int doSomething(); int main(void) { int a = 5, b =2, c=0; printf("In Main: a=%d, b=%d, c=%d\n", a, b, c); c = doSomething(); return 0; } int doSomething() { int a = 10; int b = 12; a = a*b; printf("In Function: a=%d, b=%d\n", a, b); return a; } 1. In Main: a=5, b=2, c=0 In Function: a=120, b=12 In Function: a=1440, b=12 2. In Main: a=5, b=2, c=0 In Function: a=10, b=12 3. In Main: a=5, b=2, c=0 In Function: a=120, b=12 4. In Main: a=5, b=2, c=0 In Function: a=1440, b=12

What is the output? #include int doSomething(); int main(void) { int a = 5, b =2, c=0; printf("In Main: a=%d, b=%d, c=%d\n", a, b, c); c = doSomething(); return 0; } int doSomething() { static int a = 10; static int b = 12; a = a*b; printf("In Function: a=%d, b=%d\n", a, b); return a; } 1. In Main: a=5, b=2, c=0 In Function: a=120, b=12 In Function: a=1440, b=12 2. In Main: a=5, b=2, c=0 In Function: a=10, b=12 3. In Main: a=5, b=2, c=0 In Function: a=120, b=12 4. In Main: a=5, b=2, c=0 In Function: a=1440, b=12

What is the output? #include int doSomething(); int b = 10; int main(void) { int a = 5, b = 2, c=0; printf("In Main: a=%d, b=%d, c=%d\n", a, b, c); c = doSomething(); return 0; } int doSomething() { int a = 10; a = a*b; printf("In Function: a=%d, b=%d\n", a, b); return a; } 1. In Main: a=5, b=2, c=0 In Function: a=100, b=10 2. In Main: a=5, b=2, c=0 In Function: a=10, b=10 3. In Main: a=5, b=2, c=0 In Function: a=20, b=2 4. In Main: a=5, b=10, c=0 In Function: a=100, b=10

Function syntax reminder for your assignment /*************************************** Comment block for the program ****************************************/ /*************************************** Comment block for the function ****************************************/ int function_name (parameters); /* Function prototype */ int main(void) { main program stuff; } int function_name (parameters) { function stuff; }