Skip to main content
Engineering LibreTexts

3.3: Function Examples

  • Page ID
    10639
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Overview

    The following pseudocode and flowchart examples take the Temperature program from the previous chapter and separate the functionality into independent functions for input, processing, and output, as GetFahrenheit, CalculateCelsius, and DisplayResult, respectively.

    Discussion

    As independent functions, each function acts as a miniature program, with its own input, processing, and output. As you review the following code, note which functions have parameters (input) and which functions have return values (output). Parameters and return values will be discussed in the next few pages.

    Function Purpose Parameters (input) Return Value (output)
    Main main program none none
    GetFahrenheit input none fahrenheit
    CalculateCelsius processing fahrenheit celsius
    DisplayResult output fahrenheit, celsius none

    Pseudocode

    Function Main
        ... This program asks the user for a Fahrenheit temperature, 
        ... converts the given temperature to Celsius,
        ... and displays the results.
    
        Declare Real fahrenheit
        Declare Real celsius
        
        Assign fahrenheit = GetFahrenheit()
        Assign celsius = CalculateCelsius(fahrenheit)
        Call DisplayResult(fahrenheit, celsius)
    End
    
    Function GetFahrenheit
        Declare Real fahrenheit
        
        Output "Enter Fahrenheit temperature:"
        Input fahrenheit
    Return Real fahrenheit
    
    Function CalculateCelsius (Real fahrenheit)
        Declare Real celsius
        
        Assign celsius = (fahrenheit - 32) * 5 / 9
    Return Real celsius
    
    Function DisplayResult (Real fahrenheit, Real celsius)
        Output fahrenheit & "° Fahrenheit is " & celsius & "° Celsius"
    End
    

    Output

    Enter Fahrenheit temperature:
     100
    100° Fahrenheit is 37.7777777777778° Celsius
    

    Flowchart

    356px-Flowgorithm_Functions_Main.svg_.png 389px-Flowgorithm_Functions_GetFahrenheit.svg_.png

    390px-Flowgorithm_Functions_CalculateCelsius.svg_.png 640px-Flowgorithm_Functions_DisplayResult.svg_.png


    3.3: Function Examples is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?