CS1201: Programming Language 2

Slides:



Advertisements
Similar presentations
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Advertisements

1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
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.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Function Overloading Can enables several functions Of same name Of different sets of parameters (at least as far as their types are concerned) Used to.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
Functions:Passing Parameters by Value Programming.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion.
Chapter 7 Functions.
Programming Functions: Passing Parameters by Reference.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
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.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
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)
EC-111 Algorithms & Computing Lecture #6 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ 16 September 2008.
Functions BICSE-6A Mr. Naeem Khalid Lecturer, Dept. of Computing.
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.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Chapter 15 - C++ As A "Better C"
Functions.
CSC1201: Programming Language 2
Chapter 5 Function Basics
Functions and an Introduction to Recursion
CO1401 Programming Design and Implementation
School of EECS, Peking University
Chapter 5 Functions.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Pointer Data Type and Pointer Variables
User-defined Functions
Zhen Jiang West Chester University
Extra.
CS1201: Programming Language 2
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
User-defined Functions
CS150 Introduction to Computer Science 1
Pointers & Functions.
CS150 Introduction to Computer Science 1
CS1201: Programming Language 2
Functions Pass By Value Pass by Reference
Pass by Reference.
CS1201: Programming Language 2
CS1201: Programming Language 2
Functions and an Introduction to Recursion
CS1201: Programming Language 2
CSC1201: Programming Language 2
CS150 Introduction to Computer Science 1
Fundamental Programming
Multiple Files Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers & Functions.
Pointer Data Type and Pointer Variables
CMSC 202 Lesson 6 Functions II.
Pointer Data Type and Pointer Variables
CS1201: Programming Language 2
TOPIC: FUNCTION OVERLOADING
Intro to Programming Week # 8 Functions II Lecture # 13
Introduction to Functions
Presentation transcript:

CS1201: Programming Language 2 By:Nouf Aljaffan Edited by : Nouf Almunyif Function III

methods of passing There are 2 primary methods of passing arguments to functions: pass by value, pass by reference,

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; }

methods of passing There are 2 primary methods of passing arguments to functions: pass by value, pass by reference,

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.

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!

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

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!

Passing arguments by reference Example 2 void AddOne(int &y) {     y++; }   int main()     int x = 1;     cout << "x = " << x << endl;     AddOne(x);     return 0;

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;

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

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;

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.

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

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;

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";; }