C++ normally passes arguments by value.
For example,
double volume = cube(side);
Here side is a variable that, in the run, had the value 5.
The function header for cube() was this:
double cube(double x)
When this function is called, it creates a new type double variable called x and initializes it with the value 5.
A function can have more than one argument.
In the function call, you just separate the arguments with commas:
#include <iostream>
using namespace std;
void n_chars(char, int);
int main()/*ww w . ja v a 2s . c o m*/
{
int times;
char ch;
cout << "Enter a character: ";
cin >> ch;
while (ch != 'q') // q to quit
{
cout << "Enter an integer: ";
cin >> times;
n_chars(ch, times); // function with two arguments
cout << "\nEnter another character or press the q-key to quit: ";
cin >> ch;
}
cout << "The value of times is " << times << ".\n";
return 0;
}
void n_chars(char c, int n) // displays c n times
{
while (n-- > 0) // continue until n reaches 0
cout << c;
}
The code above generates the following result.
The following code illustrates using a pointer as if it were an array name.
The program initializes the array to some values and uses the sum_arr() function to calculate the sum.
#include <iostream>
using namespace std;
/* w w w. j a v a 2s. c o m*/
const int SIZE = 8;
int sum_arr(int arr[], int n); // prototype
int main()
{
int cookies[SIZE] = {1,2,4,8,16,32,64,128};
int sum = sum_arr(cookies, SIZE);
cout << "Total cookies eaten: " << sum << "\n";
return 0;
}
// return the sum of an integer array
int sum_arr(int arr[], int n){
int total = 0;
for (int i = 0; i < n; i++)
total = total + arr[i];
return total;
}
The code above generates the following result.
The following code demonstrates that cookies and arr have the same value.
It shows how the pointer makes the sum_arr function more versatile.
The program uses the std:: qualifier instead of the using directive to provide access to cout and endl.
#include <iostream>
const int ArSize = 8;
int sum_arr(int arr[], int n);
/*www .j a va 2s . co m*/
int main()
{
int cookies[ArSize] = {1,2,4,8,16,32,64,128};
std::cout << cookies << " = array address, ";
std::cout << sizeof cookies << " = sizeof cookies\n";
int sum = sum_arr(cookies, ArSize);
std::cout << "Total cookies eaten: " << sum << std::endl;
sum = sum_arr(cookies, 3); // a lie
std::cout << "First three eaters ate " << sum << " cookies.\n";
sum = sum_arr(cookies + 4, 4); // another lie
std::cout << "Last four eaters ate " << sum << " cookies.\n";
return 0;
}
int sum_arr(int arr[], int n){
int total = 0;
std::cout << arr << " = arr, ";
std::cout << sizeof arr << " = sizeof arr\n";
for (int i = 0; i < n; i++)
total = total + arr[i];
return total;
}
The code above generates the following result.
The following code uses two pointers to specify a range.
#include <iostream>
using namespace std;
const int SIZE = 8;
int sum_arr(const int * begin, const int * end);
int main()/* w ww . j a v a 2s. c om*/
{
int cookies[SIZE] = {1,2,4,8,16,32,64,128};
int sum = sum_arr(cookies, cookies + SIZE);
cout << "Total cookies eaten: " << sum << endl;
sum = sum_arr(cookies, cookies + 3); // first 3 elements
cout << "First three eaters ate " << sum << " cookies.\n";
sum = sum_arr(cookies + 4, cookies + 8); // last 4 elements
cout << "Last four eaters ate " << sum << " cookies.\n";
return 0;
}
int sum_arr(const int * begin, const int * end){
const int * pt;
int total = 0;
for (pt = begin; pt != end; pt++)
total = total + *pt;
return total;
}
The code above generates the following result.
The following code counts the number of times a given character appears in a string.
Because the program doesn't need to deal with negative values, it uses unsigned int as the type for counting.
#include <iostream>
using namespace std;
unsigned int c_in_str(const char * str, char ch);
int main()//from www . ja v a 2 s . c o m
{
char mmm[15] = "minimum"; // string in an array
char *wail = "ululate"; // wail points to string
unsigned int ms = c_in_str(mmm, 'm');
unsigned int us = c_in_str(wail, 'u');
cout << ms << " m characters in " << mmm << endl;
cout << us << " u characters in " << wail << endl;
return 0;
}
// counts the number of ch characters in the string str
unsigned int c_in_str(const char * str, char ch)
{
unsigned int count = 0;
while (*str) // quit when *str is '\0'
{
if (*str == ch)
count++;
str++; // move pointer to next char
}
return count;
}
The code above generates the following result.
The following code defines a function called buildstr() that returns a pointer.
This function takes two arguments: a character and a number.
Using new, the function creates a string whose length equals the number, and then it initializes each element to the character.
Then it returns a pointer to the new string.
#include <iostream>
using namespace std;
char * buildstr(char c, int n);
int main()//from ww w . j a v a2 s . com
{
int times;
char ch;
cout << "Enter a character: ";
cin >> ch;
cout << "Enter an integer: ";
cin >> times;
char *ps = buildstr(ch, times);
cout << ps << endl;
delete [] ps; // free memory
ps = buildstr('+', 20); // reuse pointer
cout << ps << "-DONE-" << ps << endl;
delete [] ps; // free memory
return 0;
}
// builds string of characters
char * buildstr(char c, int n){
char * pstr = new char[n + 1];
pstr[n] = '\0'; // terminate string
while (n-- > 0)
pstr[n] = c; // fill rest of string
return pstr;
}
The code above generates the following result.
#include <iostream>
using namespace std;
//from w w w.j ava 2s.c om
struct my_time
{
int hours;
int mins;
};
const int MINUTES = 60;
my_time sum(my_time t1, my_time t2);
void show_time(my_time t);
int main()
{
my_time day1 = {5, 45}; // 5 hrs, 45 min
my_time day2 = {4, 55}; // 4 hrs, 55 min
my_time trip = sum(day1, day2);
show_time(trip);
return 0;
}
my_time sum(my_time t1, my_time t2)
{
my_time total;
total.mins = (t1.mins + t2.mins) % MINUTES;
total.hours = t1.hours + t2.hours + (t1.mins + t2.mins) / MINUTES;
return total;
}
void show_time(my_time t)
{
cout << t.hours << " hours, " << t.mins << " minutes\n";
}
The code above generates the following result.
The following code provides a short example that declares an array of string objects and passes the array to a function that displays the contents.
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 5;
void display(const string sa[], int n);
/*from w w w. j a va 2s. co m*/
int main()
{
string list[SIZE]; // an array holding 5 string object
cout << "Enter your " << SIZE << " favorite astronomical sights:\n";
for (int i = 0; i < SIZE; i++){
cout << i + 1 << ": ";
getline(cin,list[i]);
}
cout << "Your list:\n";
display(list, SIZE);
return 0;
}
void display(const string sa[], int n){
for (int i = 0; i < n; i++)
cout << i + 1 << ": " << sa[i] << endl;
}
The code above generates the following result.
#include <iostream>
#include <array>
#include <string>
const int Seasons = 4;
const std::array<std::string, Seasons> Snames =
{"Baseball", "Football", "Basketball", "Hockey"};
// w ww .j a va 2 s .c o m
void fill(std::array<double, Seasons> * pa);
void show(std::array<double, Seasons> da);
int main(){
std::array<double, 4> expenses;
fill(&expenses);
show(expenses);
return 0;
}
void fill(std::array<double, Seasons> * pa){
for (int i = 0; i < Seasons; i++){
std::cout << "Enter " << Snames[i] << " expenses: ";
std::cin >> (*pa)[i];
}
}
void show(std::array<double, Seasons> da){
double total = 0.0;
for (int i = 0; i < Seasons; i++){
std::cout << Snames[i] << ": $" << da[i] << '\n';
total += da[i];
}
std::cout << "Total: $" << total << '\n';
}
The code above generates the following result.
Inline functions can speed up programs.
The following code illustrates the inline technique with an inline square() function.
#include <iostream>
using namespace std;
inline double square(double x) { return x * x; }
int main(){/*from ww w . j av a 2s . c om*/
double a, b;
double c = 13.0;
a = square(5.0);
b = square(4.5 + 7.5); // can pass expressions
cout << "a = " << a << ", b = " << b << "\n";
cout << "c = " << c;
cout << ", c squared = " << square(c++) << "\n";
cout << "Now c = " << c << "\n";
// cin.get();
return 0;
}
The code above generates the following result.