Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaScript: API’s, Parameters and Creating Functions with Parameters

Similar presentations


Presentation on theme: "JavaScript: API’s, Parameters and Creating Functions with Parameters"— Presentation transcript:

1 JavaScript: API’s, Parameters and Creating Functions with Parameters

2 Review: So far we have learned how to use basic JavaScript commands and we learned also how to develop new commands by creating functions (sequence of commands) from our primitive commands. Today we are going to learn more about all the JavaScript commands we have available to us and how we can learn about them.

3 Programming Commands:
So far in this unit we have been exploring programming by drawing turtle art with only a few commands. Many more commands exist and are included in most programming languages. In fact, most programming languages include hundreds if not thousands of commands. Do you think programmers memorize all of the commands in a programming language? If not, how is anyone ever able to use an entire programming language?

4 Documentation, APIs, and Libraries:
Programmers weren’t born knowing how a programming language works, and, like you and me, they don’t have perfect memories. Instead they rely on written documentation to help them learn new features of a language and recall how it works.  Every programmer has to learn by seeing examples of a language being used and reading documentation Professional programmers will frequently reference documentation while designing software. Becoming a good programmer is much less about memorizing a language and more about learning habits of mind and patterns that allow you to use a language (including its documentation) effectively! As mentioned earlier JavaScript has thousands of commands available to you but you have to learn how to access them.

5 Documentation and Libraries:
JavaScript commands /functions exist in JavaScript Libraries. A Library is a collection of commands/functions typically with a shared purpose. JqUERY ; Created to simplify client-side scripting of HTML NodeJS ; Created to help developers create a diverse variety of tools and applications. Documentation exist for all the libraries mentioned above for programmers to understand how to use there functionality. We’ve already looked at Libraries before when looking at Java Documentation online. The concept is the same with other programming languages.

6 Reading the JavaScript Documentation:
Today we are going to begin reading the documentation of a programming language. Before you ask me or a classmate for help today, I want you to read through the documentation, try the examples, talk with friends, and then talk to me. It may be slower going today, but in the long term it will make you much more confident programmers. Learn to use the resources available to you to solve your own problems. Transition to Code Studio, Unit 3 Stage 7 and complete the challenges. You will have to read through the documentation to learn about the new functionality available to you. Read the definition of a Parameter!!! (Argument )

7 Documentation Review:
We learned many new commands/functions available to us by reading the available documentation. There are many more libraries that exist for the JavaScript programming languages and documentation for each. It’s up to you to learn about it so you can use them to create cool applications! Every good programmer must learn as about the many different libraries and functionality they have available to them through reading documentation. It’s about knowing what you have and understanding how to use it

8 Functions with Parameters:
So we just learned many more functions available to us by using the JavaScript API. Programmers can’t remember all the functions/commands of every programming language so they refer to documentation online which describes the commands available and how to use them. Also we learned how to use parameters (arguments) when calling some of our functions/commands. When using commands/functions that take in parameters we can change the way a function behaves a bit. Example the function moveFoward() and moveFoward(300) Both functions move foward but the second functions is more specific and gives the programmer more control of the end result. Parameters allow us to make much more interesting images by specifying precisely how far the turtle should move or turn, and introduced the ability to choose specific pen sizes and colors.

9 Why use Parameters? Parameters are a powerful programming construct.
Suppose we have a whole group of similar problems, like rotating the turtle some amount. Without a parameter we would need 360 different functions, one for each number of degrees we wanted to rotate! Parameters allow us to use a single function as a general solution to a whole group of problems. This is clearly a useful construct to use in our programs, and in today’s lesson we’re going to learn how to create functions with parameters for ourselves. function rotate(degrees){ //Code that rotates a certain amount of degrees based on the parameter passed. } When creating functions with parameters it is important to have meaningful names for the parameter itself With the function turn(degrees) above. The name of the parameter is degrees because that represents how many degrees the turtle will turn. Give meaningful names.

10 Creating your own Functions with Parameters
Now we are going to practice using different parameters with functions we create and functions created for us. We are going to explore how by simply changing parameters in a function that we can produce some very interesting results. Go to Unit 3 Stage 8 and complete the challenges presented to you. The first 4 challenges help you practice using parameters and learning how to apply them properly within your code. Once you complete part 4 pause. Make sure you get this understand this one entirely. After we’ve discussed part 4, you will continue practicing using functions with parameters and complete the rest of the challenges. You will be changing up a scene and affecting how it’s drawn all by changing the values of certain parameters.

11 Why use Parameters? When do you need a function with a parameter?
Develop a rule for deciding when to create a function with a parameter rather than a normal function. Below your rule write a couple sentences justifying your rule.

12 Why use Parameters? Answered
Parameters allow the creation of a generalized solution to a whole class of problems, rather than individually solving many specific problems. Parameters remove the need to create repetitive functions, making code easier to write, read, and change. Whenever you have two functions that basically do the same thing make a function with a parameter instead, to remove the need to duplicate code. If you need a function to do the same thing a lot of different ways, then use a parameter so one function can do it right away.

13 Definitions: Parameter: accepts a value to be passed to a function, typically affecting the behavior of that function (e.g., changing the distance the moveForward() command moves the turtle) Library: a collection of commands / functions, typically with a shared purpose (e.g., a library of functions for manipulating the turtle) Documentation: a description of the behavior of a function, library, API, etc.


Download ppt "JavaScript: API’s, Parameters and Creating Functions with Parameters"

Similar presentations


Ads by Google