c++的经验!

The first three statements are straightforward.

The fourth is relatively easy as well:

1
2
3
int a = 3;
int b = 5;
cout << add(a, b) << endl; // evaluates 3 + 5

In this case, add() is called where x = a and y = b. Since a = 3 and b = 5, add(a, b) = add(3, 5), which resolves to 8.

Let’s take a look at the first tricky statement in the bunch:

1
cout << add(1, multiply(2, 3)) << endl; // evalues 1 + (2 * 3)

When the CPU tries to call function add(), it assigns x = 1, and y = multiply(2, 3). y is not an integer, it is a function call that needs to be resolved. So before the CPU calls add(), it calls multiply() where z = 2 and w = 3. multiply(2, 3) produces the value of 6, which is assigned to add()’s parameter y. Since x = 1 and y = 6, add(1, 6) is called, which evaluates to 7. The value of 7 is passed to cout.

Or, less verbosely (where the => symbol is used to represent evaluation):
add(1, multiply(2, 3)) => add(1, 6) => 7

The following statement looks tricky because one of the parameters given to add() is another call to add().

1
cout << add(1, add(2, 3)) << endl; // evalues 1 + (2 + 3)

But this case works exactly the same as the above case where one of the parameters is a call to multiply().

Before the CPU can evaluate the outer call to add(), it must evaluate the inner call to add(2, 3). add(2, 3) evaluates to 5. Now it can evaluate add(1, 5), which evaluates to the value 6. cout is passed the value 6.

Less verbosely:
add(1, add(2, 3)) => add(1, 5) => 6

Effectively using functions

One of the biggest challenges new programmers encounter (besides learning the language) is learning when and how to use functions effectively. Functions offer a great way to break your program up into manageable and reusable parts, which can then be easily connected together to perform a larger and more complex task. By breaking your program into smaller parts, the overall complexity of the program is reduced, which makes the program both easier to write and to modify.

Typically, when learning C++, you will write a lot of programs that involve 3 subtasks:

  1. Reading inputs from the user
  2. Calculating a value from the inputs
  3. Printing the calculated value

For simple programs, reading inputs from the user can generally be done in main(). However, step #2 is a great candidate for a function. This function should take the user inputs as a parameter, and return the calculated value. The calculated value can then be printed (either directly in main(), or by another function if the calculated value is complex or has special printing requirements).

A good rule of thumb is that each function should perform one (and only one) task. New programmers often write functions that combine steps 2 and 3 together. However, because calculating a value and printing it are two different tasks, this violates the one and only one task guideline. Ideally, a function that calculates a value should return the value to the caller and let the caller decide what to do with the calculated value.

Quiz

1) What’s wrong with this program fragment?

1
2
3
4
5
6
7
8
9
10
void multiply( int x, int y)
{
     return x * y;
}
 
int main()
{
     cout << multiply(4, 5) << endl;
     return 0;
}

2) What’s wrong with this program fragment?

1
2
3
4
5
6
7
8
9
10
int multiply( int x, int y)
{
     int product = x * y;
}
 
int main()
{
     cout << multiply(4, 5) << endl;
     return 0;
}

3) What value does the following program fragment print?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int add( int x, int y, int z)
{
     return x + y + z;
}
 
int multiply( int x, int y)
{
     return x * y;
}
 
int main()
 
{
     cout << multiply(add(1, 2, 3), 4) << endl;
     return 0;
}

4) Write a function called doubleNumber() that takes one integer parameter and returns double it’s value.

5) Write a complete program that reads an integer from the user (using cin, discussed in section 1.3), doubles it using the doubleNumber() function you wrote for question 4, and then prints the doubled value out to the console.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值