Functions and scope Confidence In scope Reference: K&K textbook, Chapter 4.

Slides:



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

Introduction to C Programming
Modular Programming With Functions
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Chapter 7: User-Defined Functions II
Kernighan/Ritchie: Kelley/Pohl:
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.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Structure of a C program
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
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.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
Storage Classes.
Chapter 13. Binary Files In binary files we do not need to format data File streams include two member functions specifically designed to input and output.
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
chap13 Chapter 13 Programming in the Large.
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.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
18. DECLARATIONS.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
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.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
 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.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
CSCI 171 Presentation 6 Functions and Variable Scope.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
Pointers *, &, array similarities, functions, sizeof.
Chapter 12: Programming in the Large By: Suraya Alias 1-1.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
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.
A First Book of ANSI C Fourth Edition
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Advanced Programming in C
Lecture 3 Translation.
C Functions -Continue…-.
A Lecture for the c++ Course
CSC113: Computer Programming (Theory = 03, Lab = 01)
Introduction to C Programming Language
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
CSC 253 Lecture 8.
Scope, Parameter Passing, Storage Specifiers
CSC 253 Lecture 8.
פרטים נוספים בסילבוס של הקורס
Scope Rules and Storage Types
Scope Rules Of Variables
In C Programming Language
C Language B. DHIVYA 17PCA140 II MCA.
C Programming Lecture-17 Storage Classes
Run-time environments
Presentation transcript:

Functions and scope Confidence In scope Reference: K&K textbook, Chapter 4

Postconditions You should be able to define function prototypes draw pictures of the runtime stack during execution of code (show understanding of memory model) state scope of any identifier in a C program, ie visibility, hiding Draw pictures of memory based on classes Explain reason for initialisation restrictions compile and run multi-file programs

define function prototypes draw pictures of the runtime stack during execution of code (show understanding of memory model) state scope of any identifier in a C program, ie visibility, hiding Draw pictures of memory based on classes Explain reason for initialisation restrictions compile and run multi-file programs

Function prototypes /* */ do_that( ) { double *p;... p = do_this(*p); } /* */ double *do_this(double a) { double *p;... p = do_that( ); }

double *do_this(double); /* */ do_that( ) { double *p;... p = do_this(*p); } /* */ double *do_this(double a) { double *p;... p = do_that( ); } Function prototype

define function prototypes draw pictures of the runtime stack during execution of code (show understanding of memory model) state scope of any identifier in a C program, ie visibility, hiding Draw pictures of memory based on classes Explain reason for initialisation restrictions compile and run multi-file programs

main ( ){ int m1, m2, m3, m4;... Do_Input( ); Process_and_Print( ); } Do_Input( ){ int i1, i2;... } Process_and_Print( ){ int p1, p2;... calc(p1, p2); } calc(int x, int y){ int c1;... }

Process_and_Print( ){ int p1, p2;... calc(p1, p2); } Do_Input( ){ int i1, i2;... } calc(int x, int y){ int c1;... } main ( ){ int m1, m2, m3, m4;... Do_Input( ); Process_and_Print( ); }

Order of function execution main Do_Input Process_and_Print calc Static v dynamic structure Order of functions does not affect runtime structure

Scope, static v dynamic structure Static structure of code –Holds from time code is written Run time structures –Differ according to data in individual runs Is scope defined by the static structure or the dynamic structure?

Terminology: scope…. Scope = Visibility Visible v hidden

m1 m2 m3 m4 main ( ){ int m1, m2, m3, m4;... Do_Input( ); Process_and_Print( ); } Do_Input( ){ int i1, i2;... } Process_and_Print( ){ int p1, p2;... calc(p1, p2); } calc(int x, int y){ int c1;... }

Housekeeping data for Do_Input i1 i2 m1 m2 m3 m4 main ( ){ int m1, m2, m3, m4;... Do_Input( ); Process_and_Print( ); } Do_Input( ){ int i1, i2;... } Process_and_Print( ){ int p1, p2;... calc(p1, p2); } calc(int x, int y){ int c1;... }

m1 m2 m3 m4 main ( ){ int m1, m2, m3, m4;... Do_Input( ); Process_and_Print( ); } Do_Input( ){ int i1, i2;... } Process_and_Print( ){ int p1, p2;... calc(p1, p2); } calc(int x, int y){ int c1;... }

Housekeeping data for Process_and_Print p1 p2 m1 m2 m3 m4 main ( ){ int m1, m2, m3, m4;... Do_Input( ); Process_and_Print( ); } Do_Input( ){ int i1, i2;... } Process_and_Print( ){ int p1, p2;... calc(p1, p2); } calc(int x, int y){ int c1;... }

Housekeeping data for calc x y c1 Housekeeping data for Process_and_Print p1 p2 m1 m2 m3 m4 main ( ){ int m1, m2, m3, m4;... Do_Input( ); Process_and_Print( ); } Do_Input( ){ int i1, i2;... } Process_and_Print( ){ int p1, p2;... calc(p1, p2); } calc(int x, int y){ int c1;... }

