80 likes | 205 Views
Basic Problem Solving with Programming - Functions. Functional Decomposition.
E N D
Functional Decomposition • Functional Decomposition in computer science, also known as factoring, refers to the process by which a problem or algorithm is broken down into parts that are easier to conceive, understand, program, reuse, and maintain.
Functional Decomposition Example • You are to program a small Lego Mindstorm robot holding a marker to draw numbers on the ground. • Do you write a separate function for each digit? 10 total? • Or is there a way to abstract away the numbers and concentrate on the types of lines needed?
What is a Function? • A Discrete Piece of Code that Performs a Specific Operation or Task • Named with a Descriptive Identifier • Called with optional arguments from somewhere else in your program • When Called, Program Control (Execution) Is Transferred to the Function • Function Performs Required Tasks, and then Possibly Returns a Value (or values) • After Return from Function, Control Returns to the Statement Following the Function Call
Function Attributes • Function Name: Identifier Used to Call Function • Function Parameter(s) or Argument(s): Optional Value(s) Passed into Function for Use by Function Code • Function Return Value: Optional Value Returned by Function Back to Calling Function
Function Parameters (Arguments) • May Pass as Many Parameters as Necessary to Function • A Copy of the Value of the Parameter Is Passed to the Function • Changing the Value of the Parameter in the Function Does Not Affect the Value of the Original Variable • This Is Called Pass-by-Value
Function Return Value • No return value is required. Some functions just output something or perform some operations. • Most programming languages allow a function to return a value. Some allow returning of multiple values.
Library Functions • Programming languages provide various libraries of commonly used functions • API (Application Programming Interface) defines how to the programmer how to call the function, what parameters are required and assumptions are made. • Scratch/BYOB provides the following function libraries: - Motion - Looks - Sound - Pen - Sensing - Operator (math functions)