HCI 4. Functions
Uploaded by adrianwang2003 · 28 May 2024
Preview
Hwa Chong Institution H2 Computing 1 4 Functions Learning Outcome Function is an abstraction mechanisms that hides details and allows us to view many things as just one thing. We can use functions to organize our codes more effectively. 4.1 Advantage of Functions Functions serve as abstraction mechanisms by eliminating redundant, or repetitious, code. Functions serve as abstraction mechanisms by hiding complicated details. For example, consider the previous sum function. The idea of summing a range of numbers is simple; the code for computing a summation is not. A function call expresses the idea of a process to the programm er without forcing him/her to wade through the complex code that realizes that idea Functions Support General Methods with Systematic Variations - An algorithm is a general method for solving a class of problems - The individual problems that make up a class of problems are kn own as problem instances - Algorithms should be general enough to provide a solution to many problem instances - A function should provide a general method with systematic variations Programming Elements and Contructs Use functions and procedures to modularise problem into chunks of code Understand the concept of recursion Trace the steps and list the results of recursive and non-recursive programs
Hwa Chong Institution H2 Computing 2 Functions Support the Division of Labor - In a well-organized system, each part does its own job in colla borating to achieve a common goal - In a computer program, functions can enforce a division of labor - Each function should perform a single coherent task - Each of the tasks required by a system can be assigned to a function, including the tasks of managing or coordinating the use of other functions 4.2 Problem Solving with Top-Down Design Top-down design starts with a global view of the entire problem and breaks the problem into smaller, more manageable subproblems, process known as problem decomposition As each subproblem is isolated, its solution is assigned to a function As functions are developed to solve subproblems, solution to overall problem is gradually filled out, process known as stepwise refinement 4.3 Define Simple Functions 4.3.1 The Syntax of Simple Function Definitions A function can be defined in a Python shell, but it is more convenient to define it in an IDLE window. Definition of a function consists of header and body. Syntax of a function definition: Docstring contains information about what the function does; to display, enter help(square) 4.3.2 Parameters and Arguments A parameter is the name used in the function definition for an argument that is passed to the function when it is called. Arguments provide the function’s caller with the means of transmitting information to the function. The number and positions of arguments of a function call usually match
Content continues in the PDF.
Related notes
- VJC Chapter 21 SQLite with PythonNotes/Practices · 2025
- VJC Chapter 23 Web Applications PrinciplesNotes/Practices · 2025
- VJC Chapter 10 RecursionNotes/Practices · 2025
- VJC Chapter 20 SQLNotes/Practices · 2025
- VJC Chapter 16 Hash TableNotes/Practices · 2025
- VJC Chapter 22 NoSQLNotes/Practices · 2025

