double finalScore(double midterm, double exam) {
|
x = 50, y = 70. finalScore(x, y) in main
x is copied to function finalScore's
local variable midterm. y is copied to function finalScore's
local variable exam. finalScore's function body will be
executed.
and the result will be returned. midterm,exam are local variables of finalScore.x,y are copied to midterm,exam.
#include<iostream> |
x is not increased!
Before calling the increase function, x is 1. |
#include <iostream> |
x is unchanged!
Please input a positive number : -19 |
#include<iostream> |
x is really increased by 1.
Before calling the increase function, x is 1. |
#include <iostream> |
x is changed!
Please input a positive number : -19 |
type& variableName
The ampersand sign, & comes right after the type.or type amp;variableName
The ampersand sign, & comes right before the variableName.They are used in parameter list. Example int increase(int& num); void askForInput(double &income, char &gender); |
| call by value |
call by reference |
|---|---|
| no & in the formal parameters |
use & in the formal
parameters |
| value of the variable is passed
to the function |
the address of the memory
location of the variable is passed to the function |
| changes made to the local
variable |
changes made to the variable in
the memory location. |
| no change can be made to the
original variable |
changes can be made the the
original variable because the original variable is the variable
in the memory location. |
#include <iostream> |
Please enter your income: 100000
Charles' taxable income: 100000
Charles' tax: 25000
Please enter your income: 45000
Please enter your spouse's income: 62333
Amy's taxable income: 107333
Amy's tax: 26833.3
|
max, one
that computes the largest of 2 number, another that computes the
largest of 3 numbers,
and yet another that computes the largest of four numbers.
#include<iostream> |
The average of 2.0, 2.5, 3.0 is 2.5 |
ave(2.0, 2.5, 3.0) matches double ave(double
n1, double n2, double n3). ave(4.5, 5.5) matches double ave(double n1,
double n2) .int tax(int income, bool married)
void tax(int income, char gender)
const
or solely on
call-by-values versus call-by-function (although some compilers, like
VC++, allow you to do that).
#include<iostream> |
short to intfloat to doublebool to intchar to intint to doubledouble to int
#include<iostream> |
f(int n, double m) |
f(a,c)f(c,c)f(a,b)