Instructor - C. BoyleFall Semester - 2015

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Functions Prototypes, parameter passing, return values, activation frams.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Multiple-Subscripted Array
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
C++ Control Flow Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th -22 nd Sept 2006.
1 10/30/06CS150 Introduction to Computer Science 1 Functions Chapter 3, page 313.
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
1 Programming in C++ Dale/Weems/Headington Chapter 7 Functions.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
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.
CONTROLLING PROGRAM FLOW
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 Functions Chapter 7 2 Hope you can function! What is R2D2 doing here? What is his function? Who is Nibble? Can he function? IS he a function? Who is.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 Functions every C++ program must have a function called main program execution always begins with function main any other functions are subprograms and.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Previously Repetition Structures While, Do-While, For.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
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);
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Programming Principles II Lecture Notes 3.1 Void Functions Andreas Savva.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Suppose we want to print out the word MISSISSIPPI in big letters.
Chapter 10: Void Functions
A Lecture for the c++ Course
Programming Fundamental
New Structure Recall “average.cpp” program
Multiple Files Revisited
User Defined Functions
Functions I Creating a programming with small logical units of code.
Chapter 5 Function Basics
Value returning Functions
CS150 Introduction to Computer Science 1
Introduction to Programming
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
Multiple Files Revisited
CS150 Introduction to Computer Science 1
Chapter 10: Void Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions I Creating a programming with small logical units of code.
Presentation transcript:

Instructor - C. BoyleFall Semester

Terms invoke a function (call a function) construct a function (declare or write a function) value returning function void function parameter formal parameter actual parameter function header

Terms function body return statement return type function prototype flow of execution value parameters reference parameters local variables

4 Coding Cable Company Program –Ver 1IF ELSE –Ver 2SWITCH –Ver 3Functions

5 Coding Some functions return a value Exercises 4, 9, 10, chapter 6 –Ch6_Exer4.cpp –Ch6_Exer9_and_10 Value returning functions are used in an expression In these examples the parameters are passed by value

6 Coding Some functions are void functions In function printer, the parameters are passed by value –PrinterCalls.cpp // method printer will print out the symbol number times on a single line. void printer (int number, char symbol) { int counter; //local counter for loop control counter = 1; while (counter <= number ) { counter++; cout << symbol; } }// end of printer { {

7 Coding Some functions are void functions In function printer, the parameters are passed by value –PrinterCalls.cpp void printer (int, char); void treePrinter () ; void diamondPrinter () ; void rectanglePrinter ( int across, int down); const char BLANK =' ' ; const char PLUS ='+' ; const char STAR ='*' ; const char POUND ='#' ;

8 Coding Some functions are void functions In function printer, the parameters are passed by value –PrinterCalls.cpp // call printer several times printer ( 6,'%'); cout << endl; printer ( 12, 'A'); cout << endl; treePrinter () ; cout << endl << endl; system ( "pause");

9 Coding Some functions are void functions In function printer, the parameters are passed by value –PrinterCalls.cpp // call diamondPrinter, and rectanglePrinter functions diamondPrinter () ; cout << endl << endl; system (“pause”); rectanglePrinter ( 10, 4); rectanglePrinter ( 4,10 ) ; cout << endl << endl; system ("pause"); return 0; // end of main

10 Coding Some functions are void functions In function printer, the parameters are passed by value –PrinterCalls.cpp void printer(int number, char symbol) // method printer will print out the symbol number times on a single line. { int counter; //local counter for loop control counter = 1; while (counter <= number ) { counter++; cout << symbol; } } // end of printer

11 Coding Some functions are void functions In function printer, the parameters are passed by value –PrinterCalls.cpp void treePrinter () { cout << endl << endl; printer (9,BLANK); printer (1,STAR); cout << endl; printer (8,BLANK); printer (3,STAR); cout << endl; printer (7,BLANK); printer (5,STAR); ……………………………. ………. printer (8,BLANK); printer (3,POUND); cout << endl; printer (8,BLANK); printer (3,POUND); cout << endl; } // end of printer

12 Coding Some functions are void functions In function printer, the parameters are passed by value –PrinterCalls.cpp // print a diamond shape { int bCount ; int lCount ; ……………….. See the code….. // print a diamond shape void rectanglePrinter ( int across, int down) { for ( int d = 1 ; d <= down ; d++ ) { printer (across, '#'); cout << endl; } } // end of rectanglePrinter

13 Coding Some functions are void functions Some functions pass the parameters passed by value Some functions pass the parameters passed by reference –Value_or_Reference.cpp //A swap that doesn't really swap! void up(int a) { a = a + 5; cout << "up a = " << a << endl; } //A swap that really swaps! void down(int& x) { x = x - 5; cout << "down x = " << x<< endl; }

void swap1 (int a, int b) { int temp; temp = a; a = b; b = temp; }

15 Coding Some functions are void functions Some functions pass the parameters - by value Some functions pass the parameters - by reference –Swap_it.cpp //A swap that doesn't really swap! void swap1 (int a, int b) { int temp; temp = a; a = b; b = temp; } //A swap that really swaps! void swap2 (int& a, int& b) { int temp; temp = a; a = b; b = temp; }

Chapter 6 Write a program to input and calculate the cost of ODU football tickets. Use functions. How many? Student prices Adult prices Senior prices

Summary 1 Project 2 Quizzes….. Read Chapter 6 - Functions! Again!

QUESTIONS?