![]() The primary difference is that the function is called in an alert. The JavaScript code is very similar to the Python code. Once our program got to that line, it would have paused, ran our function, and then continued as if addingFunction(2, 4) was actually just the returned value 6. We call the function inside the print() function. The keyword return tells the function to return the following data, in this case, 2 + 4, or 6. We indicate that by separating our variable names with a comma. In this example, our function requires two variables instead of one. Python def addingFunction(a, b): return a + bprint(addingFunction(2, 4)) Our function will require two integers and will return the sum. Although you could write that inline, if you will use the same calculations many times, like converting imperial to metric, it might make more sense to write it as a function. This is particularly valuable when you want to alter data before you use it. The final function ability that we will cover is returning data. Learn about Object Oriented Design in four project-based courses. ![]() Related: 5 Functional Programming Languages You Should Know Functions That Return a Value Some common object-oriented programming (OOP) languages include: Java Python PHP C++ Ruby specialization Object Oriented Programming in Java Grow Your Portfolio as a Software Engineer. If you write a function in JavaScript that requires an integer, but the number is sent as a string, it can create a bug that is very hard to track down. This might seem a little annoying, but strict languages can often save you headaches. If you do not send the correct type of data, the function will create an error. ![]() When you create your function you have to specify what type of data your function requires. We know that your function wants string data, but that's not good enough for C++. Our C++ function required a bit more information. Hold on to that thought right now, we will explore functions will parameters later. Your keen eyes might have noticed that print() is also a function, but it is called differently than our function. In the example above, the function runs one line of code. White space matters in Python, so be sure to indent all the code that you want your function to run. The instructions in the function follow on the next line after the colon. The keyword def is used in Python to define and create a function. Python def helloFunction(): print("Hello World")helloFunction()
0 Comments
Leave a Reply. |