Download presentation
Presentation is loading. Please wait.
1
CS1201: Programming Language 2
By:Nouf Aljaffan Edited by : Nouf Almunyif Function III
2
methods of passing There are 2 primary methods of passing arguments to functions: pass by value, pass by reference,
3
Absolute value #include <iostream> using namespace std;
int absolute (int);// function prototype for absolute() int main(){ int num, answer; cout << "Enter an integer (0 to stop): "; cin >> num; while (num!=0){ answer = absolute(num); cout << "The absolute value of " << num << " is: " << answer << endl; cin >> num; } return 0; } // Define a function to take absolute value of an integer int absolute(int x){ if (x >= 0) return x; else return -x; }
4
methods of passing There are 2 primary methods of passing arguments to functions: pass by value, pass by reference,
5
Passing arguments by reference
When passing arguments by value, the only way to return a value back to the caller is via the function’s return value. While this is suitable in many cases, there are a few cases where better options are available.
6
Passing arguments by reference
In pass by reference, we declare the function parameters as references rather than normal variables: 1 2 3 4 void AddOne(int &y) // y is a reference variable { y = y + 1; } When the function is called, y will become a reference to the argument. The reference to a variable is treated exactly the same as the variable itself. any changes made to the reference are passed through to the argument!
7
Passing arguments by reference
Sometimes we need a function to return multiple values. However, functions can only have one return value. One way to return multiple values is using reference parameters
8
Cont. Example void foo(int &y) // y is now a reference { cout << "y = " << y << endl; y = 6; } // y is destroyed here int main() int x = 5; cout << "x = " << x << endl; foo(x); return 0; } This program is the same as the one we used for the pass by value example, except foo’s parameter is now a reference instead of a normal variable. When we call foo(x), y becomes a reference to x. Note that the value of x was changed by the function!
9
Passing arguments by reference
Example 2 void AddOne(int &y) { y++; } int main() int x = 1; cout << "x = " << x << endl; AddOne(x); return 0;
10
Passing arguments by reference
Example 3 #include <iostream> Using namespace std ; Void duplicate (int& a, int& b, int & c); Void main () { Int x=1,y=3,z=7; Duplicate (x,y,z); Cout << “x=“<<x<<“, y=“<<y<<“,z=“<<z; } Void duplicate (int& a, int& b, int & c) { a*=2; b*=2; c*=2;
11
Passing arguments by reference
#include <iostream> Using namespace std ; void swap(float &x, float &y); int main() { float a, b; cout << "Enter 2 numbers: " << endl; cin >> a >> b; if(a>b) swap(a,b); cout << "Sorted numbers: "; cout << a << " " << b << endl; return 0; } void swap(float &x, float &y) { float temp; temp = x; x = y; y = temp; } Example 4
12
Passing arguments by reference
1 #include <iostream> #include <math.h> // for sin() and cos() Using namespace std; void GetSinCos(double dX, double &dSin, double &dCos) { dSin = sin(dX); dCos = cos(dX); } int main() double dSin = 0.0; double dCos = 0.0; GetSinCos(30.0, dSin, dCos); cout << "The sin is " << dSin << endl; cout << "The cos is " << dCos << endl; return 0;
13
Advantages of passing by reference
It allows us to have the function change the value of the argument, which is sometimes useful. Because a copy of the argument is not made, it is fast, even when used with large structus or classes. We can return multiple values from a function.
14
Function Overloading Function overloading
Functions with same name and different parameters or return data type Should perform similar tasks I.e., function to square ints and function to square floats int square( int x) {return x * x;} float square(float x) { return x * x; } A call-time c++ complier selects the proper function by examining the number, type and order of the parameters
15
Function overloading Example
cout << "Enter Fahrenheit temperature: "; switch (level) { case 1 : cin >> inttemp; FtoC(inttemp); break; case 2 : cin >> floattemp; FtoC(floattemp); case 3 : cin >> doubletemp; FtoC(doubletemp); default : cout << "Invalid selection\n"; } return 0; #include <iostream> using namespace std; void FtoC(int temp); void FtoC(float temp); void FtoC(double temp); int main() { int inttemp, level; float floattemp; double doubletemp; cout << "CONVERTING FAHRENHEIT TO CELSIUS\n"; cout << "Select required level of precision\n"; cout << "Integer (1) - Float (2) - Double (3)\n"; cin >> level;
16
Function overloading Cont.
void FtoC( int itemp) { int temp = (itemp - 32) * 5 / 9; cout << "Integer precision: "; cout << itemp << "F is " << temp << "C\n"; } void FtoC( float ftemp) float temp = (ftemp - 32) * 5.0 / 9.0; cout << "Float precision: "; cout << ftemp << "F is " << temp << "C\n";; void FtoC( double dtemp) { double temp = (dtemp - 32) * 5.0 / 9.0; cout << "Double precision : "; cout << dtemp << "F is " << temp << "C\n";; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.