ENERGY 211 / CME 211 Lecture 8 October 8, 2008.

Slides:



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

Introduction to C Programming
Spring Semester 2013 Lecture 5
Programming Languages and Paradigms
Chapter 7: User-Defined Functions II
Procedures and Control Flow CS351 – Programming Paradigms.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
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
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Review of C++ Programming Part II Sheng-Fang Huang.
1 Chapter 9 Scope, Lifetime, and More on Functions.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
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.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
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.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 7: User-Defined Functions II
Introduction to C++ Systems Programming.
More about OOP and ADTs Classes
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Programmazione I a.a. 2017/2018.
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 14 Writing Classes part 2 Richard Gesick.
Templates.
User-Defined Functions
CISC/CMPE320 - Prof. McLeod
C++ for Engineers and Scientists Second Edition
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Chapter 9 Scope, Lifetime, and More on Functions
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
User Defined Functions
6 Chapter Functions.
More about OOP and ADTs Classes
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Submitted By : Veenu Saini Lecturer (IT)
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
C Language B. DHIVYA 17PCA140 II MCA.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Presentation transcript:

ENERGY 211 / CME 211 Lecture 8 October 8, 2008

Functions So far, we have seen programs in which all code resides within a main function Complex programs consist of sub-programs that perform particular tasks In C++, these subprograms are called functions A function accepts inputs, called arguments, and computes a return value that is the function's output

Designing a Function Decompose application into subtasks How do subtasks interact? If subtask A delegates work to subtask B, what data does A need to furnish B? What data does A require from B to complete its task? These questions determine a function's declaration Implementing each subtask completes function's definition

Calling a Function The input values passed to a function are the actual parameters (arguments) Inside the function, these values are the formal parameters (parameters) A function's definition includes its return type, name, formal parameters with types, and statements (the body) Its declaration is all this except the body If a function is called before it is defined, then it must be declared first

Procedures vs. Functions Many programming languages have procedures: subroutines that perform actions but do not return a value They also have distinct subprograms called functions, which compute output values from input values In C/C++, all subprograms are functions Can specify a return type of void, so that no value is returned, and the function is essentially a procedure

Overloading Functions Often functions perform similar tasks on inputs of different types Shouldn't require distinct functions Functions can be overloaded with different definitions for different inputs When function is called, compiler determines which definition to use Cannot overload with definitions that have same parameter types but different return types

The inline Keyword Many functions have a simple definition, such as void printint(int s) { cout << s; } When compiling, the overhead of a function call can be costly, but using a function is still best for modularity Preceding declaration with the inline keyword asks the compiler to replace calls to printint with its code

Pass by Value or Reference? Example: in void f(string x); the argument x is passed by value In f(y); the value of y is copied to x Any change in x does not affect y In void g(string& str); the argument s is passed by reference In g(s); the address of s is passed and assigned to str, so str is a reference to s A change in str is reflected in s

The const Qualifier Calling void f(string& s); by f("Hello"); is not valid "Hello" is an array of char, which supports literals, so allowing this implies a literal's value could be changed! Use void f(const string& s); to indicate s is not changed by f(), or create a string object and pass that const helps the compiler optimize code by reducing possible memory writes

The return Statement If a function is not of type void, then it should return a value, using a return statement Form: return expr; where expr is the value to be returned A return statement causes an immediate exit from the function! When function exits, local, non-static variables are deallocated, so never return a reference to a local object!

static Variables Normally, variables local to a function are destroyed when the function exits If a local variable is declared static, it persists throughout execution Value is retained, and available when function is called again Beware: initialization only happens once, when the function is first called! Global static variables can only be used in the file in which they are declared

Default Arguments Suppose we declare, for example, int f(int arg1, int optArg = 0); If we call f as follows: x = f(3); then inside f(), the value of optArg is 0 We call optArg a default argument An argument cannot be a default argument unless all subsequent arguments are also default Using defaults reduces overloading

Separate Header Files So far, we have declared and defined functions in the same file What if a function f() defined in a file f.cpp needs to call a function g() defined in another file g.cpp? Solution: declare g() in a header file, for example, g.h, and in f.cpp, use #include "g.h" Header files typically have a .h extension (sometimes .hpp)

Naming New Types C++ provides many ways of creating new types from existing ones: pointers to types, arrays of types, functions with return and argument types, etc. Types are complicated to describe The typedef keyword allows simple names to be given to these types Form (in most cases): typedef type-expression type-name; where type-name is the new name

Uses of typedef Opaque type names: to hide details about a type from a user, for example typedef unsigned int size_t; Descriptive: to convey interpretation of data: typedef int years; Simpler: to condense complex type expressions such as typedef int (*func_type)(int); where func_type is a pointer to a function that accepts, and returns, an int

Next Time All about streams and files: Console, file and string streams Stream manipulators Object persistence Project 2 distribution!