Lecture 12: Functions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.

Slides:



Advertisements
Similar presentations
1 Programming in C++ Lecture Notes 9 Functions (Returning Values) Andreas Savva.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
Starting Out with C++, 3 rd Edition 1 Chapter 5. Looping.
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)
Starting Out with C++, 3 rd Edition 1 Chapter 6 Functions.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Chapter 6: Functions.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Chapter 4 Program Control Statements
CSC 221: Computer Programming I Fall 2001  top-down design  approach to problem solving, program design  focus on tasks, subtasks, …  reference parameters.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions Continued.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
CS102 Introduction to Computer Programming Chapter 6 Functions Part I.
Starting Out with C++: From Control Structures through Objects 7 th edition By Tony Gaddis Source Code Chapter 4.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
CS102 Introduction to Computer Programming Chapter 9 Pointers.
Lecture 8: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Methods Chapter Why Write Methods? Methods are commonly used to break a problem down into small manageable pieces. This is called divide and conquer.
Lecture 9: Making Decisions Final Section Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Lecture 9: Making Decisions Final Section Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Starting Out with C++: From Control Structures through Objects
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Chapter 5 : Methods. Why Write Methods?  Methods are commonly used to break a problem down into small manageable pieces. This is called divide and conquer.
CS102 Introduction to Computer Programming Chapter 5 Looping.
Starting Out with Java: From Control Structures through Objects 5 th edition By Tony Gaddis Source Code: Chapter 5.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Lecture 12: Functions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
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.
Starting Out with C++, 3 rd Edition 1 Chapter 5. Looping.
Functions Week 10 & 11. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or.
1 C++ Classes and Data Structures Course link…..
Starting Out with C++: From Control Structures through Objects
Chapter 6 Functions.
Chapter 5. Looping.
Person 1 Translate to Indonesia & presentation Start Next Week
Addis Ababa Institute of Technology Yared Semu May 2012
Function Basics.
Introduction to Functions
Chapter 5. Looping.
Functions.
6 Chapter Functions.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Instructor: Hidayah Elias Course: COMPUTER PROGRAMMING (BFC 2042)
Presentation transcript:

Lecture 12: Functions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220

Modular Programming Concept: A program may be broken up into manageable functions, or groups of statements int main() { statement; } int main() { statement; } void function2() { statement; } int function3() { statement; }

Defining and Calling Functions Concept: A function call is a statement that causes a function to execute. A function definition contains the statements that make up the function A function has the following parts Return type: Name: Parameter list: Can be empty Body: int main(int arg1) { cout<<“Hello World\n”; return 0; }

Void Functions and Calling Void functions do not return anything void myFunction(){cout<<“I don’t return anything;} Function header void myFunction() Function Call myFunction();

Examples // This program has two functions: main and displayMessage #include using namespace std; //***************************************** // Definition of function displayMessage * // This function displays a greeting. * //***************************************** void displayMessage() { cout << "Hello from the function displayMessage.\n"; } //***************************************** // Function main * //***************************************** int main() { cout << "Hello from main.\n"; displayMessage(); cout << "Back in function main again.\n"; return 0; } // The function displayMessage is repeatedly called from a loop. #include using namespace std; //***************************************** // Definition of function displayMessage * // This function displays a greeting. * //***************************************** void displayMessage() { cout << "Hello from the function displayMessage.\n"; } //***************************************** // Function main * //***************************************** int main() { cout << "Hello from main.\n"; for (int count = 0; count < 5; count++) displayMessage(); // Call displayMessage cout << "Back in function main again.\n"; return 0; }

