##Exercise 6.1
Parameters: Local variable declared inside the function parameter list. they are initialized by the arguments provided in the each function call.
Arguments: Values supplied in a function call that are used to initialize the function's parameters.
##Exercise 6.2
(a) string f() {
string s;
// ...
return s;
}
(b) void f2(int i) { /* ... */ }
(c) int calc(int v1, int v2) { /* ... */ }
(d) double square (double x) { return x * x; }
##Exercise 6.3
#include <iostream>
int fact(int val)
{
if (val == 0 || val == 1) return 1;
else return val * fact(val-1);
}
int main()
{
int j = fact(5); // j equals 120, i.e., the result of fact(5)
std::cout << "5! is " << j << std::endl;
return 0;
}
##Exercise 6.4
int func()
{
int n, ret = 1;
std::cout << "input a number: ";
std::cin >> n;
while (n > 1) ret *= n--;
return ret;
}
int main()
{
std::cout << func() << std::endl;
return 0;
}
##Exercise 6.5
template <typename T>
T abs(T i)
{
return i >= 0 ? i : -i;
}
##Exercise 6.6
local variable: Variables defined inside a block;
parameter: Local variables declared inside the function parameter list
local static variable: Local variable whose value persists across calls to the function. The variable that are created and initialized before contorl reaches their use and are destoryed when the program ends.
// example
size_t count_add(int n) // n is a parameter.
{
static size_t ctr = 0; // ctr is a static variable.
ctr += n;
return ctr;
}
int main()
{
for (size_t i = 0; i != 10; ++i) // i is a local variable.
cout << count_add(i) << endl;
return 0;
}
size_t generate()
{
static size_t ctr = 0;
return ctr++;
}
Exercise 6.9 fact.cc | factMain.cc
void f(T)
pass the argument by value. nothing the function does to the parameter can affect the argument.
void f(T&)
pass a reference, will be bound to whatever T object we pass.
a parameter should be a reference type:
void reset(int &i)
{
i = 0;
}
a parameter should not be a reference:
void print(std::vector<int>::iterator begin, std::vector<int>::iterator end)
{
for (std::vector<int>::iterator iter = begin; iter != end; ++iter)
std::cout << *iter << std::endl;
}
why is
s
a reference to const butoccurs
is a plain reference?
cause the s
should not be changed by this function. but occurs
's result must be calculated by the function.
Why are these parameters references, but the char parameter
c
is not?
casue c
maybe a temp varable. such as find_char(s, 'a', occurs)
What would happen if we made
s
a plain reference? What if we madeoccurs
a reference to const?
s
could be changed in the function, and occurs
whould not be changed. so occurs = 0;
is an error.
bool is_empty(const string& s) { return s.empty(); }
Since this function doesn't change the argument,"const" shoud be added before string&s,otherwise this function is misleading and can't be used with const string or in a const function.
Not the same. For the first one "const" was used, since no change need to do for the argument. For the second function,"const" can't be used,because the content of the agument should be changed.
(a)
bool compare(matrix &m1, matrix &m2){ /.../ }
(b)
vector<int>::iterator change_val(int, vector<int>::iterator) { /.../ }
(a) illegal, only one parameter. (b) legal. (c) legal. (d) legal.
If we can use const
, just use it. If we make a parameter a plain reference when it could be a reference to const
,
the reference value maybe changed.
no problem.