Scope, Parameter Passing, Storage Specifiers

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.
Chapter 7: User-Defined Functions II
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
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.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Storage Classes.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
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
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
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.
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.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
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.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Dale Roberts CSCI 230 Functions Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
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.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Functions Course conducted by: Md.Raihan ul Masood
Functions Scope local global Global Resolution Operator part 5.
IS Program Design and Software Tools Introduction to C++ Programming
Functions and an Introduction to Recursion
C Functions -Continue…-.
5 C Functions.
C Functions Pepper.
The Three Attributes of an Identifier
FUNCTIONS IN C++.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals Lecture #7 Functions
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
6 Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Dale Roberts, Lecturer IUPUI
Chapter 6 - Functions Outline 5.1 Introduction
Arrays Strings and Parameter Passing CSCI N305
Pointers Call-by-Reference CSCI 230
Variables have attributes
Function.
Scope Rules Of Variables
1-6 Midterm Review.
Function.
Scope Rules.
Presentation transcript:

Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Functions Scope, Parameter Passing, Storage Specifiers

2. Initialize global variable 1 /* Fig. 5.12: fig05_12.c 2 A scoping example */ 3 #include <stdio.h> 4 5 void a( void ); /* function prototype */ 6 void b( void ); /* function prototype */ 7 void c( void ); /* function prototype */ 8 9 int x = 1; /* global variable */ 10 11 int main() 12 { 13 int x = 5; /* local variable to main */ 14 15 printf("local x in outer scope of main is %d\n", x ); 16 17 { /* start new scope */ 18 int x = 7; 19 20 printf( "local x in inner scope of main is %d\n", x ); 21 } /* end new scope */ 22 23 printf( "local x in outer scope of main is %d\n", x ); 24 25 a(); /* a has automatic local x */ 26 b(); /* b has static local x */ 27 c(); /* c uses global x */ 28 a(); /* a reinitializes automatic local x */ 29 b(); /* static local x retains its previous value */ 30 c(); /* global x also retains its value */ 1. Function prototypes 2. Initialize global variable 3. Initialize local variable 4. Initialize local variable in block 5. Call functions 6. Output results

Program Output 31 32 printf( "local x in main is %d\n", x ); 33 return 0; 34 } 35 36 void a( void ) 37 { 38 int x = 25; /* initialized each time a is called */ 39 40 printf( "\nlocal x in a is %d after entering a\n", x ); 41 ++x; 42 printf( "local x in a is %d before exiting a\n", x ); 43 } 44 45 void b( void ) 46 { 47 static int x = 50; /* static initialization only */ 48 /* first time b is called */ 49 printf( "\nlocal static x is %d on entering b\n", x ); 50 ++x; 51 printf( "local static x is %d on exiting b\n", x ); 52 } 53 54 void c( void ) 55 { 56 printf( "\nglobal x is %d on entering c\n", x ); 57 x *= 10; 58 printf( "global x is %d on exiting c\n", x ); 59 } Program Output local x in outer scope of main is 5 local x in inner scope of main is 7 local x in a is 25 after entering a local x in a is 26 before exiting a local static x is 50 on entering b local static x is 51 on exiting b global x is 1 on entering c global x is 10 on exiting c local static x is 51 on entering b local static x is 52 on exiting b global x is 10 on entering c global x is 100 on exiting c local x in main is 5

Scope Rules File scope Function scope Block scope Identifier defined outside function, known in all functions Used for global variables, function definitions, function prototypes Function scope Can only be referenced inside a function body Used only for labels (start:, case: , etc.) Block scope Identifier declared inside a block Block scope begins at declaration, ends at right brace Used for variables, function parameters (local variables of function) Outer blocks "hidden" from inner blocks if there is a variable with the same name in the inner block Function prototype scope Used for identifiers in parameter list

Header Files Header files Custom header files Contain function prototypes for library functions <stdlib.h> , <math.h> , etc Load with #include <filename> #include <math.h> Custom header files Create file with functions Save as filename.h Load in other files with #include "filename.h" Reuse functions

Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value Copy of argument passed to function Changes in function do not effect original Use when function does not need to modify argument Avoids accidental changes Example: addone(int); main () { int i = 5, j; j=addone(i); printf(“%d %d\n”,i, j); 5 6 } addone(int x) return ++x;

Calling Functions: Call by Value and Call by Reference Call by reference (Passing Address) This is not actually call by reference, although some books called this ‘call-by-reference’ Passes original argument Changes in function effect original using & operator to pass address Only used with trusted functions Example: addone(int *); main () { int i = 5, j; j=addone(&i); printf(“%d %d\n”,i, j); 6 6 } addone(int *x) return ++(*x); For now, we focus on call by value

Storage Classes Storage class specifiers Automatic storage Storage duration – how long an object exists in memory Scope – where object can be referenced in program Linkage – specifies the files in which an identifier is known (more in Chapter 14) Automatic storage Object created and destroyed within its block auto: default for local variables and usually stored in Stack. auto double x, y; register: tries to put variable into high-speed registers Can only be used for automatic variables register int counter = 1; Example: f(register int c,n) { register int i; … }

Storage Classes Static storage Variables exist for entire program execution Default value of zero static: local variables defined in functions. Keep value after function ends Only known in their own function (does not change scope) Example: get_char() { static char buf[128]; static int bufp=0; }

Storage Classes If an external variable is to be referred to before it is defined, or if it is defined in a different source file from the one where it is being used, then extern declaration is necessary extern: default for global variables and functions Known in any function Usage: if large number of variables must be shared among functions, external variables are more convenient and efficient than long argument list Example: In file 1: … int sp=0; double val[12]; … In file 2; extern int sp; extern double val[]; int push(f) double f; { }

Acknowledgements