Upcoming SlideShare
×

# Functions & Procedures [7]

597 views
461 views

Published on

Published in: Technology, Education
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
597
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
5
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Functions & Procedures [7]

1. 1. Programming & Algorithm Functions & Procedures August 17, 2006
2. 2. What Is a Function?
3. 3. What Is a Function? <ul><li>A function is a named , independent section of code that performs a specific task and optionally returns a value to the calling program . </li></ul>
4. 4. How a Function Works <ul><li>E.g.: </li></ul><ul><li>PROGRAM function_illustration </li></ul><ul><li>KAMUS </li></ul><ul><li>function quadrate (x: integer) -> integer </li></ul><ul><li>a, result: integer </li></ul><ul><li>ALGORITMA </li></ul><ul><li>input(a) </li></ul><ul><li>result ← quadrate(a) </li></ul><ul><li>output(result) </li></ul><ul><li>function quadrate (x: integer) -> integer </li></ul><ul><li>KAMUS LOKAL </li></ul><ul><li>ALGORITMA </li></ul><ul><li> -> x*x </li></ul>
5. 5. How a Function Works
6. 6. Writing a Function <ul><li>: PROGRAM function_illustration </li></ul><ul><li>: </li></ul><ul><li>: KAMUS </li></ul><ul><li>: function quadrate (x: integer) -> integer </li></ul><ul><li>: a, result: integer </li></ul><ul><li>: </li></ul><ul><li>: ALGORITMA </li></ul><ul><li>: input(a) </li></ul><ul><li>: result ← quadrate(a) </li></ul><ul><li>: output(result) </li></ul><ul><li>: {---------------------------------------------------------------------------------------------------------------} </li></ul><ul><li>: function quadrate (x: integer) -> integer </li></ul><ul><li>: </li></ul><ul><li>: KAMUS LOKAL </li></ul><ul><li>: value: integer </li></ul><ul><li>: </li></ul><ul><li>: ALGORITMA </li></ul><ul><li>: value ← x * x; </li></ul><ul><li>: -> value; </li></ul>
7. 7. Writing a Function <ul><li>The function components: </li></ul><ul><li>The Function Header [Line 12] </li></ul><ul><ul><li>The Function Return Type </li></ul></ul><ul><ul><li>The Function Name </li></ul></ul><ul><ul><li>The Parameter List </li></ul></ul><ul><li>The Function Body [Lines 18-19] </li></ul><ul><ul><li>Local Variables [Line 15] </li></ul></ul><ul><ul><li>Function Statements [Lines 18] </li></ul></ul><ul><ul><li>Returning a Value [Lines 19] </li></ul></ul>
8. 8. Recursion <ul><li>The term recursion refers to a situation in which a function call itself either directly or indirectly. </li></ul>
9. 9. Exercises <ul><li>Create a program that inputs three integers and displays the average of them on screen. The program uses a function named average(). </li></ul><ul><li>Create a program that inputs one integer (N) and display the words “I love C” N times. The program uses a procedure named I_love_C(). </li></ul>
10. 10. Exercises <ul><li>Create a program that inputs one integer (N) and displays the factorial of N on screen. The program uses a function named factorial(). </li></ul><ul><li>Create the program number 3 using a recursive function named rec_factorial(). </li></ul>