Functions:Passing Parameters by Value Programming.

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.
Programming Functions: Passing Parameters by Reference.
Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
If Statements & Relational Operators Programming.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Computer Science 1620 Loops.
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.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Programming Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
Local, Global Variables, and Scope. COMP104 Slide 2 Functions are ‘global’, variables are ‘local’ int main() { int x,y,z; … } int one(int x, …) { double.
Functions. COMP104 Lecture 13 / Slide 2 Function Prototype * The function prototype declares the interface, or input and output parameters of the function,
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
Chapter 6: Functions.
Programming Functions: Passing Parameters by Reference.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Functions—Part I. Slide 2 Where are we now? Simple types of variables 3 program structures cin(>>)/cout(
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.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
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.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
User-Defined Functions (cont’d) - Reference Parameters.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
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.
CS 240 Computer Programming 1
Fundamentals of structural programming
Chapter 6: Modular Programming
Functions Chapter 5 CS12 - Computer Programming 1 Chapter 5.
Chapter 5 Function Basics
A Lecture for the c++ Course
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.
Functions.
Functions.
CS150 Introduction to Computer Science 1
Pointers & Functions.
Lab 1 Introduction to C++.
Pass by Reference.
Local, Global Variables, and Scope
The Function Prototype
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers & Functions.
CS1201: Programming Language 2
Introduction to Functions
Presentation transcript:

Functions:Passing Parameters by Value Programming

COMP102 Prog Fundamentals I: Pass by Value /Slide 2 Return Value l A function normally returns a single result, if it is not a void function. l At least one of the statements in the function body should have the form: return ; The value passed back by the return statement should have the same type as the function.

COMP102 Prog Fundamentals I: Pass by Value /Slide 3 Pass by Value l The arguments of a function retain their original values after the function’s execution.

COMP102 Prog Fundamentals I: Pass by Value /Slide 4 Pass by Value: Example 1 l For example, consider the following code: int sum(int num_1, int num_2){ num_1 = num_1 + num_2; return num_1; } int main(){ int var_x, var_y, var_z; var_x = 3; var_y = 5; var_z = sum(var_x, var_y); cout << var_z << endl; return 0; } What are the values of var_x, var_y, and var_z at the end of the main() program?

COMP102 Prog Fundamentals I: Pass by Value /Slide 5 Pass by Value: Example 1 l The answer: 3, 5, and 8. Even though the value of parameter num_1 is changed, the corresponding value in argument var_x does not change. l The value of the argument is copied to the parameter, but changes to the value of the parameter do not affect the argument. l In fact, all information in local variables declared within the function will be lost when the function terminates.

COMP102 Prog Fundamentals I: Pass by Value /Slide 6 Pass by Value: Example 2 An example to show how the function does not affect a variable which is used as a parameter: // Test the effect of a function // on its parameter #include using namespace std; void Increment(int Number) { Number = Number + 1; cout << "The parameter Number is: " << Number << endl; }

COMP102 Prog Fundamentals I: Pass by Value /Slide 7 Pass by Value: Example 2 int main() { int I = 10; //argument is a variable Increment(I ); cout << "The variable I is: " <<I << endl; //argument is a constant Increment(35); cout << "The variable I is: " <<I<< endl; //argument is an expression Increment(I +26); cout << "The variable I is: " <<I << endl; return 0; }

COMP102 Prog Fundamentals I: Pass by Value /Slide 8 Pass by Value: Example 2

COMP102 Prog Fundamentals I: Pass by Value /Slide 9 Pass by Value: Example 3 // Print the sum and average of two numbers // Input: two numbers x & y // Output: sum - the sum of x & y // average - the average of x & y #include using namespace std; void PrintSumAve ( double, double ); int main ( ) { double x, y; cout << "Enter two numbers: "; cin >> x >> y; PrintSumAve ( x, y ); return 0; }

COMP102 Prog Fundamentals I: Pass by Value /Slide 10 Pass by Value: Example 3 void PrintSumAve (double no1, double no2) { double sum, average; sum = no1 + no2; average = sum / 2; cout << "The sum is " << sum << endl; cout << "The average is " << average << endl; }

COMP102 Prog Fundamentals I: Pass by Value /Slide 11 Pass by Value: Example 3 Data areas after call to PrintSumAve() :

COMP102 Prog Fundamentals I: Pass by Value /Slide 12 Pass by Value: Example 4 //Compute new balance at a certain interest rate //Inputs: A positive balance and // a positive interest rate //Output: The new balance after interest was posted #include using namespace std; double new_balance(double, double); /* Returns the balance in a bank account after adding interest. For example, if rate is 5.0, then the interest rate is 5% and so new_balance(100, 5.0) returns */

COMP102 Prog Fundamentals I: Pass by Value /Slide 13 Pass by Value: Example 4 int main(){ double interest, balance; cout << "Enter balance (positive number): "; cin >> balance; if (balance <= 0.0) cout <<"Balance not positive; stopping" << endl; else { cout <<"Enter interest rate (positive number): "; cin >> interest; if (interest <= 0.0) cout <<"Interest not positive; stopping"<< endl; else cout <<"New balance = $" << new_balance(balance, interest)<< endl; } return 0; }

COMP102 Prog Fundamentals I: Pass by Value /Slide 14 Pass by Value: Example 4 // New balance is computed as balance + // balance * rate/100 double new_balance(double balance, double rate) { double interest_fraction, interest; interest_fraction = rate / 100; interest = interest_fraction * balance; return (balance + interest); } /* New balance is computed as balance * (1 + rate/100) double new_balance(double balance, double rate) { double interest_fraction, updated_balance; interest_fraction = rate / 100; updated_balance = balance * (1 + interest_fraction); return updated_balance; } */

COMP102 Prog Fundamentals I: Pass by Value /Slide 15 Pass by Value: Example 5 // Input: inches // Output: feet and inches #include using namespace std; // Function prototypes int feet(int); int remain_inches(int);

COMP102 Prog Fundamentals I: Pass by Value /Slide 16 Pass by Value: Example 5 int main() { int inches; // Number of inches cout << "Enter number of inches to convert:"; cin >> inches; cout << "Result is " << feet(inches) << " feet " << remain_inches(inches) << " inches" << endl; return 0; } int feet(int input_inches) { return input_inches / 12; } int remain_inches(int input_inches) { return input_inches % 12; }

COMP102 Prog Fundamentals I: Pass by Value /Slide 17 Pass by Value: Example 6 // File main.cpp // Input inches // Output feet and inches #include #include "i2f.h" using namespace std; int main() { int inches; // Number of inches cout << "Enter number of inches to convert:"; cin >> inches; cout << "Result is "<< feet(inches)<<" feet " << remain_inches(inches) << " inches" << endl; return 0; }