1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006.

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.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
1 Lecture 14:User-Definded function I Introduction to Computer Science Spring 2006.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Chapter 7 Functions.
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.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
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.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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.
CPS120: Introduction to Computer Science Functions.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Learners Support Publications Functions in C++
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
CHAPTER 6 USER-DEFINED FUNCTIONS Made By- Kartik Belwal.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
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.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
1 Functions Part 1 Prototypes Arguments Overloading Return values.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
User-Written Functions
Chapter 7: User-Defined Functions II
Functions and an Introduction to Recursion
A Lecture for the c++ Course
Pointers and Pointer-Based Strings
User-Defined Functions
Extra.
CS150 Introduction to Computer Science 1
Pointers & Functions.
Chapter 7: User-Defined Functions II
Functions and an Introduction to Recursion
Introduction To Programming
Pointers and Pointer-Based Strings
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Pointers & Functions.
Presentation transcript:

1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006

2 User-Defined Functions Void functions: do not have a data type Value-returning functions: have a data type

3 #include using namespace std; int main() { int i, j; int k; cin>>i>>j; if (i > j) { k = i; } else { k = j; } cout << "The max is " << k << endl; return 0; } #include using namespace std; /* Function Declarations */ int FindMax(int n1, int n2); void PrintMax(int someNumber); int main() { int i, j; int k; cin>>i>>j; k = FindMax(i,j); PrintMax(k); // Prints Max Value return 0; } /* Function Definitions */ int FindMax(int n1, int n2) { if (n1 > n2) { return n1; } else { return n2; } /* Function Definitions */ void PrintMax(int someNumber) { cout << "The max is " << someNumber << endl; } Value-returning functions Void functions

4 #include using namespace std; int main() { float tempInF = 85.0; float tempInC; cout << "Hello World" << endl; float factor = 5./9.; float freezing = 32.0; tempInC = factor * (tempInF - freezing); cout << tempInF << " Fahrenheit equals " << tempInC << " Celsius " << endl; return 0; } #include using namespace std; /* Function Declarations */ void PrintHW(); float FtoC(float faren); int main() { float tempInF = 85.0; float tempInC; PrintHW(); /* Prints Hello World */ tempInC = FtoC(tempInF); cout << tempInF << " Fahrenheit equals " << tempInC << " Celsius " << endl; return 0; } /* Function Definitions */ float FtoC(float faren) { float factor = 5./9.; float freezing = 32.0; float celsius; celsius = factor * (faren - freezing); return celsius; } /* Function Definitions */ void PrintHW() { cout << "Hello World" << endl; } Value-returning functions Void functions

5 Void Functions: function definition Void functions and value-returning functions have similar structures: Heading: 1. Name of the function 2. Number of parameters 3. Data type of each parameter 4. Function Type (type of the value returned by the function) Body: 1. Code required to accomplish the task (the body of the function) The syntax of the function definition is: void functionName(formal parameter list) { statements } void is a reserved word A void function does not have a data type The syntax of the formal parameter list is: dataType identifier, dataType identifier,... Formal parameters are optional: void functionName() { statements }

6 Void Functions: function definition void PrintMax(int someNumber) { cout << "The max is " << someNumber << endl; } Function Heading void PrintMax(int someNumber) Function Body Function Name Formal parameter list void PrintHW() { cout << "Hello World" << endl; } Function Heading Function Body

7 Void Functions: call a void function To call a void function: Use its name, with the actual parameters (if any) in parentheses The return statement without any value is typically used to exit the function early A call to a void function is a stand-alone statement The syntax for a function call is: functionName(actual parameter list) functionName() (If no formal parameters in function definition) The syntax for the actual parameter list is: expression or variable,expression or variable,...

8 Actual parameter list Call a void Function with parameters #include using namespace std; /* Function Declarations */ int FindMax(int n1, int n2); void PrintMax(int someNumber); int main() { int i, j; int k; cin>>i>>j; k = FindMax(i,j); PrintMax( k ); return 0; } /* Function Definitions */ void PrintMax( int someNumber) { cout << "The max is " << someNumber << endl; } /* Function Definitions */ int FindMax(int n1, int n2) { if (n1 > n2) { return n1; } else { return n2; } Formal parameter list

9 Call a void Function without parameters #include using namespace std; /* Function Declarations */ void PrintHW(); int main() { /* Prints Hello World */ PrintHW(); return 0; } /* Function Definitions */ void PrintHW() { cout << "Hello World" << endl; }

10 Question How many values can be returned by void function? How many values can be returned by value- returning function? What if we want to return multiple values from the funtion? Answer1: Pass by reference (Using reference variables as parameters Answer2: pass a pointer to the variable to the functionpointer (will be covered later)

11 Reference Referencing is generally used in a wider context - in the context of "aliasing“. The "&" operator is used for referencing - meaning "reference to". This is of the form: Datatype& variable_name = initialisation_expression For example: int i = 10; int& j = i; // j is an alias for i Both i and j refer to the same object - modifying i is equivalent to modifying j, and vice versa. That is, a reference is an alias for an object and does not, itself, occupy any memory.

12 Reference (cont.) #include using namespace std; int main() { int x=10; int& y =x; y=y+1; cout<<"x="<<x<<endl; cout<<"y="<<y<<endl; }

13 #include using namespace std void swap(int x, int y); int main() { int x = 4; int y = 2; cout << "Before swap, x is " << x << ", y is " << y << endl; swap(x,y); cout << "After swap, x is " << x << ", y is " << y << endl; } void swap(int first, int second) { int temp; temp = second; second = first; first = temp; } A formal parameter receives a copy of the content of corresponding actual parameter. which is called Pass by value & & & & Any modifications to the local copy do not change the original variable in the calling program. An alias of the argument is passed to the called function. which is called Pass by reference When references are passed into a function, any changes made to the references will be seen in the calling function. Parameters Passing

14 Parameters Passing Pass by value (A formal parameter is a value parameter): The value of the corresponding actual parameter is copied into it The value parameter has its own copy of the data Any modifications to the local copy do not change the original variable in the calling program Pass by reference (A formal parameter is a reference parameter): An alias of the argument is passed to the called function. no copies of the actual parameter are made. When references are passed into a function, any changes made to the references will be seen in the calling function.

15 Reference Variables as Parameters  Reference parameters can:  Pass one or more values from a function  Change the value of the actual parameter  Reference parameters are useful in three situations:  Returning more than one value  Changing the actual parameter  When passing the address would save memory space and time

16 End of lecture 17 Thank you!