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

Functions & Procedures [7]

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