CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.

Slides:



Advertisements
Similar presentations
Modular Programming With Functions
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Overview creating your own functions calling your own functions.
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
CS 117 Spring 2002 Functions Hanly - Chapter 5 Friedman-Koffman - Sections & Chapter 6.
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
CS 1400 March 21, 2007 Odds and Ends Review. Reading to the end of a file Example test.txt : … Suppose we need.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
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 7 Functions.
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Chapter 6: Functions Starting Out with C++ Early Objects
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Variable Scope Storage Class Recursion
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Seventh Edition.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
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.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
CSCI 161 Lecture 14 Martin van Bommel. New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
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.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Reference and Value Parameters Reference Parameters (&) The formal parameter receives the address of the actual parameter, and the function can read and.
Chapter 6: User-Defined Functions I
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Dr. Shady Yehia Elmashad
A Lecture for the c++ Course
Dr. Shady Yehia Elmashad
User-Defined Functions
Dr. Shady Yehia Elmashad
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
CS150 Introduction to Computer Science 1
Pass by Reference.
Chapter 6: User-Defined Functions I
Variables have attributes
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Presentation transcript:

CS 1400 Chap 6 Functions

Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments

Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations – or it can be empty

Example: my_abs() int my_abs (int value) {int answer; if (value < 0) answer = -value; else answer = value; return answer; } number passed into the function number passed back from the function

Example: main program int main () {int number, answer; cout << “enter a number: “; cin >> number; answer = my_abs (number); cout << “absolute value is: “ << answer << endl; }

More on functions… Functions can be placed –after boilerplate but before main() simplest –below main() covered later –in another file covered later A function has its own variables!

types… The return value of a function must match the function type. Arguments passed to function parameters must match in type and in position. if either of these rules is neglected, the computer will force (cast) a match.

Value passing… int MyFunc (float a, int b) { int temp = x+y; return temp; } int main() { float x=5.1; int z, y=21; z = MyFunc (x, y);

Value passing diagrams… main() MyFunc() z x y a b temp MyFunc (x, y); 5.121

Examples: FunctionExample Squarecout << Square (21.6); Cubey = Cube (z); Maxcout << Max (a, b); Absy = Abs (x); PrintHeadPrintHead(); GetBetweeny = GetBetween (21, 80); Roundcout << Round (3.567);

Quiz Write a void function that accepts two integer parameters and puts them in numerical order. In other words, if the first is less than the second, their values are exchanged. Example use: int a, b; cout << “Enter two integers: “; cin >> a >> b; Order (a, b); cout << “In order: “ << a << “, “ << b << endl;

Global variables… Variables declared outside of main() or a function are globally accessible. int x; int main() { cout << x; … Local variables may eclipse global variables int x; int main() { int x; cout << x; …

Static variables… Static variables retain their values when a function exits and are only created and initialized once void MyFunc () { static int counter = 0; cout << counter++; } int main() {MyFunc();// outputs 0 MyFunc();// outputs 1 MyFunc();// outputs 2…

Reference parameters… A function using reference parameters can modify corresponding arguments void Swap (int &a, int &b) { int temp = a; a = b; b = temp; } int main() { … Swap (x, y); Swap (cost, rate);

The value passed or copied into a reference parameter is a forwarding address… main() Swap() x y cost rate a b see x in main() see y in main() temp Swap (x, y);

Results… main() Swap() x y cost rate a b see x in main() see y in main() temp

main() Swap() x y cost rate a b see cost in main() see rate in main() temp Swap (cost, rate);

main() Swap() x y cost rate a b see cost in main() see rate in main() temp Results… 3912

Example: Max() The function Max() is intended to determine the maximum value of a list of N numbers input by the user (The argument N is provided by the caller).

Example: MaxMin() The function MaxMin() is intended to determine two values; the maximum and minimum values of a list of 10 numbers input by the user

Functions need not return a value! If a function does not return a value, its type is void and it does not need a return void MyFunc ( int a ) { cout << “the value is: “ << a; cout << “and the square is: “ << a*a; }

Alternate function locations… Functions can be placed below calling functions if the function prototype is above the other functions that call it. A prototype is the first line or title of a function – followed by a semicolon. Functions can be placed in a separate file if the function prototype is above other functions that call it and the function file is included in the project

Example A void Swap (int &a, int &b) { int temp = a; a = b; b = temp; } int main() { …// function Swap() is used here return 0; }

Example B void Swap (int &a, int &b); // function prototype int main() { … // function Swap() is used here return 0; } void Swap (int &a, int &b) { int temp = a; a = b; b = temp; }

Example C FILE A.cpp: void Swap (int &a, int &b); int main() { … // Swap used here return 0; } FILE B.cpp: void Swap (int &a, int &b) { int temp = a; a = b; b = temp; }

Overloading functions… Several functions may have the same name Each function must have a unique signature –name –number and type of parameters Note that the return type of a function is not part of its signature!

Examples… cout << Sum (a, b, c); cout << Sum (a, b); cout << Sum (a, b, c, d);

Default Arguments A function may specify default values for parameters in its prototype. Example: Example function calls: int TotalPrice (float item_cost; float taxrate = 0.065; int count=1); cout << TotalPrice (5.98); cout << TotalPrice (7.95, 0.075); cout << TotalPrice (3.56, 0.055, 3);

Default Arguments… Only the last or ending arguments may have default values Illegal: cout << TotalCost (4.68,, 4); // ERROR!!

Reference Parameters (bis) All arguments for reference parameters must be simple variables – expressions are not allowed; int MyFunc (int x, int& y); … z = MyFunc (5+a, 7*b); OK Illegal!

Example project Calculate the average of a series of test scores. The input file consists of many students in following format; Fred (name) (count) (scores) For each student, the output should be the name, average, maximum, and minimum; Fred: average 63.3 max: 96 min: 25