Functions vs. Procedures

A procedural program is made up of the following parts:
PROCEDURES (void functions in C)FUNCTIONS
  1. General Purpose
  2. Adds statements to a language
      Examples:
    • input an integer
    • get a real >= zero
    • swap 2 integer values
    • print a menu


  3. Communication methods
    • in only (pass by value)
    • in out (pass an address in C)
    • out only (pass an address in C)

  4. Any results or information needed by the calling program part are returned through out only or in out parameters.





  5. Any valid statement in the language including subprogram calls can be used in the body of a procedure.








  6. The calling program part calls a procedure on a line by itself. A procedure is NOT part of an expression.
  1. Restricted form of a procedure.
  2. Adds operators to a language
      Examples
    • calculate the sum of 2 integers
    • calculate the sine of an angle
    • calculate the area of a rectangle
    • determine if one integer is bigger than another integer

  3. Communication type is in only (pass by value)



  4. One result (no more than one, no less than one, exactly one) is returned to the calling program part through a special function return statement.

    The data type of the returned value determines the type of the function.


  5. Any valid statement in the language other than the following may be used in the body of the function. Calling another function is valid.
    STATEMENTS THAT SHOULD NOT BE IN THE BODY OF A FUNCTION
    • input
    • output
    • procedure calls

  6. Functions are always called as part of another statement, typically as part of an assignment statement or output statement. Basically, any place you can use an expression like "a + b", you can use a function call such as "plus(a,b)"