Update
This commit is contained in:
parent
a62675cb93
commit
a64b3eb7a9
19 changed files with 1978 additions and 1966 deletions
|
@ -368,11 +368,11 @@ int main(void)
|
|||
|
||||
## Functions (Subprograms)
|
||||
|
||||
Functions are extremely important, no program besides the most primitive ones can be made without them (well, in theory any program can be created without functions, but in practice such programs would be extremely complicated and unreadable).
|
||||
Functions are highly important in programming, no program besides the most primitive ones can be made without them (well, in theory any program can be created without functions, but in practice such programs would be too complicated, unreadable and unmaintainable).
|
||||
|
||||
**[Function](function.md) is a subprogram** (in other languages functions are also called procedures or subroutines), i.e. it is code that solves some smaller subproblem that you can repeatedly invoke, for instance you may have a function for computing a [square root](sqrt.md), for encrypting data or for playing a sound from speakers. We have already met functions such as `puts`, `printf` or `rand`.
|
||||
|
||||
Functions are similar to but **NOT the same as mathematical functions**. Mathematical function (simply put) takes a number as input and outputs another number computed from the input number, and this output number depends only on the input number and nothing else. C functions can do this too but they can also do additional things such as modify variables in other parts of the program or make the computer do something (such as play a sound or display something on the screen) -- these are called **[side effects](side_effect.md)**; things done besides computing an output number from an input number. For distinction mathematical functions are called *pure* functions and functions with side effects are called non-pure.
|
||||
Functions are similar to but **NOT the same as mathematical functions**. Mathematical function (simply put) takes a number on input and outputs another number computed from the input number, and the output number depends solely on the input number and nothing else. C functions can do this too but they can also do additional things such as modify variables in other parts of the program or make the computer do something (such as play a sound or display something on the screen) -- these are called **[side effects](side_effect.md)**; things done besides computing an output number from an input number. For distinction mathematical functions are called *pure* functions and functions with side effects are called non-pure.
|
||||
|
||||
**Why are function so important?** Primarily they help us divide a big problem into small subproblems and make the code better organized and readable, but mainly they aid us in respecting the [DRY](dry.md) (*Don't Repeat Yourself*) principle -- this is extremely important in programming. Imagine you need to solve a [quadratic equation](quadratic_equation.md) in several parts of your program; you do NOT want to solve it in each place separately, you want to make a function that solves a quadratic equation and then only invoke (call) that function anywhere you need to solve your quadratic equation. This firstly saves space (source code will be shorter and compiled program will be smaller), but it also makes your program manageable and eliminates bugs -- imagine you find a better (e.g. faster) way to solving quadratic equations; without functions you'd have to go through the whole code and change the algorithm in each place separately which is impractical and increases the chance of making errors. With functions you only change the code in one place (in the function) and in any place where your code invokes (calls) this function the new better and updated version of the function will be used.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue