From the course: Go for Python Developers
Function definition - Go Tutorial
From the course: Go for Python Developers
Function definition
- [Instructor] Let's take a look at the function definition. We are going to define a function, and complete a step, in the Collatz sequence. And, a step in the Collatz sequence says that if a number is odd, we multiply it by three and add one, otherwise we divide it by two. Here's the Python code. We have definition of Collatz step. And, the convention in Python is using lower case with underscores. We have a documentation string, which, if I'm hovering over the function, I'm going to see it. And, then, if "n" modulo 2, equals zero, return division by two, otherwise times three plus one. And, we can run without debugging and see Collatz step four and five. Let's have a look at the Go code. Let me close this one. So, Collatz step. In Go, the convention is to have camel case, and if you want to export a function, you start with a capital letter. Otherwise, it is starting with the lower case letter. Instead of a documentation string, we have a comment on top of the function that starts with the name of the function. And, again, if I hover over the function, you're going to see the documentation here. The code inside the function body looks the same. I have if "n" modulo two equal zero, return and divide by two. Otherwise, "n" times three plus one. Functions, arguments, and return values are mandatory in Go. In Python, we can do type annotation, right? We can do "n" of "int" and says that it returns an "int". But, this is annotation. It is not enforced by the Python interpreter. Here, it is enforced. This is static typing, so you must paste an integer, not a float, not any other type. Let's run this code, as well. So run, run without debugging. And, let's have a look at the debug console. And, we see again two and sixteen.