C++通常通過(guò)值傳遞參數(shù)。
例如,
double volume = cube(side);
這邊是一個(gè)變量,在運(yùn)行中,值為5。
cube()的函數(shù)頭是這樣的:
double cube(double x)
調(diào)用此函數(shù)時(shí),將創(chuàng)建一個(gè)新的類型double變量x,并將其初始化為5。
一個(gè)函數(shù)可以有多個(gè)參數(shù)。
在函數(shù)調(diào)用中,你只需用逗號(hào)分隔參數(shù):
#include <iostream>
using namespace std;
void n_chars(char, int);
int main()
{
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;
}
上面的代碼生成以下結(jié)果。
以下代碼說(shuō)明如何使用指針,就像它是一個(gè)數(shù)組名稱一樣。
該程序?qū)?shù)組初始化為某些值,并使用sum_arr()函數(shù)來(lái)計(jì)算和。
#include <iostream>
using namespace std;
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;
}
上面的代碼生成以下結(jié)果。
以下代碼顯示了Cookie和arr具有相同的值。
它顯示了指針如何使sum_arr函數(shù)更加通用。
該程序使用std:: qualifier而不是using指令來(lái)提供對(duì)cout和endl的訪問(wèn)。
#include <iostream>
const int ArSize = 8;
int sum_arr(int arr[], int n);
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;
}
上面的代碼生成以下結(jié)果。
以下代碼使用兩個(gè)指針來(lái)指定范圍。
#include <iostream>
using namespace std;
const int SIZE = 8;
int sum_arr(const int * begin, const int * end);
int main()
{
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;
}
上面的代碼生成以下結(jié)果。
以下代碼計(jì)算給定字符在字符串中出現(xiàn)的次數(shù)。
因?yàn)槌绦虿恍枰幚碡?fù)值,它使用unsigned int作為計(jì)數(shù)類型。
#include <iostream>
using namespace std;
unsigned int c_in_str(const char * str, char ch);
int main()
{
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;
}
上面的代碼生成以下結(jié)果。
以下代碼定義了一個(gè)返回指針的名為buildstr()的函數(shù)。
此函數(shù)有兩個(gè)參數(shù):一個(gè)字符和一個(gè)數(shù)字。
使用new,該函數(shù)創(chuàng)建一個(gè)長(zhǎng)度等于數(shù)字的字符串,然后將每個(gè)元素初始化為字符。
然后它返回一個(gè)指向新字符串的指針。
#include <iostream>
using namespace std;
char * buildstr(char c, int n);
int main()
{
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;
}
上面的代碼生成以下結(jié)果。
#include <iostream>
using namespace std;
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";
}
上面的代碼生成以下結(jié)果。
以下代碼提供了一個(gè)簡(jiǎn)短的示例,它聲明一個(gè)字符串對(duì)象數(shù)組,并將該數(shù)組傳遞給顯示內(nèi)容的函數(shù)。
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 5;
void display(const string sa[], int n);
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;
}
上面的代碼生成以下結(jié)果。
#include <iostream>
#include <array>
#include <string>
const int Seasons = 4;
const std::array<std::string, Seasons> Snames =
{"Baseball", "Football", "Basketball", "Hockey"};
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";
}
上面的代碼生成以下結(jié)果。
內(nèi)聯(lián)函數(shù)可以加速程序。
以下代碼說(shuō)明了使用inline square()函數(shù)的內(nèi)聯(lián)技術(shù)。
#include <iostream>
using namespace std;
inline double square(double x) { return x * x; }
int main(){
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;
}
上面的代碼生成以下結(jié)果。
更多建議: