#include "std_lib_facilities

Slides:



Advertisements
Similar presentations
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Advertisements

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Writing and Testing Programs Drivers and Stubs Supplement to text.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
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.
Chapter 6: Functions.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Chapter 8 Technicalities: Functions, etc. John Keyser’s Modifications of Slides by Bjarne Stroustrup
1 Chapter 9 Scope, Lifetime, and More on Functions.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Scope Accessibility of Names. Review We’ve seen that C++ permits a programmer to declare names and then use those names in a manner consistent with their.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
Runtime Environments Compiler Construction Chapter 7.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
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.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Slides adapted from: Bjarne Stroustrup, Programming – Principles and Practice using C++ Chapter 8 Technicalities: Functions, etc. Hartmut Kaiser
Motivation for Generic Programming in C++
Test 2 Review Outline.
CSE 374 Programming Concepts & Tools
Motivation and Overview
Predefined Functions Revisited
Bill Tucker Austin Community College COSC 1315
Chapter Structured Types, Data Abstraction and Classes
Pointers and Pointer-Based Strings
Global & Local Identifiers
Multiple Files Revisited
Chapter 5 Function Basics
Chapter 8 Technicalities: Functions, etc.
Pointers, Dynamic Data, and Reference Types
6 Chapter Functions.
Chapter 8 Technicalities: Functions, etc.
Chapter 6 Class Definitions and Member Functions
Anatomy of a Function Part 1
Chapter 4 Implementing Free Functions
Reference Parameters.
Functions 2: Stupid Function Tricks
CS2011 Introduction to Programming I Methods (II)
Writing Functions.
Namespaces How Shall I Name Thee?.
Chapter 6: User-Defined Functions I
Chapter 8 Technicalities: Functions, etc.
Pointers and Pointer-Based Strings
COMS 261 Computer Science I
Chapter 9: Pointers and String
CS1201: Programming Language 2
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
各題答對人數.
Pointers, Dynamic Data, and Reference Types
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Functions Chapter No. 5.
Presentation transcript:

#include "std_lib_facilities #include "std_lib_facilities.h"       // we find the declaration of cout in here int main() {           int i = 7;                                  // declaration of i           cout << f(i) << '\n'; } Will this compile? No, f not declared.

Int f(int x) needs definition, it was declared but not defined. #include "std_lib_facilities.h"       // we find the declaration of cout in here int f(int);                                           // declaration of f int main() {           int i = 7;                                  // declaration of i           cout << f(i) << '\n'; } Will this compile? Yes, but wil not link. Int f(int x) needs definition, it was declared but not defined. Declaration and definition are typically separate for readability, Also, remember expression() … expression() chain?

int f() { int x=4;           g(x);                  // error: g() isn’t (yet) in scope } void g(int k) {           k = f();                   // OK: f() is in scope } void h() {           int x = y;         // error: y isn’t (yet) in scope           int y = x;         // OK: x is in scope           g(y);                  // OK: g() is in scope } void g(int): // declaration of function g() int f() { int x=4;           g(x);                  // now ok: g() is in scope } void g() {           int k = f();                   // OK: f() is in scope } void h() { int y;           int x = y;         // now ok: y is delared           int y = x;         // OK: x is in scope           g(y);                  // OK: g() is in scope }

Always initialize to create object: void f(int z) {           int x;                              // uninitialized           // . . . no assignment to x here . . . int z = 8;           if (z>x) {           // . . .           }           // . . .           x = 7;                        // give x a value           // . . . } What is z>x?

Declaration and definition may differ: int my_find(vector<string> vs, string s, int)       // 3rd argument unused {           for (int i = 0; i<vs.size(); ++i)                     if (vs[i]==s) return i;           return –1; }

Why we may drop a parameter from function argument: int my_find(vector<string> vs, string s, int hint) // search for s in vs starting at hint {           if (hint<0 || vs.size()<=hint) hint = 0;           for (int i = hint; i<vs.size(); ++i)     // search starting from hint                     if (vs[i]==s) return i;           if (0<hint) {                                         // if we didn’t find s search before hint           for (int i = 0; i<hint; ++i)                     if (vs[i]==s) return i;           }           return –1; } No one is using the facility of “hint”, so: int my_find(vector<string> vs, string s, int)       // 3rd argument unused {           for (int i = 0; i<vs.size(); ++i)                     if (vs[i]==s) return i;           return –1; } Someone may still try to use it, but I am ignoring it now!

int max(int a, int b)            // max is global; a and b are local {           return (a>=b) ? a : b; } Write a function for choosing max of three integers: int max(int, int, int);

Scope: void f(int x)                  // f is global; x is local to f {           int z = x+7;        // z is local } int g(int x)                    // g is global; x is local to g {           int f = x+2;         // f is local           return 2*f; }

Three types of parameter passing: void f(int a, int& r, const int& cr) {           ++a;            // change the local a           ++r;            // change the object referred to by r           ++cr;          // error: cr is const }

void g(int a, int& r, const int& cr) {           ++a;                  // change the local a           ++r;                  // change the object referred to by r           int x = cr;         // read the object referred to by cr } int main() {           int x = 0;           int y = 0;           int z = 0;           g(x,y,z);      // x==0; y==1; z==0           g(1,2,3);      // error: reference argument r needs a variable to refer to           g(1,y,3);      // OK: since cr is const we can pass a literal } Use of parameter passing

1. Use pass-by-value to pass very small objects. 2. Use pass-by-const-reference to pass large objects that you don’t need to modify. 3. Return a result rather than modifying an object through a reference argument. 4. Use pass-by-reference only when you have to.

Function call stack:

Function call stack:

Function call stack:

Header (file) Function declaration vs. definition Function parameter passing by reference by value by const reference (read only) Call stack Constexpr: compile-time evaluation of simple function Mentally visualize memory usage: construction and destruction namespace

Review 1. What is the difference between a declaration and a definition? 2. How do we syntactically distinguish between a function declaration and a function definition? 3. How do we syntactically distinguish between a variable declaration and a variable definition? 4. Why can’t you use the functions in the calculator program from Chapter 6 without declaring them first? 5. Is int a; a definition or just a declaration? 6. Why is it a good idea to initialize variables as they are declared? 7. What can a function declaration consist of? 8. What good does indentation do? 9. What are header files used for? 10. What is the scope of a declaration? 11. What kinds of scope are there? Give an example of each. 12. What is the difference between a class scope and local scope? 13. Why should a programmer minimize the number of global variables? 14. What is the difference between pass-by-value and pass-by-reference? 15. What is the difference between pass-by-reference and pass-by-const-reference? 16. What is a swap()?

17. Would you ever define a function with a vector<double>-by-value parameter? 18. Give an example of undefined order of evaluation. Why can undefined order of evaluation be a problem? 19. What do x&&y and x||y, respectively, mean? 20. Which of the following is standard-conforming C++: functions within functions, functions within classes, classes within classes, classes within functions? 21. What goes into an activation record? 22. What is a call stack and why do we need one? 23. What is the purpose of a namespace? 24. How does a namespace differ from a class? 25. What is a using declaration? 26. Why should you avoid using directives in a header? 27. What is namespace std?