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.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 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.
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
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.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
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.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
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);
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.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
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.
What Is? function predefined, programmer-defined
Predefined Functions Revisited
Separate Compilation and Namespaces
Chapter 13 - The Preprocessor
Chapter 5 Function Basics
14. THE PREPROCESSOR.
Chapter Structured Types, Data Abstraction and Classes
Programmer-Defined Functions, Call-by-Value, Multiple Files Lab 5
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
Pre-processor Directives
Multiple Files Revisited
Separate Compilation and Namespaces
Structures putting data together.
Separate Compilation.
User Defined Functions
Chapter 5 Function Basics
Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register,
C Preprocessor(CPP).
Introduction to Classes and Objects
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
C Preprocessor Seema Chandak.
Multiple Files Revisited
Separate Compilation.
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
C++ Programming Basics
Presentation transcript:

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

Programming in Multiple Files 2

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

Include Files #include instructions tell the compiler to include a specified file. The files included are also called header files and commonly have extensions .h two forms: #include <filename> - the file is found in a standard system-dependent location #include ”filename.h” - the file is located in the same directory as the rest of the code the include directives are processed before the rest of the compilation 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) function invocations, constants and variables cannot be put in program before the corresponding declarations. what if they are in a separate 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

Preprocessor Directives 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 note that substitution is textual problem: #define press 50 (later) int press=20; The name in the declaration gets substituted, creating a syntax error. #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