Housekeeping data for Process_and_Print p1 p2 m1 m2 m3 m4 main ( ){ int m1, m2, m3, m4;... Do_Input( ); Process_and_Print( ); } Do_Input( ){ int i1, i2;... } Process_and_Print( ){ int p1, p2;... calc(p1, p2); } calc(int x, int y){ int c1;... }

m1 m2 m3 m4 main ( ){ int m1, m2, m3, m4;... Do_Input( ); Process_and_Print( ); } Do_Input( ){ int i1, i2;... } Process_and_Print( ){ int p1, p2;... calc(p1, p2); } calc(int x, int y){ int c1;... }

define function prototypes draw pictures of the runtime stack during execution of code (show understanding of memory model) state scope of any identifier in a C program, ie visibility, hiding Draw pictures of memory based on classes Explain reason for initialisation restrictions compile and run multi-file programs

Communication between functions Arguments (aka parameters) Global data Which is better?

Scope Within blocks Within a file Between files

Static structure of programs Typical program structure global declarations main function other functions and global declarations interspersed Note: need function prototypes before the function is used

Static structure of functions Function header Declarations Function body int Dojob(int age) { int jam; … statements of the function body }

Static structure of blocks Declarations Block body while …. { int jam; … statements of the block body }

{/* outer block */ int a; int b;... { /* inner block */ char a; int d;... }... } Scope of variable identifiers outer a b inner a d

int main( ){ int a2;... } fileA float a3; char A(char a4){ char a5;... } functionsvariables a1 a2 a3 a4 a5 int a1; main A

Scope between files Importing variable identifiers extern int a1; Hiding variable and function identifiers static

Scope between files See example on page 75, Chapter 4 of textbook

Exercise How to make a3 accessible to main? To B2? What happens if we added to start of fileA: int b1; and what about int b2; What is the effect of adding to start of fileB: extern char b1; What happens if we add to start of fileA: extern int B2();

Storage classes auto (stack) static register (optimisation - obsolete) extern You need to distinguish these in drawings of memory models

Storage classes stack global/static You need to distinguish these in drawings of memory models

Storage classes – memory models Dynamic, volatile memory, change through the execution of the program –auto (stack) –register Persistent memory – stable for the duration of the runtime of the program –static –extern You need to distinguish these in drawings of memory models

Initialisations - globals #define BOUND 100 static int a = 72 * sizeof(int); int b = BOUND; extern char x; int y; Volatile storage Persistent storage registerauto heapextern, static

Initialisations - globals #define BOUND 100 static int a = 72 * sizeof(int); int b = BOUND; extern char x; int y; Volatile storage Persistent storage registerauto heapextern, static

Initialisations - globals #define BOUND 100 static int a = 72 * sizeof(int); /* constant expression */ int b = BOUND; /* constant expression */ extern char x; /* cannot be initialised here */ int y; /* defaults to zero */

Initialisations - local #define BOUND 100 static int a = 72 * sizeof(int); int b = BOUND; extern char x; int y; fnA( ) { static int c = BOUND + 7; int d = a + fnXX(); register int e = b; extern char g;... }

Initialisations - local fnA( ) { static int c = BOUND + 7; /* constant expression */ int d = a + fnXX(); /* any expression */ register int e = b; /* any expression */ extern char g; /* cannot be initialised */... }

define function prototypes draw pictures of the runtime stack during execution of code (show understanding of memory model) state scope of any identifier in a C program, ie visibility, hiding Draw pictures of memory based on classes Explain reason for initialisation restrictions compile and run multi-file programs

Compiling multi-file programs Preprocessor C compiler Assembler Linker Source code Name.c Assembly code Name.s Object code Name.o a.out

Compiling multi-file programs Compiler can stop at the.o stage Linker takes one or more.o files and produces the a.out Use the ‘-c’ flag to gcc to produce the.o

Compiling multi-file programs bash$ gcc -c doin.c proc.c dout.c bash$ gcc doin.o proc.o dout.o –o pds bash$ gcc -c proc.c bash$ gcc doin.o proc.o dout.o -o pds

Using functions from the standard libraries #include double sin(double); double n; double x; scanf("%f", &n); x = sin(n); __________________________________ gcc -lm... Library name is ‘m’

Postconditions You should be able to define function prototypes draw pictures of the runtime stack during execution of code (show understanding of memory model) state scope of any identifier in a C program, ie visibility, hiding Draw pictures of memory based on classes Explain reason for initialisation restrictions compile and run multi-file programs

define function prototypes draw pictures of the runtime stack during execution of code (show understanding of memory model) state scope of any identifier in a C program, ie visibility, hiding Draw pictures of memory based on classes Explain reason for initialisation restrictions compile and run multi-file programs

Memory: the picture so far Memory as a long stream of bits C code associates a name with a part of the memory – with space for that type Memory for different things: –Ordinary data –Arrays –Pointers Draw lines showing where pointers point to

Memory: the picture so far Memory as a long stream of bits C code associates a name with a part of the memory – with space for that type Memory for different things: –Ordinary data –Arrays –Pointers Draw lines showing where pointers point to chs[0] nump X_accurate chs[1] chs[2] chs[3]