Unit 7: Functions - Tin học đại cương (IT1110) | Trường Đại học Bách khoa Hà Nội
Unit 7: Functions
Preview text:
lOMoAR cPSD| 27879799 lOMoAR cPSD| 27879799 Functions lOMoAR cPSD| 27879799
Avoid rewriting same logic/code again and again in a
Improve understandability of very large C programs. 3
Downloaded by VietJack TV Official (vietjackvideos@gmail.com) lOMoAR cPSD| 27879799
functions with library functions lOMoAR cPSD| 27879799 Where to write function definition?
• The function definition itself can act as an implicit function declaration.
• All identifiers in C need to be declared before they
are used.That’s why function definitions were put
before main. If the order was reversed the compiler
would not recognize the function. 5
Downloaded by VietJack TV Official (vietjackvideos@gmail.com) lOMoAR cPSD| 27879799
• To correct this a prototype could be added before main. lOMoAR cPSD| 27879799 definition 7
Downloaded by VietJack TV Official (vietjackvideos@gmail.com) lOMoAR cPSD| 27879799 definition with lOMoAR cPSD| 27879799 Function definition
Function-name: any valid identifier 9
Downloaded by VietJack TV Official (vietjackvideos@gmail.com) lOMoAR cPSD| 27879799 Function definitions lOMoAR cPSD| 27879799 11
Downloaded by VietJack TV Official (vietjackvideos@gmail.com) lOMoAR cPSD| 27879799 s lOMoAR cPSD| 27879799 13
Downloaded by VietJack TV Official (vietjackvideos@gmail.com) lOMoAR cPSD| 27879799 Avoids accidental changes Call by reference
Changes in function effect original For
now, we focus on call by value lOMoAR cPSD| 27879799 15
Downloaded by VietJack TV Official (vietjackvideos@gmail.com) lOMoAR cPSD| 27879799 Scope rules
Scope rules tell us if an entity ( , variable, lOMoAR cPSD| 27879799 Used for 17
Downloaded by VietJack TV Official (vietjackvideos@gmail.com) lOMoAR cPSD| 27879799 Used for lOMoAR cPSD| 27879799
The variable's scope is limited to the {} block that lOMoAR cPSD| 27879799 20