Revisit Terms and Concepts of Functions by Jumail Bin Taliba Faculty of Computer Science & Information System
Page 1 Page 2 How a function works? Example:  What is the output of the following flowchart when the input is   N = 6 This...
Thinking in “f unction way” Example:  Create a flowchart to calculate the average of three numbers The detail of  how the ...
Function Name and Parameters This flowchart calculates the average of three numbers Page 1 Page 2 AVRG  is the  function n...
In a function-definition, you should only  use formal parameters – R, a, b, c You shouldn’t use actual parameters  Page 1 ...
Page 1 This flowchart calculates the average of three numbers Page 2 A function may be  called more than once At this time...
Input and Output Parameters <ul><li>Each function parameter may act as:  </li></ul><ul><ul><li>Input </li></ul></ul><ul><u...
This flowchart calculates the average of three numbers Page 1 Page 2 Function call: result  is the  output  parameter .   ...
Page 1 Page 2 This flowchart exchanges or swaps the value  of x and y each other Function call: p  and  q  act as both  in...
If there is  only one output  parameter, the flowchart may  “RETURN”  the result Example: let’s take a look again at  the ...
Since the function-definition has been modified, the way of the function to be called will also be changed  Original main ...
Writing a C Program is a systematic task  Problem Flowchart Intermediate code -prepare a rough code- Complete code - add d...
Converting flowchart to C code is almost straight forward Example: multiplying two numbers void main (void) { scanf(&A); s...
You will get some errors Error 1: Call to undefined function ‘scanf’ The compiler doesn’t  recognize ‘scanf’ Error 2: Unde...
Fixing the errors and completing the program This line will help the compiler to recognize words ‘ scanf ’ and ‘ printf ’....
Converting function flowcharts Problem:  Finding the average of three numbers Flowcharts: Example:
void main () { scanf(&n1,&n2,&n3); Average( &avrg , n1, n2, n3); printf(avrg); } Intermediate code of the main flowchart P...
void  Average (*result , a, b, c) { sum = a + b + c; *result  = sum / 3.0; return; } Intermediate code of the function flo...
#include <stdio.h> void Average( float  *result ,  float  a,  float  b,  float   c) {  float sum; sum = a + b + c; *result...
Example:  This example is the same as the previous one but this time the result is “return” Problem:  Finding the average ...
void main () { scanf(&n1,&n2,&n3); avrg = Average(n1, n2, n3); printf(avrg); } Intermediate code of the main flowchart Pre...
Average(a, b, c) { sum = a + b + c; result  = sum / 3.0; return result ; } Intermediate code of the function flowchart Pre...
#include <stdio.h> float  Average( float  a,  float  b,  float   c) {  float sum; float result; sum = a + b + c; result = ...
Upcoming SlideShare
Loading in …5
×

04 0 Revisit Terms And Concepts Of Functions

992 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
992
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

04 0 Revisit Terms And Concepts Of Functions

  1. 1. Revisit Terms and Concepts of Functions by Jumail Bin Taliba Faculty of Computer Science & Information System
  2. 2. Page 1 Page 2 How a function works? Example: What is the output of the following flowchart when the input is N = 6 This flowchart calculates the average of three numbers average 10 5 N=6 Sum = 10 + 5 + 6 average = 21/3 Output: Average: 7
  3. 3. Thinking in “f unction way” Example: Create a flowchart to calculate the average of three numbers The detail of how the function should work is put into another flowchart. This flowchart is known as a function-definition This flowchart calculates the average of three numbers Page 1 Page 2 End terminal must be a “Return” Start terminal for a function is different. Do not use “Start” This part is known as a function-call At this point, we only focus on what we are going to do . But, how it is going to be done , we focus later. This part is called body of function . It is nothing much different from main flowchart
  4. 4. Function Name and Parameters This flowchart calculates the average of three numbers Page 1 Page 2 AVRG is the function name Objects enclosed by ( ) – result, n1, n2, n3 - are called parameters Parameters used in a function-call are called actual parameters result, n1, n2, n3 are actual parameters Parameters used in a function-definition are called formal parameters R, a, b, c are formal parameters <ul><li>Each formal parameter represents </li></ul><ul><li>an actual parameter according </li></ul><ul><li>to its order : </li></ul><ul><ul><li>R represents result , </li></ul></ul><ul><ul><li>a represents n1 , </li></ul></ul><ul><ul><li>b represents n2 , </li></ul></ul><ul><ul><li>c represents n3 </li></ul></ul><ul><li>The name of an actual parameter may be </li></ul><ul><li>different from its formal parameter </li></ul>
  5. 5. In a function-definition, you should only use formal parameters – R, a, b, c You shouldn’t use actual parameters Page 1 Page 2 This is wrong! n1, n2, n3 are actual parameters. Should use a, b, c instead. This is wrong! R is an formal parameters. Should use result instead.
  6. 6. Page 1 This flowchart calculates the average of three numbers Page 2 A function may be called more than once At this time: R represents average1 , a represents n1 , b represents n2 , c represents n3 When comes to this: R represents average2 , a represents n4 , b represents n5 , c represents n6
  7. 7. Input and Output Parameters <ul><li>Each function parameter may act as: </li></ul><ul><ul><li>Input </li></ul></ul><ul><ul><ul><li>data to be processed by the function </li></ul></ul></ul><ul><ul><li>Output </li></ul></ul><ul><ul><ul><li>the result produced by the function </li></ul></ul></ul><ul><ul><li>Both </li></ul></ul>
  8. 8. This flowchart calculates the average of three numbers Page 1 Page 2 Function call: result is the output parameter . n1, n2, n3 are the input parameters. Function definition: R is the output parameter a, b, c are input parameters
  9. 9. Page 1 Page 2 This flowchart exchanges or swaps the value of x and y each other Function call: p and q act as both input and output parameters. Function definition: x and y act as both input and output parameters
  10. 10. If there is only one output parameter, the flowchart may “RETURN” the result Example: let’s take a look again at the function that calculates the average of three numbers. Original function flowchart: Since it has only one output, the output may be “RETURN” Page 2 Page 2 The output parameter (R) is removed from the formal parameter list and the result is return
  11. 11. Since the function-definition has been modified, the way of the function to be called will also be changed Original main flowchart: Modified main flowchart: Page 1 Page 1 Now , result is not anymore a parameter of the function-call
  12. 12. Writing a C Program is a systematic task Problem Flowchart Intermediate code -prepare a rough code- Complete code - add details - Converting function flowcharts to C Code
  13. 13. Converting flowchart to C code is almost straight forward Example: multiplying two numbers void main (void) { scanf(&A); scanf(&B); C = A * B; printf(C); } Intermediate C Code But!, the program still cannot be executed. It is not completed yet. This code is called an intermediate code.
  14. 14. You will get some errors Error 1: Call to undefined function ‘scanf’ The compiler doesn’t recognize ‘scanf’ Error 2: Undefined symbol ‘A’ The program is trying to use a variable A but has never been registered . Compiler doesn’t recognize the variable
  15. 15. Fixing the errors and completing the program This line will help the compiler to recognize words ‘ scanf ’ and ‘ printf ’. File stdio.h contains the information of those functions and some others. This tells to register (declare) variables. Compiler only recognizes registered variables. You may notice some extra things. These are called prompts . They used to let the user knows what is going on while the program is running
  16. 16. Converting function flowcharts Problem: Finding the average of three numbers Flowcharts: Example:
  17. 17. void main () { scanf(&n1,&n2,&n3); Average( &avrg , n1, n2, n3); printf(avrg); } Intermediate code of the main flowchart Preparing the rough code The ampersand (&) indicates that avrg is the output or result of the function-call .
  18. 18. void Average (*result , a, b, c) { sum = a + b + c; *result = sum / 3.0; return; } Intermediate code of the function flowchart Preparing the rough code The asterisk (*) indicates that result is an output parameter .
  19. 19. #include <stdio.h> void Average( float *result , float a, float b, float c) { float sum; sum = a + b + c; *result = sum/3.0; return; } void main () { float n1; float n2; float n3; float avrg; printf (&quot;Enter three numbers: “); scanf (“%f%f%f”, &n1, &n2, &n3); Average( &avrg ,n1,n2,n3); prinf (&quot;The average is %f“, avrg); } Complete source code Adding details to the rough code. The details are shown by bold texts
  20. 20. Example: This example is the same as the previous one but this time the result is “return” Problem: Finding the average of three numbers Flowcharts:
  21. 21. void main () { scanf(&n1,&n2,&n3); avrg = Average(n1, n2, n3); printf(avrg); } Intermediate code of the main flowchart Preparing the rough code
  22. 22. Average(a, b, c) { sum = a + b + c; result = sum / 3.0; return result ; } Intermediate code of the function flowchart Preparing the rough code <ul><li>Notice that, </li></ul><ul><li>result is removed </li></ul><ul><li>from parameter list </li></ul><ul><li>“ void” is also removed </li></ul>
  23. 23. #include <stdio.h> float Average( float a, float b, float c) { float sum; float result; sum = a + b + c; result = sum/3.0; return result; } void main () { float n1; float n2; float n3; float avrg; printf (&quot;Enter three numbers: “); scanf (“%f%f%f”, &n1, &n2, &n3); avrg = Average(n1,n2,n3); prinf (&quot;The average is %f“, avrg); } Complete source code Adding details to the rough code. The details are shown by bold texts

×