Intro to Programming Week # 8 Functions II Lecture # 13

Slides:



Advertisements
Similar presentations
Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
Advertisements

PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
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.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Function Part II: Some ‘advanced’ concepts on functions.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
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.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function.
Functions Pass by reference, or Call by reference Passing addresses Use of & part 3.
1 CS161 Introduction to Computer Science Topic #10.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Functions Sujana Jyothi C++ Workshop Day 2. Functions 3 Parameter transmission modes pass by value (default) pass by reference (&) pass by const reference.
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.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1 FUNCTIONS - II Chapter 9 and Today we will continue with functions covering…. Passing by Reference  Scope Sorting variables Function Structure.
Templates Where the TYPE is generic. Templates for functions Used when the you want to perform the same operation on different data types. The definition.
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.
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value –A copy of the arguments’svalue.
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
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.
Lecture 10 Oct 7, 02. Pass by value and reference ► Calling a function and passing the value of a variable as an argument is called pass by value. The.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Introduction to Programming
-Neelima Singh PGT(CS) KV Sec-3 Rohini
CSC1201: Programming Language 2
Chapter 5 Function Basics
Functions and an Introduction to Recursion
A Lecture for the c++ Course
Pointers and Pointer-Based Strings
Intro to Programming Week # 7 & 8 Functions Lecture # 11& 12
Intro to Programming Week # 3 If-else Statement Lecture # 6
Reference parameters (6.2.3)
CS1201: Programming Language 2
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)
Pointers & Functions.
Lecture 12 Oct 16, 02.
More ‘concepts’ on Function
Lec 14 Oct 23, 02.
Pass by Reference.
CS2011 Introduction to Programming I Methods (II)
Dynamic Memory A whole heap of fun….
Dr. Khizar Hayat Associate Prof. of Computer Science
Simulating Reference Parameters in C
Function “Inputs and Outputs”
Functions and an Introduction to Recursion
CS1201: Programming Language 2
Pointers and Pointer-Based Strings
CSC1201: Programming Language 2
The Stack.
Dr. Khizar Hayat Associate Prof. of Computer Science
CS1201: Programming Language 2
Structure (i.e. struct) An structure creates a user defined data type
CS-161 Computer Programming Lecture 15 & 16: Arrays II
TOPIC: FUNCTION OVERLOADING
Unit-1 Introduction to Java
Introduction to Algorithms and Programming COMP151
More ‘concepts’ on Function
Functions Chapter No. 5.
Presentation transcript:

Intro to Programming Week # 8 Functions II Lecture # 13 By: Saqib Rasheed

Parameters by value When arguments are passed by value, the called function creates a new variable of the same type and copies the argument’s value into it. Function cannot access the original variable in the calling program, only the copy it created. Passing arguments by value is useful when the function does not need to modify the original variable in the calling program. In fact, it offers insurance that the function cannot harm the original variable. Saqib Rasheed

Parameters by reference Passing arguments by reference uses a different mechanism. Instead of a value being passed to the function, a reference to the original variable, in the calling program, is passed. It’s actually pass the memory address of the variable that is passed An important advantage of passing by reference is that the function can access the actual variables in the calling program. Among other benefits, this provides a mechanism for passing more than one value from the function back to the calling program.

Example void foo(int &y) { cout << "y = " << y ; y = 6; cout << "y = " << y; } // y is destroyed here void foo(int &y) ; int main() {     int x = 5;     cout << "x = " << x ;     foo(x);     return 0; } Saqib Rasheed

Passing By Value Versus Passing By Reference int &x; The parameter x is a local reference. It is a copy for the argument. It can change the argument. The argument passed by reference must be a variable. The argument is read-write. Passing By Value int x; The parameter x is a local variable. It is a duplicate of the argument. It cannot change the argument. The argument passed by value may be a constant, a variable, or an expression. The argument is read-only. Saqib Rasheed

Passing values by reference e.g void square(double &); void main() { double x; cout <<"\n main(), before calling "; cout<<"\nEnter the number to find square="; cin>>x; square(x); cout <<"\n main(), after calling square(), x = " << x<<endl; } void square(double &x) x=x*x; Saqib Rasheed

Example Develop a program that swap the two values using function by reference i.e a = 22.2 b = 44.4 After swap a = 44.4 b = 22.2 Saqib Rasheed

Example(code) void swap(float ,float ); void main() { float a = 22.2,b = 44.4; cout << "a = " << a << ", b = " << b << endl; swap(a,b); } void swap(float x, float y) float temp = x; x = y; y = temp; Saqib Rasheed

Example(code) void swap(float&,float&); void main() { float a = 22.2,b = 44.4; cout << "a = " << a << ", b = " << b << endl; swap(a,b); } void swap(float& x, float& y) float temp = x; x = y; y = temp; Saqib Rasheed

Function Overloading int test() { } int test(int a){ } int test(double a){ } int test(int a, double b){ } All 4 functions mentioned above are overloaded function. It should be noticed that, the return type of all 4 functions is same,i.e, int. Overloaded function may or may not have different return type but it should have different argument Saqib Rasheed

Example void test(int); void test(float); void test(int, float); int main() { int a = 5; float b = 5.5; test(a); test(b); test(a, b); return 0; } void test(int var) { cout<<"Integer number: "<<var; } void test(float var){ cout<<"Float number: "<<varl; void test(int var1, float var2) { cout<<"Integer number: "<<var1; cout<<" And float number:"<<var2; Saqib Rasheed

Overloading the max() Function #include<iostream.h> int max(int,int); int max(int,int,int) ; int main() { int x,y,z; cout<<"Enter x ="; cin>>x; cout<<"Enter y ="; cin>>y; cout<<"Enter z ="; cin>>z; } Saqib Rasheed

Overloading the max() Function cout << "Largest Among Two = "<<max(x,y) << "\nLargest Among Three =" << max(x,y,z); cout<<endl; int max(int x,int y) { // returns the maximum of the two given integers: return (x > y ? x : y); } int max(int x,int y, int z) { // returns the maximum of the three given integers: int m = (x > y ? x : y); // m = max(x,y) return (z > m ? z : m); Saqib Rasheed