// This program has three functions: main, first, and second. #include using namespace std; //***************************************** // Definition of function first * // This function displays a message. * //***************************************** void first() { cout << "I am now inside the function first.\n"; } //***************************************** // Definition of function second * // This function displays a message. * //***************************************** void second() { cout << "I am now inside the function second.\n"; } //***************************************** // Function main * //***************************************** int main() { cout << "I am starting in function main.\n"; first(); // Call function first second(); // Call function second cout << "Back in function main again.\n"; return 0; } // This program has three functions: main, deep, and deeper #include using namespace std; //***************************************** // Definition of function deeper * // This function displays a message. * //***************************************** void deeper() { cout << "I am now inside the function deeper.\n"; } //***************************************** // Definition of function deep * // This function displays a message. * //***************************************** void deep() { cout << "I am now inside the function deep.\n"; deeper(); // Call function deeper cout << "Now I am back in deep.\n"; } //***************************************** // Function main * //***************************************** int main() { cout << "I am starting in function main.\n"; deep(); // Call function deep cout << "Back in function main again.\n"; return 0; }

Function Prototypes Concept: A function prototype eliminates the need to place a function definition before all calls to the function. Function prototype(declaration) is placed before the call, and the function itself is defined after the call Similar to the function header, except for the semicolon void displayMessage();

// This program has three functions: main, First, and Second. #include using namespace std; // Function Prototypes void first(); void second(); int main() { cout << "I am starting in function main.\n"; first(); // Call function first second(); // Call function second cout << "Back in function main again.\n"; return 0; } //************************************* // Definition of function first. * // This function displays a message. * //************************************* void first() { cout << "I am now inside the function first.\n"; } //************************************* // Definition of function second. * // This function displays a message. * //************************************* void second() { cout << "I am now inside the function second.\n"; }

Sending Data into a function Concept: When a function is called, the program may send values into the function These values are called arguments Example: result = pow(2.0,4.0); Parameter: a special variable to hold the arguments as they are passed into the function void displayValue(int num); num is the parameter

// This program demonstrates a function with a parameter. #include using namespace std; // Function Prototype void displayValue(int); int main() { cout << "I am passing 5 to displayValue.\n"; displayValue(5); // Call displayValue with argument 5 cout << "Now I am back in main.\n"; return 0; } //********************************************************* // Definition of function displayValue. * // It uses an integer parameter whose value is displayed. * //********************************************************* void displayValue(int num) { cout << "The value is " << num << endl; } // This program demonstrates a function with a parameter. #include using namespace std; // Function Prototype void displayValue(int); int main() { cout << "I am passing several values to displayValue.\n"; displayValue(5); // Call displayValue with argument 5 displayValue(10); // Call displayValue with argument 10 displayValue(2); // Call displayValue with argument 2 displayValue(16); // Call displayValue with argument 16 cout << "Now I am back in main.\n"; return 0; } //********************************************************* // Definition of function displayValue. * // It uses an integer parameter whose value is displayed. * //********************************************************* void displayValue(int num) { cout << "The value is " << num << endl; }

// This program demonstrates a function with three parameters. #include using namespace std; // Function Prototype void showSum(int, int, int); int main() { int value1, value2, value3; // Get three integers. cout << "Enter three integers and I will display "; cout << "their sum: "; cin >> value1 >> value2 >> value3; // Call showSum passing three arguments. showSum(value1, value2, value3); return 0; } //************************************************************ // Definition of function showSum. * // It uses three integer parameters. Their sum is displayed. * //************************************************************ void showSum(int num1, int num2, int num3) { cout << (num1 + num2 + num3) << endl; }

Passing Data By Value When an argument is passed into a parameter, only a copy of the argument’s value is passed. Changes to the parameter do not affect the original argument. // This program demonstrates that changes to a function parameter // have no affect on the original argument. #include using namespace std; // Function Prototype void changeMe(int); int main() { int number = 12; // Display the value in number. cout << "number is " << number << endl; // Call changeMe, passing the value in number // as an argument. changeMe(number); // Display the value in number again. cout << "Now back in main again, the value of "; cout << "number is " << number << endl; return 0; } //******************************************* ******************* // Definition of function changeMe. * // This function changes the value of the parameter myValue. * //******************************************* ******************* void changeMe(int myValue) { // Change the value of myValue to 0. myValue = 0; // Display the value in myValue. cout << "Now the value is " << myValue << endl; }

