Chapter 8 Functions in Depth. Chapter 8 A programmer-defined function is a block of statements, or a subprogram, that is written to perform a specific.

Slides:



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

Spring Semester 2013 Lecture 5
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS 201 Functions Debzani Deb.
Top-Down Design with Functions 4 What do programmer’s (not programs!) use as input to the design of a program? –Documentation Problem definition Requirements.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Chapter 6: Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
C++ for Engineers and Scientists Third Edition
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
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.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
Chapter 10 Introduction to Classes
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 7 Functions CS185/09 - Introduction to Programming Caldwell College.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
TMC1414/TMC1413 I NTRODUCTION T O P ROGRAMMING Lecture 06 Function.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
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.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 ICS103 Programming in C Lecture 8: Functions I.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
C++ Programming Lecture 12 Functions – Part IV
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objectives  Function Templates  Recursion Functions.
Functions + Overloading + Scope
User-Written Functions
Chapter 5 Function Basics
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 Function Basics
Group Status Project Status.
A function is a group of statements that exist within a program for the purpose of performing a specific task. We can use functions to divide and conquer.
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

Chapter 8 Functions in Depth

Chapter 8 A programmer-defined function is a block of statements, or a subprogram, that is written to perform a specific task required by you, the programmer.

Chapter 8 Functions eliminate the need for duplicate statements within a program. Given a task to be performed more than once, the statements are written just once for the function. Then, the function is called each time the task must be performed. Functions make the program easier to design, since the program behaviors (algorithms) are modularized as individual functions, whose combined execution solves the problem at hand.

Chapter 8 Functions make the program easier to code and test, since you can concentrate on a single function at a time, rather than one big main() function. Functions allow for software reusability, where existing functions can be reused to create new programs. Functions make the program more clear and readable, thus making the program easier to maintain.

Chapter 8 Functions provide the basis for structured as well as object-oriented programming

Chapter 8 A non-void function is a function that, when called, returns a single value to the calling program.

Chapter 8 Non-void Function Format //FUNCTION HEADER return data type function name (parameter list) { //BEGIN FUNCTION STATEMENT BLOCK //LOCAL VARS local variables should go here //FUNCTION STATEMENTS or BODY statement #1; statement #2; statement #n; return value; } //END FUNCTION STATEMENT BLOCK

Chapter 8 The function header forms the interface between the calling program and the function.

Chapter 8  The function header consists of a return type, if any, the function name, and a parameter listing.

Chapter 8 The value returned by a non-void function replaces the function name wherever the name is used in the calling program. Examples: result = cube(a); cout << cube(a); solution = 2  cube(a) + 5;

Chapter 8 Arguments are values/variables used within the function call, and parameters are variables used within the function header that receive the argument values.

Chapter 8 A void function is a function that returns multiple values or performs some specific task, rather than returning a single value to the calling program.

Chapter 8 When calling a void function, simply list the function name and required arguments as a single statement within your program. Do not call a void function with an assignment operator or cout object as you do non-void functions. Example: displayHeader();

Chapter 8 A value parameter provides for one-way communication of data from the calling program to the function. A reference parameter provides two-way communication of data between the calling program and function.

Chapter 8 Locating Functions in a Program

Chapter 8 A function prototype is a model of the interface to the function. Simply copy/paste the function header and add a semicolon to code the prototype. Locate your function prototypes after the preprocessor directives and before main().

Chapter 8 A default parameter is a function parameter that is assigned a default value in either the function prototype or the function header, but not both. Example: int volume(int length, int width = 5, int height = 2);

Chapter 8 An overloaded function exhibits different behavior with a different number of arguments or argument data types. Example: i nt calculateArea(int); int calculateArea(int, int); double calculateArea(double);

Chapter 8 A local variable is a variable that is defined within a specific block of code, such as a function. The scope of a variable refers to the largest block in which a given variable is accessible. Always define your variables as locally as possible within a given function block. If variables need to be shared among functions, pass them.

Chapter 8 Recursion is a process whereby an operation calls and clones itself until a terminating condition is reached. Examples of recursive operations include compound interest calculation, summation, and factorial, just to mention a few.

Chapter 8 Calculating a Monthly Compounded Bank Balance using Recursion If n is 0 balance(n) = deposit Else balance(n) = (1+rate/12/100)  balance(n–1) (Where n is the month for which the balance must be determined.)

Chapter 8 A recursive function must always reach a terminating condition. If it does not, the function will keep calling itself forever, resulting in a memory overflow run-time error.

Chapter 8 double balance(double deposit, int month, double rate) { if (month == 0) return deposit; else return (1 + rate / 12 / 100) * balance(deposit,month – 1,rate); } //END balance()

Chapter 8

If n is 1 sum(n) = 1 Else sum(n) = n + sum (n – 1)

Chapter 8 int sum(int n) { if (n == 1) return 1; else return n + sum(n  1); } //END sum()

Chapter 8 int sum(int n) { int subTotal = 0;//SUM SUBTOTAL for (int count = 1; count <= n; ++count) subTotal = subTotal + count; return subTotal; } //END sum()