C Programming Lecture 8 Call by Reference Scope. Call-by-Reference b For a C function to “effect” (read as “imitate”) call-by- reference: Pointers must.

Slides:



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

Introduction to C Programming
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
1 Review of Class on Oct Outline  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
1 CSE1301 Computer Programming Lecture 16 Pointers.
Loose endsCS-2301, B-Term “Loose Ends” CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.
"Loose ends"CS-2301 D-term “Loose Ends” CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming Language, 2 nd edition,
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
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.
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
1 CSE1301 Computer Programming Lecture 16 Pointers.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
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.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
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.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
University of Malta CSA2090: Lecture 4 © Chris Staff 1 of 20 CSA2090: Systems Programming Introduction to C Dr. Christopher Staff.
Lecture 17: The Last Puzzle Piece with Functions.
CSE 3302 Programming Languages
Introduction to Computing Lecture 12 Pointers Dr. Bekir KARLIK Yasar University Department of Computer Engineering
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.
PHY 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
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.
Introduction to Programming Using C
CSE 220 – C Programming Pointers.
C Functions -Continue…-.
Functions and Structured Programming
Pointers in C.
Functions Dr. Sajib Datta
Programming Fundamentals Lecture #7 Functions
CSI-121 Structured Programming Language Lecture 16 Pointers
Global & Local Identifiers
Dynamic memory allocation and Intraprogram Communication
INC 161 , CPE 100 Computer Programming
CSC 253 Lecture 8.
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
CSC 253 Lecture 8.
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Lecture 18 Arrays and Pointer Arithmetic
Writing Functions.
Simulating Reference Parameters in C
CISC181 Introduction to Computer Science Dr
ECE 103 Engineering Programming Chapter 12 More C Statements
Method of Classes Chapter 7, page 155 Lecture /4/6.
Arrays and Pointers CSE 2031 Fall May 2019.
Arrays and Pointers CSE 2031 Fall July 2019.
Programming Fundamental
Scope Rules.
Presentation transcript:

C Programming Lecture 8 Call by Reference Scope

Call-by-Reference b For a C function to “effect” (read as “imitate”) call-by- reference: Pointers must be used in the parameter list in the function definition.Pointers must be used in the parameter list in the function definition. When the function is called, addresses of variables must be passed as arguments.When the function is called, addresses of variables must be passed as arguments.

Example of “Call-by-Reference” #include void swap(int *, int *); int main(void) { int a = 3, b = 7; printf(“%d %d\n”, a, b); /* 3 7 is printed */ swap(&a, &b); printf(“%d %d\n”, a, b); /* 7 3 is printed */ return 0; } void swap(int *p, int *q) { int tmp; tmp = *p; /* Be sure you understand this */ *p = *q; *q = tmp; }

Scope Rules b The scope of an identifier is the part of the program text in which the identifier is known or accessible. Identifiers are only accessible within the block in which they are declared.Identifiers are only accessible within the block in which they are declared. They are unknown outside the boundaries of that block.They are unknown outside the boundaries of that block.

What is a Block? A block is a compound statement that includes declarations.A block is a compound statement that includes declarations. A compound statement is a series of declarations and statements surrounded by braces.A compound statement is a series of declarations and statements surrounded by braces. –Recall that it is syntactically correct to place a compound statement wherever it is correct to place a statement.

Reusing Identifier Names b For a variety of reasons, programmers use the same identifier (such as a variable name) in different locations. b If variables are declared in different blocks (such as in different functions), then they are different variables (represent different memory locations) even if they may have the same name.

How it Works with Nested Blocks b An identifier declared in an outer block is valid in that block and all inner (nested) blocks unless an inner block redefines (declares) an identifier with the same name. If this happens, the outer block identifier is hidden, or masked, from the inner block.If this happens, the outer block identifier is hidden, or masked, from the inner block. Inner blocks may be nested to depths determined by system limitations.Inner blocks may be nested to depths determined by system limitations.

Example of Nested Blocks Using the Same Identifier Name { int a = 2; /* outer block a */ int a = 2; /* outer block a */ printf(“%d\n”, a); /* 2 is printed */ printf(“%d\n”, a); /* 2 is printed */ { int a = 7; int a = 7; printf(“%d\n”, a); /* 7 is printed */ printf(“%d\n”, a); /* 7 is printed */ } /* back to outer block */ } /* back to outer block */ printf(“%d\n”, ++a); /* 3 is printed */ printf(“%d\n”, ++a); /* 3 is printed */}