CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations.

Slides:



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

Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
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 ; }
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.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
 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.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
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.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
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.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Seventh Edition.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
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.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
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.
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 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.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
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.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
 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 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
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.
Chapter 6: User-Defined Functions I
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Dr. Shady Yehia Elmashad
Chapter 7: User-Defined Functions II
A Lecture for the c++ Course
Dr. Shady Yehia Elmashad
User-Defined Functions
Dr. Shady Yehia Elmashad
Parameter Passing in Java
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 General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

More on functions… Functions can be placed –after boilerplate but before main() simplest –after 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);

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!

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);

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!