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.

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

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
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.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.
Function Part II: Some ‘advanced’ concepts on functions.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
C Programming Lecture 8-2 : Function (advanced). Recursive Function (recursion) A function that calls itself (in its definition) Classic example : factorial.
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.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message **** ** Welcome.
Learners Support Publications Functions in C++
Chapters 1-5 Review C++ Class. Chapter 1 – the big picture Objects Class Inheritance Reusability Polymorphism and Overloading.
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.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
Week 2. Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
Functions Sujana Jyothi C++ Workshop Day 2. Functions 3 Parameter transmission modes pass by value (default) pass by reference (&) pass by const reference.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
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)
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
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.
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.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
 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.
FUNCTIONS In C++.
Chapter 5 Function Basics
Functions, locals, parameters, and separate compilation
Functions and an Introduction to Recursion
Chapter 5 Functions.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Subject Name: PROGRAMMING IN C++ Subject Code: 10EC665
Pointers and Pointer-Based Strings
CSC113: Computer Programming (Theory = 03, Lab = 01)
Lecture 4-7 Classes and Objects
Extra.
Chapter 5 Function Basics
Pointers & Functions.
More ‘concepts’ on Function
Pass by Reference.
CS2011 Introduction to Programming I Methods (II)
Simulating Reference Parameters in C
The Function Prototype
Functions and an Introduction to Recursion
Introduction To Programming
Pointers and Pointer-Based Strings
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers & Functions.
CS1201: Programming Language 2
Intro to Programming Week # 8 Functions II Lecture # 13
More ‘concepts’ on Function
Functions Chapter No. 5.
Presentation transcript:

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 of the function. In this case, changes made to the parameter inside the function have no effect on the argument. By default, C++ uses call by value to pass arguments. In general, this means that code within a function cannot alter the arguments used to call the function. Consider the function swap() definition as follows.

// function definition to swap the values. void swap(int x, int y) { int temp; temp = x; /* save the value of x */ x = y; /* put y into x */ y = temp; /* put x into y */ return; }

#include using namespace std; // function declaration void swap(int x, int y); int main () { // local variable declaration: int a = 100; int b = 200; cout << "Before swap, value of a :" << a << endl; cout << "Before swap, value of b :" << b << endl; // calling a function to swap the values. swap(a, b); cout << "After swap, value of a :" << a << endl; cout << "After swap, value of b :" << b << endl; return 0; }

– Before swap, value of a :100 – Before swap, value of b :200 – After swap, value of a :100 – After swap, value of b :200

C++ function call by pointer The call by pointer method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument. To pass the value by pointer, argument pointers are passed to the functions just like any other value. So accordingly you need to declare the function parameters as pointer types as in the following function swap(), which exchanges the values of the two integer variables pointed to by its arguments.

/ function definition to swap the values. void swap(int *x, int *y) { int temp; temp = *x; /* save the value at address x */ *x = *y; /* put y into x */ *y = temp; /* put x into y */ return; }

#include using namespace std; // function declaration void swap(int *x, int *y); int main () { // local variable declaration: int a = 100; int b = 200; cout << "Before swap, value of a :" << a << endl; cout << "Before swap, value of b :" << b << endl; /* calling a function to swap the values. * &a indicates pointer to a ie. address of variable a and * &b indicates pointer to b ie. address of variable b. */ swap(&a, &b); cout << "After swap, value of a :" << a << endl; cout << "After swap, value of b :" << b << endl; return 0; }

When the above code is put together in a file, compiled and executed, it produces the following result: – Before swap, value of a :100 – Before swap, value of b :200 – After swap, value of a :200 – After swap, value of b :100

C++ function call by reference #include using namespace std; // function declaration void swap(int& x, int& y); int main () { // local variable declaration: int a = 100; int b = 200; cout << "Before swap, value of a :" << a << endl; cout << "Before swap, value of b :" << b << endl; /* calling a function to swap the values.*/ swap(a, b); cout << "After swap, value of a :" << a << endl; cout << "After swap, value of b :" << b << endl ; return 0; } // function definition to swap the values. void swap(int& x, int& y) { int temp; temp = x; /* save the value at address x */ x = y; /* put y into x */ y = temp; /* put x into y */ return; }

When the above code is compiled and executed, it produces the following result: – Before swap, value of a :100 – Before swap, value of b :200 – After swap, value of a :200 – After swap, value of b :100

C++ Recursion call a function from a same function. This function is known as recursive function and this programming technique is known as recursion.

#include using namespace std; int factorial(int); int main() { int n; cout<<"Enter a number to find factorial: "; cin>>n; cout<<"Factorial of "<<n<<" = "<<factorial(n); return 0; } int factorial(int n) { if (n>1) { return n*factorial(n-1); } else { return 1; } } Output Enter a number to find factorial: 4 Factorial of 4 = 24

C++ Function Overloading In C++ programming, two functions can have same identifier(name) if either number of arguments or type of arguments passed to functions are different. /* Example of function overloading */ int – test() { } – int test(int a){ } – int test(double a){ } – int test(int a, double b){ }

#include using namespace std; 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<<endl; } void test(float var){ cout<<"Float number: "<<var<<endl; } void test(int var1, float var2) { cout<<"Integer number: "<<var1; cout<<" And float number:"<<var2; } Output – Integer number: 5 – Float number: 5.5 – Integer number: 5 And float number: 5.5

#include using namespace std; int absolute(int); float absolute(float); int main() { int a = -5; float b = 5.5; cout<<"Absolute value of "<<a<<" = "<<absolute(a)<<endl; cout<<"Absolute value of "<<b<<" = "<<absolute(b); return 0; } int absolute(int var) { if (var < 0) var = -var; return var; } float absolute(float var) { if (var < 0.0) var = -var; return var; }

Absolute value of -5 = 5 Absolute value of 5.5 = 5.5

C++ Inline Functions C++ inline function is powerful concept that is commonly used with classes. If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time. To inline a function, place the keyword inline before the function name and define the function before any calls are made to the function. The compiler can ignore the inline qualifier in case defined function is more than a line.

#include using namespace std; inline int Max(int x, int y) { return (x > y)? x : y; } // Main function for the program int main( ) { cout << "Max (20,10): " << Max(20,10) << endl; cout << "Max (0,200): " << Max(0,200) << endl; cout << "Max (100,1010): " << Max(100,1010) << endl; return 0; } When the above code is compiled and executed, it produces the following result: Max (20,10): 20 Max (0,200): 200 Max (100,1010): 1010

Defining Inline function within a class #include Using namespace std; Class myclass{ Int a,b; Public: Void init(ini I, int j){a = I; b=j;} Void show(){cout<<a<<“ ”<<b<<“\n”} }; Int main() { Myclass x; x.init(10,20); x.show(); Return 0; }