From the course: Programming Foundations: Fundamentals

Functions across languages - Python Tutorial

From the course: Programming Foundations: Fundamentals

Functions across languages

- We've seen functions that fall into four main categories. Those that have no parameters and no return value, no parameters but do return a value, those that have parameters and don't return a value, and finally, those that have both parameters and a return value. But so far, we've only seen these functions in Python. Let's take a look at how a few other languages handle functions. First up is Java. This is the no parameters and no return value example of a function. Does it feel slightly familiar? Yes, the curly braces are used to mark off the function's body, just like how we saw curly braces used for the code block of an if statement. Also, we have the function named followed by empty parentheses, just like we do in Python, but what's totally different here? It's the void keyword in front of our function name. Can you guess what that void means? It means that our function doesn't return a value, similar to how we can't get any money back from a voided check. It has no value. Unlike Python, Java requires that you always specify explicitly what the return type is of a function, even when that function isn't going to return a value. Okay, onto the next example, Kotlin. This is the has parameters and no return value example of a function. Let's look for similarities and differences from what we've seen so far. Just like Java, we use curly braces to mark off the function's body, and similar to Python, we have a keyword, fun, which is used to signal that we've creating a function. However, unlike Python, we have to specify what the type of our parameter is when we define the function. We do that by placing a colon after the parameter's name, and then the type. In this case, it's a string. As you explore now programming languages, I encourage you to compare languages, as it makes it easier to learn. Do you feel like you can spot a function now? Let's put your skills to the test in a rapid-fire round of Find That Function. Where's the function here? Yep, you got it. This is the Ruby programming language, and this is how functions are defined. Okay, onto the next one. Can you spot the function? Yes, that's right. This is the C++ programming language, and this is the syntax it uses for functions. One more. Can you find the function? Nice work. This is some JavaScript code, and indeed, this is how you define functions in JavaScript. Okay, thanks for playing along. Even if you didn't understand exactly how each function worked, you were able to spot them easily in code. That's half the battle. You're now an expert function spotter.

Contents