The power of Functions

△ grow upward, TO BE TO UP. Programmer growth charging station △

640?wx_fmt=jpeg

Computer Basics Lesson  29  The Sharing

Reprint please contact an authorized (micro letter ID: qianpangzi0206)


01

Index Calculation


Then speaking of games, we can use any number, 10, 42, 10 billion and assume that after the end of each level the player a number of bonus points, bonus depends on how many points the remaining number of relays. With increasing difficulty, and the remaining relays will become increasingly difficult, and therefore reward points based on the current number of clubs grew, exponentially. We write a short code to calculate the index, the index is a number multiplied by yourself, take a specific number of times. With a loop to achieve almost perfect!

First, create one called "bonus points" of the new variable set to 1, then for the cycle, from 1 to [current number of checkpoints], [bonus points] x [Relay remaining], the result is stored [bonus points]. For example the number of relays is 2, the number of points is 3, will be for 3 cycles, by incentives branch, the number of x number of relays Relay Relay number x, which is 1 × 2 × 2 × 2, 3 bonus points 8,2 of power .

02

Code is packaged into a "function"


The index code is very practical, might be used elsewhere, if every time you want to use to copy and paste, will be difficult, every time the amount of change the name, if the code is found to the problem, to fill holes, make a copy of each sticky posted all over the place to find out reform, and makes the code more difficult to understand. less is more! We want some way, the code "package" can be used directly, the results do not control the internal complexity. This in turn enhance a layer of abstraction!

In order to hide the complexity of the code can be packaged into a "function", also called "methods" or "routine" (so called some programming languages). Elsewhere I want to use this function, write directly to the function name on it.

现在我们把指数代码变成函数。 第一步,取名。叫什么都行,比如"快乐独角兽",但因为是算指数,  直接叫"指数"合适一些。还有,与其用特定变量名,比如 "继电器" 和 "关卡数",用更通用的名字,比如 底数(Base) 和 指数(Exp),Base 和 Exp 的初始值需要外部传入,剩余代码和之前一样,现在完成了,有函数名和新变量名。最后, 我们还需要把结果交给使用这个函数的代码,所以用 RETURN 语句,指明返回什么。

完整版代码是这样,现在可以随意用这个函数,只需要写出名字 然后传入2个数字  就可以了。如果要算 2 的 44 次方,写 exponent(2,44),结果是 18 万亿左右。幕后原理是,2 和 44 存进 Base 和 Exp,跑循环,然后返回结果。

03

使用函数


我们来用这个新函数算奖励分,首先,奖励分初始化为 0,然后用 if 语句,看剩不剩继电器(看上图的 > 0)。如果还剩,用指数函数,传入 [继电器数] 和 [关卡数],它会算 [继电器数]的[关卡数]次方,  存入奖励分。这段算奖励分的代码,之后可能还会用,也打包成一个函数。没错,这个函数 (CalcBonus) 会调用另一个函数 (Exponent)。还有这个 CalcBonus 函数,可以用在其他更复杂的函数。

我们来写一个函数, 每一关结束后都会调用,叫 LevelFinished (关卡结束),需要传入 [剩余继电器数]  [关卡数] [当前分],这些数据必须传入。里面用 CalcBonus 算奖励分,并加进总分。还有,如果当前分 > 游戏最高分,把新高分和玩家名 存起来。

Now the code becomes very " fancy ", the function call function call function , we write this line of code complexity are hidden. Not need to know the inside of loops and variables, only know the result will be returned like magic, like, out of 53, but this is not magic, abstract force. If you understand this example, to understand the core power of modern programming function.

For example, complex procedures such as browser, with a long list of statements to write is not possible, there will be millions of lines of code, no one can understand, so modern software consists of thousands of functions, each responsible for different things.

Today, more than 100 lines of code function is rare, if more than 100 lines, there should be something can be made out of demolition of a function, modular programming can not only make a single programmer indie App, but also to teamwork can write larger program . Different programmers to write different function, just make sure your code works correctly, the owners put together, the whole program should be able to work properly!

04

Storehouse


In reality, the programmer will not waste time writing an exponential function of this kind of thing, there are many modern programming language pre-written set of functions, called "libraries." Written by professionals, not only efficient, but after a careful examination, do almost all things are libraries, networking, image, sound, then we can speak on these topics.

But before we talk about algorithms, curious?

See you next week

Related Reading:

 

  1. Evolved from assembly language to high-level programming language

  2. The basic elements of programming languages


Programmers grow charging station

640?wx_fmt=jpeg

Press scan code concerned, five minutes a day to learn computer the most basic knowledge and principles

Lower right corner

Give a look


Guess you like

Origin blog.csdn.net/cq20110310/article/details/91692699