General Notes

Subroutine/Functions

  • Subroutine is a reusable block of code that is given a name
  • Designed to perform a specific task
  • Often called within a program to avoid repetition.
  • Can be tested/improved independently of program
  • Easy to share procedures/functions with other programs
  • Create routines that can be called like built-in command
  • Header: The first statement in subroutine definition
    • Includes name of the subroutine
    • Includes parameter passed along with data type
    • Includes return data type if it exists

Procedure

  • Subroutine that does not return a value
  • Procedure calls are standalone statements

Function

  • Subroutine that returns a value.
  • Function calls are always made as part of an expression
  • The keyword return is used as one of the statements within the body of the function to specify the value to be returned.
  • Normally, this will be the last statement in the function definition, however, if the return statement is in the body of the function its execution is immediate and any subsequent lines of code are omitted.
  • Because a function returns a value that is used when the function is called, function calls are not complete program statements
  • When the return statement is executed, the value returned replaces the function call in the expression and the expression is then evaluated

Parameter

  • Variables used in a subroutine to accept input values, allowing data to be passed into the subroutine.
  • A subroutine may or may not have parameters.
  • When calling a function, the parameters used must be of correct data type and of the same sequence as the definition
  • Parameters should not be passed by reference to a function.
  • Unless otherwise stated, it should be assumed that parameters are passed by value.

Argument: The actual value or data passed to the subroutine when it is called

Local Variable: Variables defined within a function or block, accessible only within that specific scope. These variables are not accessible outside the subroutine

Global Variable: Variables defined outside all functions or blocks, accessible throughout the entire program.

Procedure/Function Interface: The way a procedure or function interacts with the rest of the program. It includes its name, parameters (inputs), and, for functions, the return value.

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

Library Routines

  • A library routine is a pre-written, reusable function or procedure provided by a programming library to perform common tasks, such as mathematical operations or input/output handling.
  • IDEs typically include a standard library of functions and procedures
  • Programming language development systems often offer library routines that can be easily integrated into programs

Best Coding Practices

  • Use meaningful identifiers name for variables, constants, arrays, procedure, functions
  • Use commenting feature wisely
  • Divide the program into different modules using procedures and functions