Using Functions in a Menu- Driven Program // This is a menu-driven program that makes a function call // for each selection the user makes. #include using namespace std; // Function prototypes void showMenu(); void showFees(double, int); int main() { int choice; // To hold a menu choice int months; // To hold a number of months // Constants for membership rates const double ADULT = 40.0; const double SENIOR = 30.0; const double CHILD = 20.0; // Set up numeric output formatting. cout << fixed << showpoint << setprecision(2); do { // Display the menu and get the user's choice. showMenu(); cin >> choice; // Validate the menu selection. while (choice 4) { cout << "Please enter 1, 2, 3, or 4: "; cin >> choice; } if (choice != 4) { // Get the number of months. cout << "For how many months? "; cin >> months; // Display the membership fees. switch (choice) { case 1: showFees(ADULT, months); break; case 2: showFees(CHILD, months); break; case 3: showFees(SENIOR, months); } } while (choice != 4); return 0; } //***************************************************************** // Definition of function showMenu which displays the menu. * //***************************************************************** void showMenu() { cout << "\n\t\tHealth Club Membership Menu\n\n"; cout << "1. Standard Adult Membership\n"; cout << "2. Child Membership\n"; cout << "3. Senior Citizen Membership\n"; cout << "4. Quit the Program\n\n"; cout << "Enter your choice: "; } //***************************************************************** // Definition of function showFees. The memberRate parameter * // the monthly membership rate and the months parameter holds the * // number of months. The function displays the total charges. * //***************************************************************** void showFees(double memberRate, int months) { cout << "The total charges are $" << (memberRate * months) << endl; }

The return Statement Concept: The return statement causes a function to end immediately. // This program uses a function to perform division. If division // by zero is detected, the function returns. #include using namespace std; // Function prototype. void divide(double, double); int main() { double num1, num2; cout << "Enter two numbers and I will divide the first\n"; cout << "number by the second number: "; cin >> num1 >> num2; divide(num1, num2); return 0; } //*************************************************************** // Definition of function divide. * // Uses two parameters: arg1 and arg2. The function divides arg1* // by arg2 and shows the result. If arg2 is zero, however, the * // function returns. * //*************************************************************** void divide(double arg1, double arg2) { if (arg2 == 0.0) { cout << "Sorry, I cannot divide by zero.\n"; return; } cout << "The quotient is " << (arg1 / arg2) << endl; }

Returning a Value from a Function A function may send a value back to the part of the program that called the function Return Type int main(int arg1) { cout<<“Hello World\n”; return 0; } int sum(int num1, int num2) { int result; result = num1 + num2; return result; }

// This program uses a function that returns a value. #include using namespace std; // Function prototype int sum(int, int); int main() { int value1 = 20, // The first value value2 = 40, // The second value total; // To hold the total // Call the sum function, passing the contents of // value1 and value2 as arguments. Assign the return // value to the total variable. total = sum(value1, value2); // Display the sum of the values. cout << "The sum of " << value1 << " and " << value2 << " is " << total << endl; return 0; } //***************************************************** // Definition of function sum. This function returns * // the sum of its two parameters. * //***************************************************** int sum(int num1, int num2) { return num1 + num2; } // This program demonstrates two value- returning functions. // The square function is called in a mathematical statement. #include using namespace std; //Function prototypes double getRadius(); double square(double); int main() { const double PI = ; // Constant for pi double radius; // To hold the circle's radius double area; // To hold the circle's area // Set the numeric output formatting. cout << fixed << showpoint << setprecision(2); // Get the radius of the circle. cout << "This program calculates the area of "; cout << "a circle.\n"; radius = getRadius(); // Caclulate the area of the circle. area = PI * square(radius); // Display the area. cout << "The area is " << area << endl; return 0; } //*************************************************** *** // Definition of function getRadius. * // This function asks the user to enter the radius of * // the circle and then returns that number as a double.* //*************************************************** *** double getRadius() { double rad; cout << "Enter the radius of the circle: "; cin >> rad; return rad; } //*************************************************** *** // Definition of function square. * // This function accepts a double argument and returns * // the square of the argument as a double. * //*************************************************** *** double square(double number) { return number * number; }