C++ primer 函数(1)

//第七章:函数
//函数基本知识
//函数原型
//按值传递函数参数
//设计处理数组的函数
//使用const指针参数
//设计处理文版字符串的函数

#include <iostream>
void cheers(int);
double cube(double x);

int main()
{
    using namespace std;
    cheers(5);
    cout<< "give me a number: ";
    double side;
    cin>>side;
    double volume = cube(side);
    cout<<"A "<< side << "-foot cube has a volume of ";
    cout << volume << " cubic feet.\n";
    cheers(cube(2));
    
    
    return 0;
}

void cheers(int n)
{
    using namespace std;
    for(int i = 0; i< n; i++)
        cout << "cheers! ";
    cout << endl;
}

double cube(double x)
{
    return x*x*x;
}

在这里插入图片描述
在这里插入图片描述
实验结果:

cheers! cheers! cheers! cheers! cheers! 
give me a number: 3
A 3-foot cube has a volume of 27 cubic feet.
cheers! cheers! cheers! cheers! cheers! cheers! cheers! cheers! 
Program ended with exit code: 0

//第七章:函数
//函数基本知识
//函数原型
//按值传递函数参数
//设计处理数组的函数
//使用const指针参数
//设计处理文版字符串的函数

#include <iostream>
using namespace std;

void n_chars(char, int);


int main()
{
    int times;
    char ch;
    std::cout<<"Enter an integer: ";
    cin >> ch;
    while(ch != 'q')
    {
        cout<<"Enter an integer: ";
        cin>>times;
        n_chars(ch, times);
        cout << " \nEmter another character or press the q-key to quit: ";
        cin >> ch;
    }
  //  cout << "The value of times is "<< times << ".\n";
    cout << "Bye\n";
    
    return 0;
}

void n_chars(char c, int n)
{
    while(n--> 0)
        cout << c;
    
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190422204807862.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3pqZ3VpbGFp,size_16,color_FFFFFF,t_70)

猜你喜欢

转载自blog.csdn.net/zjguilai/article/details/89460106