Unit 7: Functions - Tin học đại cương (IT1110) | Trường Đại học Bách khoa Hà Nội

Unit 7: Functions

lOMoARcPSD| 27879799
lOMoARcPSD| 27879799
Funcons
lOMoARcPSD| 27879799
3
Downloaded by VietJack TV Official (vietjackvideos@gmail.com)
Avoid rewring same logic/code again and again in a
Improve understandability of very large C programs.
lOMoARcPSD| 27879799
lOMoARcPSD| 27879799
5
Downloaded by VietJack TV Official (vietjackvideos@gmail.com)
Where to write funcon
denion?
The funcon denion itself can act as an implicit
funcon declaraon.
All ideners in C need to be declared before they
are used.Thats why funcon denions were put
before main. If the order was reversed the compiler
would not recognize the funcon.
lOMoARcPSD| 27879799
To correct this a prototype could be added before
main.
lOMoARcPSD| 27879799
7
Downloaded by VietJack TV Official (vietjackvideos@gmail.com)
denion
lOMoARcPSD| 27879799
denion
with
lOMoARcPSD| 27879799
9
Downloaded by VietJack TV Official (vietjackvideos@gmail.com)
Funcon denion
Funcon-name: any valid idener
lOMoARcPSD| 27879799
Funcon denions
lOMoARcPSD| 27879799
11
Downloaded by VietJack TV Official (vietjackvideos@gmail.com)
lOMoARcPSD| 27879799
s
lOMoARcPSD| 27879799
13
Downloaded by VietJack TV Official (vietjackvideos@gmail.com)
lOMoARcPSD| 27879799
Avoids accidental changes
Call by reference
Changes in funcon eect original
For now, we focus on call by value
lOMoARcPSD| 27879799
15
Downloaded by VietJack TV Official (vietjackvideos@gmail.com)
lOMoARcPSD| 27879799
Scope rules
Scope rules tell us if an enty (
, variable,
lOMoARcPSD| 27879799
17
Downloaded by VietJack TV Official (vietjackvideos@gmail.com)
Used for
lOMoARcPSD| 27879799
Used for
lOMoARcPSD| 27879799
The variable's scope is limited to the {} block that
lOMoARcPSD| 27879799
20
| 1/27

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