Upcoming SlideShare
×

# Theory 4

367
-1

Published on

Published in: Education, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
367
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Theory 4

1. 1. Theory # 4 Functions
2. 2.   <ul><ul><li>Functions </li></ul></ul><ul><ul><li>Returning a value from a function </li></ul></ul><ul><ul><li>Sending values to a functions </li></ul></ul><ul><ul><li>Arguments </li></ul></ul><ul><ul><li>External Variables </li></ul></ul><ul><ul><li>Prepocessor directives </li></ul></ul>
3. 3. Functions <ul><ul><li>Avoiding Unnecessary Repetition of Code </li></ul></ul><ul><ul><li>Program Organization </li></ul></ul>
4. 4. Simple Functions <ul><ul><li>Structure of Functions </li></ul></ul><ul><ul><li>The Function Definition </li></ul></ul><ul><ul><li>Calling the Function </li></ul></ul><ul><ul><li>Function Prototype (Declaration) </li></ul></ul><ul><ul><li>Local Variables </li></ul></ul>
5. 5. Functions That Return a Value <ul><ul><li>The return Statement </li></ul></ul><ul><ul><li>Returning Type (int, float, etc) </li></ul></ul><ul><ul><li>Limitation of return() </li></ul></ul><ul><li>  </li></ul>
6. 6. Using Arguments to Pass Data to a Function <ul><ul><li>Structure of Function Call with Arguments </li></ul></ul><ul><ul><li>Passing Variables as Arguments </li></ul></ul><ul><ul><li>Passing Multiple Arguments </li></ul></ul>
7. 7. Using More Than One Function <ul><li>  </li></ul>
8. 8. External Variables <ul><li>int external_num; </li></ul><ul><li>void print(void); </li></ul><ul><li>void main(void) </li></ul><ul><li>{ </li></ul><ul><li>     external_num = 9; </li></ul><ul><li>    print(); </li></ul><ul><li>     </li></ul><ul><li>} </li></ul><ul><li>void print(void) </li></ul><ul><li>{ </li></ul><ul><li>    printf(&quot;%d&quot;,external_num); </li></ul><ul><li>} </li></ul>
9. 9. Argument Names in the Declarator <ul><li>void drawSquare(int , int); </li></ul><ul><li>void drawSquare(int width, int height); </li></ul>
10. 10. Proprocessor Directives <ul><li>The #define Directive </li></ul><ul><li>#define PI3.14159 </li></ul><ul><li>float area(float); </li></ul><ul><li>void main(void) </li></ul><ul><li>{ </li></ul><ul><li>    printf(&quot;area of sphere is %.2f&quot;, area(3)) </li></ul><ul><li>} </li></ul><ul><li>float area(float rad) </li></ul><ul><li>{ </li></ul><ul><li>    return (4 * PI * rad * rad);   </li></ul><ul><li>} </li></ul>
11. 11.   <ul><li>why use #define </li></ul><ul><li>why not use variable Names </li></ul><ul><li>The const Modifier </li></ul><ul><li>    const float PI = 3.141592; </li></ul>
12. 12. Macros <ul><li>#define ERROR printf(&quot;nError.n&quot;); </li></ul><ul><li>#define PR(n) printf(&quot;%.2f&quot;,n); </li></ul><ul><li>void main(void) </li></ul><ul><li>{ </li></ul><ul><li>    float num1 = 27.25; </li></ul><ul><li>    float num2; </li></ul><ul><li>     num2 = 1.0 / 3.0 </li></ul><ul><li>    if(num1 > 2) </li></ul><ul><li>     { </li></ul><ul><li>        ERROR; </li></ul><ul><li>     } </li></ul><ul><li>    PR(num1); </li></ul><ul><li>     PR(num2); </li></ul><ul><li>} </li></ul>
13. 13. Macros and Functions <ul><ul><li>#define PI 3.14159 </li></ul></ul><ul><ul><li>#define AREA(X) (4* PI * X * X); </li></ul></ul><ul><ul><li>#define SUM(x,y) (x+y); </li></ul></ul>