Functions Parameters & Variable Scope Chapter 6. 2 Overview Using Function Arguments and Parameters...

20
Functions Parameters & Variable Scope Chapter 6

Transcript of Functions Parameters & Variable Scope Chapter 6. 2 Overview Using Function Arguments and Parameters...

Page 1: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

Functions Parameters &Variable Scope

Chapter 6

Page 2: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

2

Overview

Using Function Arguments and Parameters Differences between Value Parameters and Reference

Parameters Using Local Variables in a Function Variable Scope and Duration

Page 3: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

3

Functions

Every C++ program must have a main function execution starts by looking for a “main”

All other functions are called directly from main, or indirectly through a chain of function called from main

Function Calls One function calls another by using the name of the called

function next to ( ) enclosing an argument list.

ex. strlen(FirstName) A function call temporarily transfers control from the

calling function to the called function.

Page 4: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

4

We’ve been using value parameters

Simple types (like int, char, float, double) are value parameters by default you can specify reference parameters with &

To create a reference parameter place an & after the type in both the function heading and prototype

void Cube(int &x);

Page 5: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

5

When to Use Reference Parameters

If you want your function to Assign a value to a variable from where it was called Change the value of a variable permanently

Using a reference parameter is like giving someone the key to your home the key can be used by the other person to change the

contents of your home!

Page 6: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

6

Anatomy of a function call

0

0

-234535

1243

0

0

25

-234534

0

0

0

101

4000

4004

4008

4012

4016

4020

4024

4028

4032

4036

4040

4044

age

myByRef(int& a){ a = 10; }myByVal(int a){ a = 100; }

int main(){ int age = 25; myByVal(age); myByRef(age);

Page 7: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

7

Anatomy of a function call

0

0

-234535

1243

0

0

25

-234534

0

0

0

101

4000

4004

4008

4012

4016

4020

4024

4028

4032

4036

4040

4044

age

myByRef(int& a){ a = 10; }myByVal(int a){ a = 100; }

int main(){ int age = 25; myByVal(age); myByRef(age);

Page 8: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

8

Anatomy of a function call

0

0

-234535

1243

0

0

25

25

0

0

0

101

4000

4004

4008

4012

4016

4020

4024

4028

4032

4036

4040

4044

age

myByRef(int& a){ a = 10; }myByVal(int a){ a = 100; }

int main(){ int age = 25; myByVal(age); myByRef(age);

a

Page 9: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

9

Anatomy of a function call

0

0

-234535

1243

0

0

25

100

0

0

0

101

4000

4004

4008

4012

4016

4020

4024

4028

4032

4036

4040

4044

age

myByRef(int& a){ a = 10; }myByVal(int a){ a = 100; }

int main(){ int age = 25; myByVal(age); myByRef(age);

a

Page 10: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

10

Anatomy of a function call

0

0

-234535

1243

0

0

25

100

0

0

0

101

4000

4004

4008

4012

4016

4020

4024

4028

4032

4036

4040

4044

age

myByRef(int& a){ a = 10; }myByVal(int a){ a = 100; }

int main(){ int age = 25; myByVal(age); myByRef(age);

Page 11: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

11

Anatomy of a function call

0

0

-234535

1243

0

0

25

4024

0

0

0

101

4000

4004

4008

4012

4016

4020

4024

4028

4032

4036

4040

4044

age

myByRef(int& a){ a = 10; }myByVal(int a){ a = 100; }

int main(){ int age = 25; myByVal(age); myByRef(age);

&a

Page 12: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

12

Anatomy of a function call

0

0

-234535

1243

0

0

10

4024

0

0

0

101

4000

4004

4008

4012

4016

4020

4024

4028

4032

4036

4040

4044

age

myByRef(int& a){ a = 10; }myByVal(int a){ a = 100; }

int main(){ int age = 25; myByVal(age); myByRef(age);

&a

Page 13: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

13

Example: Pass-by-Reference

We want to find 2 real roots for a quadratic equation with coefficients a,b,c. Write a prototype for a bool function named GetRoots( ) with 5 parameters. Return value is true if real roots exist, false otherwise

The first 3 (value) parameters are type float. The last 2 are reference parameters of type float.

Need to pass 2 values back…

Page 14: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

14

bool GetRoots(float, float, float, float&, float&);

This function uses 3 incoming values It calculates 2 outgoing values, returning the 2 real roots of

the quadratic equation with coefficients a, b, c

GetRoots prototype

Page 15: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

15

Function Definition

bool GetRoots( float a, float b, float c, float& root1, float& root2){ float temp; // local variable temp = b * b - 4.0 * a * c; if (temp < 0) return false; else { root1 = (-b + sqrt(temp))/(2.0 * a); root2 = (-b - sqrt(temp))/(2.0 * a); return true; }}

Page 16: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

16

#include <iostream>

#include <cmath>

bool GetRoots(float, float, float, float&, float&);

using namespace std;

int main ( )

{ float c1, c2, c3, first, second;

int count = 0;

...

while ( count < 5 ){

cin >> c1 >> c2 >> c3;

if (GetRoots(c1, c2, c3, first, second))

cout << first << second << endl;

else

cout << "No real solution";

count++ ;

}

...

}

Page 17: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

17

Data Flow Passing Mechanism

Direction Passing mechanism

Input parameter pass-by-value

Output parameter pass-by-reference

Both pass-by-reference

Page 18: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

18

Functions with reference parameters

Write a prototype and definition for a void function called GetGrade( ) with one reference parameter of type char The function repeatedly prompts the user to enter a character at

the keyboard until one of these has been entered: A, B, C, D, F

Page 19: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

19

void GetGrade(char& letter); // prototype

void GetGrade(char& letter){ cout << “Enter employee rating.” << endl; cout << “Use A, B, C, D, F : ” ; cin >> letter; while ((letter != 'B') && (letter != 'C')

&& (letter != 'A') && (letter != 'D') && (letter != 'F')) { cout << "Invalid. Enter again: "; cin >> letter; }}

Page 20: Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

20

Pass-by-Reference use only if the design of the called function requires that

it be able to modify the value of the parameterPass-by-Constant-Reference

use if the called function has no need to modify the value of the parameter, but the parameter is very large

guarantees that the called function cannot modify the variable passed in

Pass-by-Value use when none of the reasons given above apply

Using a Parameter Passing Mechanism