Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
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.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
1 Walter Savitch Chapter 3 Procedural Abstraction and Functions That Return a Value Slides by David B Teague, Western Carolina University, A constituent.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 ; Programmer-Defined Functions Two components of a function definition.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
Programming Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Chapter 4 Summation.
Functions:Passing Parameters by Value Programming.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Programmer-defined functions Development of simple functions using value parameters.
Computer Science 1620 Function Scope & Global Variables.
Local, Global Variables, and Scope. COMP104 Slide 2 Functions are ‘global’, variables are ‘local’ int main() { int x,y,z; … } int one(int x, …) { double.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Functions. COMP104 Lecture 13 / Slide 2 Function Prototype * The function prototype declares the interface, or input and output parameters of the function,
Function Part II: Some ‘advanced’ concepts on functions.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Functions Modules in C++ are called functions and classes
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 6: Functions.
Programming Functions: Passing Parameters by Reference.
Chapter 4 Procedural Abstraction and Functions That Return a Value.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Functions g g Data Flow g Scope local global part 4 part 4.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
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.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
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.
Introduction to Functions.  A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
FUNCTIONS - What Is A Function? - Advantages Function Declaration
User-Defined Functions (cont’d) - Reference Parameters.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
 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.
Fundamentals of structural programming
Predefined Functions Revisited
A Lecture for the c++ Course
FUNCTIONS IN C++.
Global & Local Identifiers
User-defined Functions
User-defined Functions
Anatomy of a Function Part 1
Pass by Reference.
Namespaces How Shall I Name Thee?.
Function “Inputs and Outputs”
Local, Global Variables, and Scope
The Function Prototype
Predefined Functions Revisited
Anatomy of a Function Part 1
Introduction to Functions
Presentation transcript:

Function 2

User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function definitions

Arguments/Parameters one-to-one correspondence between the arguments in a function call and the parameters in the function definition. int argument1; double argument2; // function call (in another function, such as main) result = thefunctionname(argument1, argument2); // function definition int thefunctionname(int parameter1, double parameter2){ // Now the function can use the two parameters // parameter1 = argument 1, parameter2 = argument2

Function Definition The function definition can be placed anywhere in the program after the function prototypes. If a function definition is placed in front of main(), there is no need to include its function prototype.

Pass by Value: Example // Print the sum and average of two numbers // Input: two numbers x & y // Output: sum - the sum of x & y // average - the average of x & y #include using namespace std; void PrintSumAve ( double, double ); int main ( ) { double x, y; cout << "Enter two numbers: "; cin >> x >> y; PrintSumAve ( x, y ); return 0; }

Pass by Value: Example void PrintSumAve (double no1, double no2) { double sum, average; sum = no1 + no2; average = sum / 2; cout << "The sum is " << sum << endl; cout << "The average is " << average << endl; }

Pass by Value: Example Data areas after call to PrintSumAve() :

Programming Scope of Identifiers

Scope A sequence of statements within { … } is considered a block of code. The part of the program where you can use a certain identifier is called the scope of that identifier. The scope of an identifier starts immediately after its declaration and ends when the “innermost” block of code within which it is declared ends. It is possible to declare the same identifier in another block within the program.

Scope  The scope of an identifier does not apply if the same identifier is declared in an inner block.  A global declaration of an identifier is made outside the bodies of all functions, including the main function. It is normally grouped with the other global declarations and placed at the beginning of the program file.  A local declaration of an identifier is made inside a block of code which could be the body of a function.  Globally declared identifiers can be accessed anywhere in the program.  Locally declared identifiers cannot be accessed outside of the block they were declared in.

int y = 38; void fun(int, int); int main( ){ int z=47; while(z<400){ int a = 90; z += a++; z++; } y = 2 * z; fun(1, 2); return 0; } void fun(int s, int t){ int r = 12; s = r + t; int i = 27; s += i; } Scope: Example 1 scope of i scope of r scope of s & t scope of a scope of z scope of y scope of fun

Example 2:Global Constants #include using namespace std; const double PI = ; double area(double radius); //Returns the area of a circle with the specified radius. double volume(double radius); //returns the volume of a sphere with the specified radius. int main(){ double radius_of_both, area_of_circle, volume_of_sphere. cout << " Enter a radius to use for both a circle " << " and a sphere (in inches): “ cin >> radius_of_both; area_of_circle = area(radius_of_both); volume_of_sphere = volume(radius_of_both);

Scope: Example 3 Number in Increment () is the global variable. #include using namespace std; int Number; //global variable void Increment(int IncNum) { IncNum = IncNum + 3; cout << IncNum << endl; Number = Number + 1; } int main() { Number = 1; Increment(Number); cout << Number << endl; return 0; }

Scope: Example int A,B,C,D; void Two(int A, int B, int& D) { B = 21; D = 23; cout << A << " " << B << " " << C<< " " << D << endl; } void One(int A, int B, int& C) { int D; // Local variable A = 10; B = 11; C = 12; D = 13; cout << A << " " << B<< " " << C<< " " << D << endl; Two(A,B,C); } int main() { A = 1; B = 2; C = 3; D = 4; One(A,B,C); cout << A << " " << B << " " << C<< " " << D << endl; Two(A,B,C); cout << A << " " <<B << " " << C << " " << D << endl; return 0; }

Scope: Example Output: ABCD in One = ABCD in Two = ABCD in Main = ABCD in Two = ABCD in Main =