Presentation is loading. Please wait.

Presentation is loading. Please wait.

AP CSP: Creating Functions & Top-Down Design

Similar presentations


Presentation on theme: "AP CSP: Creating Functions & Top-Down Design"— Presentation transcript:

1 AP CSP: Creating Functions & Top-Down Design

2 Do Now: 1.

3 Do Now: 2.

4 Review: Last class we learned to create algorithms to find the smallest card in a set using a limited set of commands. Algorithms creation is an art the requires creativity and a good understanding of the commands available to a programmer. We also started programming in App Lab last class. You had to complete some tasks using only four JavaScript commands. Sequencing these commands together in a particular order allowed us to complete our objective. Today we are going to learn how to make more commands from the basic four commands given to us.

5 Review Discussion: In the previous lesson we created simple turtle drawings using only four commands. At some point you probably wished that more commands were available to you. Describe a command you wanted to be able to use and explain why you wanted to use it. Think of commands you wish you had to complete the last task! Programming languages will always have some commands that are already defined, but there will be many instances when the exact command we want isn’t available. Today we’re going to start exploring a powerful feature of most programming languages that will allow us to overcome this issue and create commands of our own.

6 Creating Function is App Lab:
We are going to transition back to App Lab and continue practicing your programming skills. You will still be limited to the four commands as before but now you are introduced to the concept of functions. A Function is simply a sequence of commands that will be performed when that function called upon. For the most part we will be dealing with programs that follow a certain style when being created Camelcase : Multi-word function names are made a single word which begins in lowercase and uses uppercase letters to indicate the start of a new word. For example: thisFunctionNameIsCamelcase() **Function Definition at the Bottom : Function definitions appear at the bottom of the program. This is done so that the first line of the program is the first line of code actually run (Opposite format also common) Function Names Can’t Start with Numbers: In most programming languages, including JavaScript, function names are not allowed to begin with numbers or most symbols. Underscore, however, is allowed.

7 Creating Functions in App Lab:
Go to Unit 3 Stage 5 and work on parts 2 – 8. You will be defining new functions from the four commands given to us which will allows us to use even more commands! Follow the prompts in the and complete all the tasks in Stage 5.

8 Function Benefits: List the benefits of being able to define and call functions in a program. Who specifically gets to enjoy those benefits? How is the use of a function an example of abstraction?

9 Function Benefits Answered:
List the benefits of being able to define and call functions in a program. Who specifically gets to enjoy those benefits? programs are easier to read and write functions remove the need to repeat large chunks of code functions break the program into logical chunks The person programming and any other human reading the program enjoys these benefits. Important to note: functions do not make the program easier (or harder) for the computer to run or understand – it must still run all of the same commands as before. How is the use of a function an example of abstraction? Abstraction is the practice of temporarily forgetting details unnecessary to addressing the problem at hand. Defining and calling functions is another example of how computer scientists use abstraction to solve problems. Once you have written a function that you know works as intended, you can call the function as often as you wish, without worrying about the details or problems you had to solve to get it working.

10 Efficiency of Functions:
Imagine that you have two programs that drew the diamond-shaped figure. One program uses functions as we did in the previous lesson. The other doesn’t use functions; it’s just a long sequence of the turtle’s primitive commands. Which program is more efficient? Make an argument for why one is more efficient than the other.

11 Efficiency and Functions:
“Efficient” can mean several things: It could mean the total number of primitive operations performed by the turtle. It could mean number of lines of code. It could hinge on the ability to reuse code within your own code. It could be about the speed and clarity with which you can write the program. Efficiency is an interesting thing to think about, but functions also introduce the ability to leverage the power of abstraction: when we write a function, it solves a small piece of a bigger problem. Having the small problem solved allows us to ignore its details and focus on bigger problems or tasks.

12 Top-Down Design: With the creation of functions we are able to abstract away certain details. To create the function right() we needed to make a left 3 times. After we create that function we no longer need know how the function right() works, you just know it works. Abstraction caused by functions allows us to focus on task at hand (creating a diamond) and not worry about small problems anymore (turning right). Creating Functions for small problems and then reusing those functions allow us to focus on the real problem without having to worry about resolving our small problem over and over again. Sometimes we must look at the larger goal at hand and break that down into smaller sub-goals to complete our task. This methodology is called Top-Down design. You are going to practice Top-Down Design Now

13 Create a Snowflake using Top-Down Design
In our last lesson we were able to create a diamond by solving smaller subtasks until we created one function able to create a diamond just by calling it. We could have actually looked at the main problem itself and break down that goal into smaller and smaller goals that I know how to program. You are going down to use Top-Down Design try and create a snowflake pattern. Break down the process of creating a snowflake into smaller subtasks. Look for repeating patterns in your snowflake and work down from there until you reach a point where you know how to code what you need. Work with a partner to figure out an approach to solve this problem using Top-Down design. Try and come up with  descriptive function names that give insight to your thinking.

14 Creating a Snowflake in Code Studio:
When you are finished out how to breaking down your snowflake problem, transition to Unit 3 Stage 5 and create the code that will produce a snowflake. Remember to design functions that reflect your Top-Down design! Create functions for each of your sub-goals. Your final code should only run one command and that is drawSnowflake() You will then try out Top-Down Design with creating 3x3 grid Try to break down in to smaller goals with their own functions Your code should only run one command called drawGrid(); Break down the problem how you see fit. Try to look for patterns and then try to break down those patterns into something you know you can code.

15 Top-Down Design, Functions, and Abstraction
When we layer functions - with functions that call other functions - we are creating layers of abstraction. In programming, writing functions helps us create layers of abstraction that allow us to design and create increasingly complex systems. We’ve seen layers of abstraction before in the design of Internet protocols, or in the binary encoding of information. Solving a fundamental piece of a problem that can be reliably reused in a different context frees us to think about more complex problems because we can trust the piece that has been solved and don’t have to worry about its details. Solving small problems - like how to send a single bit from one place to another - allows us to think about bigger problems, like sending numbers, or text, or images, to multiple people, over networks, in packets…etc., etc., etc.

16 Definitions: Abstraction - Pulling out specific differences to make one solution work for multiple problems. Function - A piece of code that you can easily call over and over again.


Download ppt "AP CSP: Creating Functions & Top-Down Design"

Similar presentations


Ads by Google