User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers.

Slides:



Advertisements
Similar presentations
The Software Lifecycle. Example Problem: Update a Checkbook Write a program that allows the user to enter a starting balance, a transaction type, D or.
Advertisements

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:
CSE Lecture 10 – Functions
Chapter Five Functions
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations.
Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Writing and Testing Programs Drivers and Stubs Supplement to text.
1 Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Overview creating your own functions calling your own functions.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
CS1061 C Programming Lecture 10: Macros, Casting and Intro. to Standard Library A. O’Riordan, 2004.
CS 201 Functions Debzani Deb.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Programming is instructing a computer to perform a task for you with the help of a programming language.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store.
Chapter 4 Procedural Abstraction and Functions That Return a Value.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:
Chapter 06 (Part I) Functions and an Introduction to Recursion.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
Unit 3 Lesson 11 Passing Data and Using Library Functions Textbook Authors: Knowlton, Barksdale, Turner, & Collings PowerPoint Lecture by Dave Clausen.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to simple functions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Function PrototypetMyn1 Function Prototype We can declare a function before we use or define it by means of a function prototype. A function prototype.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
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.
Chapter 6: User-Defined Functions I
Engineering Problem Solving With C An Object Based Approach
CMPT 201 Functions.
CSCI 161: Introduction to Programming Function
Chapter 4: Subprograms Functions for Problem Solving
User-defined Functions
Functions Inputs Output
User-defined Functions
Modular Programming with Functions
Chapter 6: User-Defined Functions I
See requirements for practice program on next slide.
FUNCTION.
Fundamental Programming
Predefined Functions Revisited
Single-Result Functions & Modularity
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Introduction to Methods and Interfaces
Presentation transcript:

User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers

Why Define Functions? Break complex tasks into simpler subtasks Avoid writing one piece of code more than once (enhances maintenance) Use names for tasks (enhances readability and organization)

Function Declarations Provide enough information to use a function Do not reveal implementation details Occur above the main function or in a library header file

Example Declaration // Function: cube // Computes the cube of a number // Input: a real number // Output: a real number representing the cube // of the input double cube(double n); Use a prefatory comment to describe the task, the inputs (parameters), and output (return value)

Example Implementation double cube(double n) { return n * n * n; } Can occur below or above the main function or in a library implementation file

Example Use in Complete Program #include double cube(double n); int main() { cout << cube(3) << endl; return 0; } double cube(double n) { return n * n * n; }

Formal and Actual Parameters A formal parameter appears in the functions header (definition) An actual parameter appears in the functions call (use)

#include double cube(double n); int main() { cout << cube(3) << endl; return 0; } double cube(double n) { return n * n * n; } actual parameter formal parameter Formal and Actual Parameters

#include double cube(double n); int main() { cout << cube(3 + 5) << endl; return 0; } double cube(double n) { return n * n * n; } actual parameter formal parameter Formal and Actual Parameters

Return Values and void Functions If a function has a single output datum, use a return statement with that value and declare the appropriate return type If a function has no output data or more than one output datum, use the void return type void displayResults(double total) double cube(double n)

Value and Reference Parameters Use a value parameter when the data should be input-only Use a reference parameter when the data should be input/output

Value Parameters The computer –evaluates the actual parameter –places a copy of that value in a temporary memory location –leaves the memory location of the actual parameter unchanged double x = 9.5; double y = cube(x); double cube(double n);

Reference Parameters The computer –treats the formal parameter as an alias for actual parameters memory location –allows uses and modifications of the value in that location through the formal parameter double x, y; getData(x, y); void getData(double &a, double &b);

Constant Reference Parameters The computer –treats the formal parameter as an alias for actual parameters memory location –allows uses but no modifications of the value in that location through the formal parameter double x; getData("Enter a number:", y); void getData(const apstring &a, double &b);

The Scope of a Variable The scope of a variable is the area of program text in which the variable is associated with a particular value Nested blocks {} introduce local scopes Visibility extends inwards but not outwards

#include double cube(double n); int main() { int x = 3; cout << cube(x) << endl; return 0; } double cube(double n) { return n * n * n; } Scope and Contour Diagrams Outermost block contains global names (cube) They are visible in nested blocks

#include double cube(double n); int main() { int x = 3; cout << cube(x) << endl; return 0; } double cube(double n) { return n * n * n; } Scope and Contour Diagrams Nested blocks contain local names They are visible in the block where theyre declared

#include double cube(double n); int main() { int x = 3; cout << cube(x) << endl; return 0; } double cube(double n) { int x = n * n * n; return x; } Scope and Contour Diagrams Parameters and local variables are visible only in nested blocks The variable x refers to two distinct memory locations in separate blocks

// Library header file: myinput.h #ifndef MY_INPUT #define MY_INPUT #include "apstring.h" // Function: getInteger // Prompts user for an integer, inputs it, and returns it // // Input: A string representing the prompt // Output: The integer input by the user int getInteger(const apstring &prompt); // Function: getString // Prompts user for a string, inputs it, and returns it // // Input: A string representing the prompt // Output: The string input by the user apstring getString(const apstring &prompt); #endif Library Header File

// Library implementation file: myinput.cpp #include "myinput.h" int getInteger(const apstring &prompt) { int data; cout << prompt; cin >> data; return data; } apstring getString(const apstring &prompt) { apstring data; cout << prompt; cin >> data; return data; } Library Implementation File