What Is? function predefined, programmer-defined

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.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
 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.
Chapter 6: User-Defined Functions I
 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.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
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.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
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: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
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.
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.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
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
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.
Chapter 6: User-Defined Functions I
Predefined Functions Revisited
Separate Compilation and Namespaces
Chapter 13 - The Preprocessor
14. THE PREPROCESSOR.
Chapter Structured Types, Data Abstraction and Classes
Programmer-Defined Functions, Call-by-Value, Multiple Files Lab 5
CSC113: Computer Programming (Theory = 03, Lab = 01)
Pre-processor Directives
User-Defined Functions
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.
CSc 352 An Introduction to the C Preprocessor
Code Organization CSCE 121 J. Michael Moore.
Namespaces How Shall I Name Thee?.
C++ Compilation Model C++ is a compiled language
Chapter 6: User-Defined Functions I
C Preprocessor Seema Chandak.
Separate Compilation.
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
C++ Programming Basics
What Is? function predefined, programmer-defined
Presentation transcript:

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

Program in Multiple Files when one file is not enough

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

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

Program in Multiple Files large programs are usually kept in multiple files reasons: easy to maintain can be compiled separately functions are usually grouped into files by their purpose: functions dealing with one particular part of program are kept in one file scope rule: function, variable, constant has to be declared before use what if declaration is in a different file? program is structured as follows: program file (extension .cpp) - contains function definitions 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 included in file AA.cpp

Example Program in Multiple Files #include "add1.h" // adds 1, // returns added value int add1(int n) { return n + 1; } add1.cpp add1.h // adds one int add1(int); // uses the function add1 // defined in a separate file #include <iostream> #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

Separate Compilation separate compilations source program (add1.cpp) Include files (add1.h, iostream) link object file with standard object files and other object files to produce an executable program add include files check object file (add1.o) file unit for legal syntax and compile it into an object file standard libraries compilation executable program

Multiple Inclusion Protection each definition (e.g. global constant def.) can be encountered only once during compilation when definition is placed in a header file, it may be included multiple times header file must structured so it is safe in case of multiple inclusion; term – multiple inclusion protection mechanism - preprocessor directives #define name value textual substitution of name by value do not use #define instead of global constants problem: #define press 50+5 int myvar = press * 20; // changes order of operations value may be skipped: #define BIG_NAME #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