11

The title says it all.

What is the difference between a Function and a Procedure?

When should I use which?

  • 2
    What is the connection to TeX and LaTeX? – Stefan Kottwitz Nov 19 '13 at 8:29
  • 5
    The question is about the commands provided by algpseudocode, so is on topic. – Andrew Swann Nov 19 '13 at 9:13
14

According to algpseudocode, these two are structurally the same, apart from their name:

\algdef{SE}[PROCEDURE]{Procedure}{EndProcedure}%
   [2]{\algorithmicprocedure\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}%
   {\algorithmicend\ \algorithmicprocedure}%
\algdef{SE}[FUNCTION]{Function}{EndFunction}%
   [2]{\algorithmicfunction\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}%
   {\algorithmicend\ \algorithmicfunction}%

From a programming perspective, the difference is embedded in the language and have the following commonly-used structure (in laymen's terms):

  • Procedure: A collection of instructions
  • Function: A collection of instructions that also returns something

A minimal example indicating their typical usage:

enter image description here

\documentclass{article}
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\begin{document}
\begin{algorithmic}
\Procedure{name}{params}
  \State body
\EndProcedure
\Function{name}{params}
  \State body
  \State \Return something
\EndFunction
\end{algorithmic}
\end{document}
  • should we specify the output of Function somewhere or it is just specified through return? – Ahmad Jul 9 '16 at 6:37
  • Is it the standard, if we mention both input and output parameters in {params} – Renjth V Ravi Sep 17 '17 at 1:23
  • @RenjthVRavi: That depends. This is pseudo-code, so as long as it's clear in the presentation. – Werner Sep 17 '17 at 17:17
  • A function, depending on definition, also does not modify state (has side-effects). – Jochem Kuijpers Nov 21 '18 at 19:36
5

This is not at all a TeX-related question, but a function returns a value while a procedure does not (void in C++). A procedure only performs some actions, it is invoked because of its side effects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.