Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
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.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 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.
 2007 Pearson Education, Inc. All rights reserved C Functions.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 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.
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.
1 Storage Classes, Scope, and Recursion Lecture 6.
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.
© 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.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
C Programming Lecture 8-1 : Function (Basic). What is a Function? A small program(subroutine) that performs a particular task Input : parameter / argument.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Header files and Library Functions) Outline.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3Math Library Functions Math library functions –perform.
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.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Lecture 17: The Last Puzzle Piece with Functions.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
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.
 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.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 2 Functions September.
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Functions Course conducted by: Md.Raihan ul Masood
Functions.
IS Program Design and Software Tools Introduction to C++ Programming
Functions and an Introduction to Recursion
C Functions -Continue…-.
FUNCTIONS IN C++.
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
Scope, Parameter Passing, Storage Specifiers
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
Chapter 6 - Functions Outline 5.1 Introduction
Variables have attributes
Function.
1-6 Midterm Review.
Function.
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

Lecture 13: Working with Multiple Programmers

Headers Header files: Each standard library has a corresponding header. The function prototype for all the library functions. Definition of various data types Definition of constants needed by the library functions Load with #include #include Custom header files Including function prototypes, definition of data types and constants. Save as filename.h Load with #include“filename.h” Source file and filename.h are in the same directory. Purposes: Reuse functions

Calling 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 To avoid accidental changes #include int add10(int x); int main( void ) { int a = 5; add10(a); printf(“%d\n”, a); return 0: } int add10(int x) { x += 10; return x; } … … … a 5 memory … … … a 5 x 5 … … … a 5 x 15 … … … a 5 memory Before calling add10() when calling add10() Just before return from add10() Just after return from add10()

Calling Functions Call by reference Passes original argument Changes in function effect original Only used with trusted functions In C, all calls are by value. Possible to simulate call-by-reference in C

Scope Rules The scope of an identifier is the portion of the program in which the identifier can be referenced. File scope Identifier defined outside function, know in all functions from the point at which the identifier is declared until the end of the file. Used for global variables, function definitions, function prototypes

Scope Rules Block scope Identifier declared inside a block Block scope begins at definition, ends at the terminating right brace ({) of the block. Used for local variables, function parameters Outer blocks “hidden” from inner blocks if there is a variable with the same name in the inner block

Scope Rules - Example #include void useGlobal( void ); void useLocal( void ); int x = 1; /* global variable */ int main( void ) { int x = 5; printf("x on entering main is %d\n", x); {/* start new scope */ int x = 7; /*local variable to new scope */ printf("x in inner scope of main is %d\n", x); }/* end new scope */ printf("x in outer scope of main is %d\n", x); useLocal(); useGlobal(); useLocal(); useGlobal(); printf("\nx on exiting main is %d\n", x); return 0; } void useLocal(void) { int x = 25; printf("\n x is %d on entering useLocal\n", x); x++; printf(" x is %d on exiting useLocal\n", x); } void useGlobal(void) { printf("\n x is %d on entering useGlobal.\n", x); x *= 10; printf(" x is %d on exiting useGlobal.\n", x); }

Scope Rules Function prototype scope Identifiers used in the parameter list of a function prototype. Variable names are ignored by the compiler Identifiers used in a function prototype can be reused elsewhere in the program without ambiguity. Function scope Can only be referenced inside a function body. Used only for labels (start:, case:, etc.)

Practice Question #include void anotherGlobal( void ); void useGlobal( void ); int x = 1; /* global variable */ int main( void ) { anotherGlobal(); printf(”%d ", x); return 0; } void anotherGlobal(void) { int x = 5; useGlobal(); printf(“%d ”, x); } void useGlobal(void) { x *= 10; printf(”%d ", x); } Q. What is the output of the following program? A B C D Solution: D