Chapter 7 Modularity Using Functions: Part II. A First Book of ANSI C, Fourth Edition 2 Variable Scope If variables created inside a function are available.

Slides:



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

Introduction to C Programming
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
Pointers CS 308 – Data Structures. Getting the address of a variable You need to use the address operator & #include void main() { int num; num = 22;
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Lecture 11 Oct 14, 02. Recursion ► The programs we have discussed are generally structured as functions that call one another in a disciplined, hierarchical.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Chapter 6: Functions.
C++ for Engineers and Scientists Third Edition
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
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.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Pointers CSE 5100 Data Structures and Algorithms.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
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 C++ CHAPTER 8 ARRAYS AND POINTERS.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
A First Book of ANSI C Fourth Edition
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Chapter 6 Modularity Using Functions
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
Function There are two types of Function User Defined Function
User-Defined Functions
Chapter 10: Pointers 1.
Chapter 5 - Functions Outline 5.1 Introduction
6 Chapter Functions.
A First Book of ANSI C Fourth Edition
Objectives You should be able to describe: Addresses and Pointers
Chapter 7: User-Defined Functions II
Functions Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

Chapter 7 Modularity Using Functions: Part II

A First Book of ANSI C, Fourth Edition 2 Variable Scope If variables created inside a function are available only to the function itself, they are said to be local to the function, or local variables Scope is the section of the program where the variable is valid or “known”

A First Book of ANSI C, Fourth Edition 3 Variable Scope (continued) A variable with a local scope has had storage set aside for it by a declaration statement made within a function body A variable with global scope is one whose storage has been created for it by a declaration statement located outside any function

A First Book of ANSI C, Fourth Edition 4 Variable Scope (continued)

A First Book of ANSI C, Fourth Edition 5 Variable Scope (continued) Program 7.1 produces the following output From main(): firstnum = 10 From main(): secnum = 20 From valfun(): firstnum = 10 From valfun(): secnum = 30 From main() again: firstnum = 40 From main() again: secnum = 20 While a function is executing, only the storage area for the variables and parameters created by this function are automatically accessed

A First Book of ANSI C, Fourth Edition 6 Variable Scope (continued) If a variable that is not local to the function is used by the function, the program searches the global storage areas for the correct name The scope of a variable does not influence the data type of the variable

A First Book of ANSI C, Fourth Edition 7 When to Use Global Declarations The scoping rules for symbolic constants and function prototypes are the same as for variables When a symbolic constant has a general meaning that is applicable throughout an application, it makes good programming sense to declare it globally at the top of a source code file Coding a function prototype as a global makes sense when the function is used by a number of other functions in a source code file –Doing so avoids repeating the prototype within each of the functions that will call it

A First Book of ANSI C, Fourth Edition 8 Misuse of Global Variables Except for symbolic constants and prototypes, global variables should almost never be used By making a variable global, you instantly destroy the safeguards C provides to make functions independent and insulated from each other Using global variables can be especially disastrous in large programs with many user- created functions –Because a global variable can be accessed and changed by any function following the global declaration, it is a time-consuming and frustrating task to locate the origin of an erroneous value

A First Book of ANSI C, Fourth Edition 9 Pass by Reference In pass by value, a called function receives values from its calling function, stores the passed values in its own local parameters, manipulates these parameters appropriately, and directly returns, at most, a single value Passing an address is referred to as a function pass by reference, because the called function can reference, or access, the variable using the passed address –Also referred to as a call by reference when the term applies only to those parameters whose addresses have been passed

A First Book of ANSI C, Fourth Edition 10 Passing Addresses to a Function Output is: num = 22 The address of num is

A First Book of ANSI C, Fourth Edition 11 Storing Addresses numAddr = # A variable that can store an address is known as a pointer variable or pointer

A First Book of ANSI C, Fourth Edition 12 Using Addresses Indirection operator: * –*numAddr means the variable whose address is stored in numAddr Or, the variable pointed to by numAddr When using a pointer, the value obtained is always found by first going to the pointer for an address; this is called indirect addressing

A First Book of ANSI C, Fourth Edition 13 Declaring and Using Pointers In declaring a pointer variable, C requires that we also specify the type of variable that is pointed to –int *numAddr; This declaration can be read in a number of ways: as the variable pointed to by numAddr is an integer, or as numAddr points to an integer

A First Book of ANSI C, Fourth Edition 14 Output is: The address stored in milesAddr is The value pointed to by milesAddr is 22 The value in miles is now 158 Declaring and Using Pointers

A First Book of ANSI C, Fourth Edition 15 Passing Addresses to a Function Sample run of Program 7.6: Enter a number: 24.6 The address that will be passed is The address received is The value pointed to by xnum is: 24.60

A First Book of ANSI C, Fourth Edition 16 Returns multiple values Passing Addresses to a Function