# Showing the functions a calculation calls A calculation is often built from functions written once and used again: a section property, a resistance, a check. `# hc: --dive` (or `-d`) on a line shows the working inside each function the line calls, with the values of that call, before the line itself. You don't need `@handcalc` on the functions. ```{hc-example} def plastic_moment(Z, F_y): M_p = Z * F_y return M_p def flexural_resistance(Z, F_y, phi=0.9): """Factored flexural resistance, CSA S16 13.5.""" M_p = plastic_moment(Z, F_y) M_r = phi * M_p return M_r Z_x = 1210e3 # mm³ F_y = 350 # MPa M_r = flexural_resistance(Z_x, F_y) # hc: --dive ``` The block starts with a header that names the call and matches each argument to the parameter it is passed as: `Z_x → Z` means the line passes `Z_x`, and the function calls it `Z`. Every parameter is listed, the ones with the same name too (`F_y → F_y`). A parameter left at its default shows its value, `φ=0.9`. An argument that is an expression is shown as written: `2 * b_f → b`. Under the header come the function's docstring, as a note, and its body, worked through with the values of this call. The calling line follows, with the function's result. A final `return M_r` isn't shown, because the line above it already shows `M_r`. A `return` of an expression (`return a * b`) is shown with its value. ## How deep to go A dive shows one level: the functions the marked line calls. To go further, mark the lines inside a function that call other functions. The comments are part of the function, so a library of calculation functions can carry its own `--dive` comments, and every calculation that dives into them shows the same depth: ```{hc-example} def plastic_moment(Z, F_y): M_p = Z * F_y return M_p def flexural_resistance(Z, F_y, phi=0.9): M_p = plastic_moment(Z, F_y) # hc: --dive M_r = phi * M_p return M_r M_r = flexural_resistance(1210e3, 350) # hc: --dive ``` ## Which calls `--dive` opens every call on the line to a function written in Python. To open only some of them, name them: `# hc: --dive flexural_resistance`. Some calls have no Python code to show, so they are skipped: - builtins and compiled functions (`max`, `math.sqrt`, numpy's ufuncs); - lambdas, generators and classes; - functions whose source can't be read. A function you named that has nothing to show gets a warning; a plain `--dive` skips these calls quietly. When a line calls the same function twice (`area(a) + area(b)`), each call gets its own block, in the order the calls ran. In a loop, the last pass is shown, as for every other line in a loop. ## Scope As with every command, `--dive` at the end of a line applies to that line, and on its own line it applies to every line after it, until `# hc: dive = False`: ```python # hc: --dive M_r = flexural_resistance(Z_x, F_y) V_r = shear_resistance(A_w, F_y) # hc: dive = False ``` Commands inside a function's body apply inside it only. A `# hc: -f .1f` in the body formats the body's lines, and the lines after the call keep the format they had. A line hidden with `-i` hides its dive too. ## In each renderer | Renderer | A dive is | |---|---| | ASCII, Markdown, LaTeX | the header, with the body indented (quoted in Markdown) | | HTML | a tinted header, with the body indented beside a coloured rule for each level | | Word | the header, with the body indented | | Excel | a header row, a row for each parameter (a formula reading the argument), then the body's rows, grouped so the dive folds away | In Excel, each name inside a dive has the function's name in front (`flexural_resistance.M_r`), so it never clashes with the sheet's own `M_r`. The calling line's formula reads the cell of the name the function returns. For that, the function must end with `return `. A dive needs each line's values, which handcalcs records while the code runs. It works in every interface (`%%render`, `HandCalcs`, `@handcalc` and the command line), but not with the `line_values` option turned off.