L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.

Slides:



Advertisements
Similar presentations
Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 ; Programmer-Defined Functions Two components of a function definition.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
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.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 12 - The Preprocessor Directives (Macros)
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Libraries Making Functions Globally Reusable (§ 6.4) 1.
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
CPS120: Introduction to Computer Science Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
L what are executable/non-executable statements l out of the ones below which constructs are executable #include p=3.14; const double PI=3.14; int myfunc(int);
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Programming Fundamentals Enumerations and Functions.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
What Is? function predefined, programmer-defined
Predefined Functions Revisited
INC 161 , CPE 100 Computer Programming
Separate Compilation and Namespaces
Chapter 13 - The Preprocessor
Programmer-Defined Functions, Call-by-Value, Multiple Files Lab 5
Pre-processor Directives
Multiple Files Revisited
Separate Compilation and Namespaces
Structures putting data together.
Separate Compilation.
User Defined Functions
Chapter 5 Function Basics
C Preprocessor(CPP).
Comments, Prototypes, Headers & Multiple Source Files
Structures putting data together.
Namespaces How Shall I Name Thee?.
C++ Compilation Model C++ is a compiled language
C Preprocessor Seema Chandak.
Multiple Files Revisited
Predefined Functions Revisited
C++ Programming Basics
What Is? function predefined, programmer-defined
Presentation transcript:

l function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition n head, body l function prototype (declaration) n expanded form, abbreviated form l local variables, global variables, scope l call-by-value What Is? 1

Program in Multiple Files

l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated by the compiler n examples: assignment statements, looping/branching constructs, function invocations l non-executable - no machine code generated examples: function prototypes, global variable and constant declarations, #include directives l global constant declarations may look like executable - they are not: const double PI=3.14; the compiler substituites 3.14 for every occurrence of PI in the program (Non) Executable Statements 3

#include directives tell the compiler to include specified file. The files included are also called header files and commonly have extensions.h l two forms: #include - the file is found in standard system-dependent location #include ”filename.h” - the file is located in the same directory as the rest of the code l the include directives are processed before the rest of the compilation l include files may also contain include directives l what to put in include files - non-executable statements l what not to put in include files - executable statements, function definitions l purpose of include files - centralize declarations Include Files 4

l large programs are usually kept in multiple files l reasons: n easy to maintain n can be compiled separately l functions are usually grouped into files by their purpose (functions dealing with one particular part of program are kept in one file) l function invocations, constants and variables cannot be put in program before the corresponding declarations. n what if they are in a separate file? l program is structured as follows: n program file (extension.cpp) - contains function definitions n include file (extension.h) - contains corresponding function prototypes, global constant and variable declarations if function A defined in file AA.cpp needs to call a function B which is defined in a different file BB.cpp - the corresponding header file BB.h is include d in file AA.cpp Program in Multiple Files 5

// adds one int add1(int); Example Program in Multiple Files // uses the function add1 // defined in a separate file #include #include "add1.h" int main() { // get the number cout << "Enter a number: "; int n; cin >> n; // find the number plus 1 int newn = add1(n); // print out the number plus 1 cout << newn << endl; } add1test.cpp add1.h #include "add1.h" // adds 1, // returns added value int add1(int n) { return (n + 1); } add1.cpp 6

Separate Compilation Add include files Executable program Source program (add1.cpp) Check file unit for legal syntax and compile it into an object file Link object file with standard object files and other object files to produce an executable program Include files (add1.h, iostream) compilation Object file (add1.o) Separate compilations Standard libraries 7

l each definition (e.g. global constant def.) can be encountered only once during compilation l when definition is placed in a header file, it may be included multiple times l header file must structured so it is safe in case of multiple inclusion; term – multiple inclusion protection n mechanism - preprocessor directives #define name value textual substitution of name for value do not use #define instead of global constants problem: #define press 50+5 int myvar = press * 20; // changes order of operations #ifdef name - true if name defined, #ifndef name - true if not #endif - completes #if header file myheader.h containing definitions usually has the following structure: #ifndef MYHEADER_H #define MYHEADER_H // text of the header file goes here #endif Multiple Inclusion Protection 8