What functions look like in various programming languages

Mav Tipi
5 min readOct 28, 2020

--

This post is a reference sheet for syntax in different languages. Ever look at a piece of foreign code and try to figure out what it might be? Ever switch projects and suddenly get confused about what symbols you’re supposed to put to paper? Here we’re going to look at the function syntax for some of the big names: C++, Ruby, Python, Go, Java and JavaScript.

If you spot an error, or something you want to clarify, please let me know below!

Context

Some of these languages are descended from each other:

Python’s logo hasn’t been green in fifteen years, actually

Don’t read too much into this diagram I’ve made; small and large influences, and syntactic and functional influences are all represented the same here. It’s just some background, to give a sense of the timeline and who’s iterating on who.

Functions are called the same way in all of these languages:

myfunction(comma,separated,arguments)

but defined with different syntax, which is what we’ll be looking at. Other languages not covered here may call functions differently; for example, Logo, MATLAB and others do

myfunction whitespace separated arguments

with no brackets of any kind. However, the way our six do it is certainly the most common way.

C++, Java and Go programs have a “main” function which is what is executed; all other functions need to be called inside of main to work. Python, Ruby and JavaScript don’t have such a feature. Here’s what main looks like in C++:

int main ()
{
myfunction();
whatever else;
}

Verbiage

Different languages and their communities may use the word “function”, “method”, or others, or use multiple and make distinctions between them. I personally don’t think these distinctions or the arguments around them are relevant in 2020, if they ever were. But I’m not much of a computer scientist.

One more thing I want to note before we begin: We can’t hit every difference here. For example, which characters can’t or shouldn’t be used in function names in each language? Nor can we hit every variation of how you can write something, like including default values for arguments. Unfortunately, this article isn’t enough to make you fluent in six programming languages.

Okay, let’s go.

Function syntax in C++

type myfunction (type comma, type separated, type parameters)
{
code goes in here;
}

Each of those types I typed up there needs to be replaced with a data type: for the function, the data type it returns, and for the arguments, the data type it is. So an example with four different data types:

bool myfunction (int comma, double separated, char parameters)
{
code goes in here, outputs a boolean;
}

In C++, every statement must end in a semicolon.

Function syntax in Java

Java function syntax can be identical to C++ syntax. However, you will commonly see two additions at the front:

accessibility scope type myfunction (type comma, type separated, type parameters)
{
code goes in here;
}

Like C++, everything needs a data type, semicolons are mandatory, the syntax is fully the same.

“Accessibility” tells you where the method can be accessed from, while “scope” defines whether it’s a class function or an object function. The two can be written in either order, or omitted for default behavior. A filled-out example:

static public boolean myfunction (int comma, double separated, char parameters)
{
code goes in here, outputs a boolean;
}

Another difference, not pictured here, is that all methods in Java are written inside a class; you run the class (which runs the class’s main function).

Function syntax in Python

def myfunction(comma, separated, parameters):
code goes here

Python functions are defined with def, the function name, parentheses for comma-separated arguments, and a colon. What follows is the code, indented.

In Python, whitespace is significant: functions aren’t ended with a word or symbol or anything like that, but rather by ceasing to indent.

def myfunction(comma, separated, parameters):
code goes here
then code goes here to show that my_function is over

If your function is a one-liner, you can just write it on one line instead of bothering with indentation:

def myfunction(comma, separated, parameters): some short code

Python does not need semicolons to separate statements, though it can use them in certain cases.

Function syntax in Ruby

def myfunction(comma, seperated, parameters)
code goes here
end

Clean. Ruby uses “def” like Python, doesn’t need brackets, a colon, semicolons or a strict whitespace scheme, and ends explicitly with the word “end”.

Function syntax in Go

The basic way to write a function in Go is the following:

func myfunction(comma type, separated type, parameters type) type {
code goes here
}

Go retains more of C’s syntax than Python or Ruby, though it shuffles things around and adds some func.

You can also create unnamed functions as variables:

myvariable := func(comma, separated, parameters) {
code goes here
}

If you want to call the function immediately and never again, you don’t even need to assign it to a variable:

func(comma, separated, parameters) {
code goes here
}(comma, separated, arguments)

The parentheses at the end call the function right as you define it.

Function syntax in JavaScript

JavaScript has a few common ways to write functions.

function myfunction(comma, separated, parameters) {
code goes here
}

Just like Go but with the data types ripped out. Also like Go, JS can do unnamed functions assigned to variables:

var myvariable = function (comma, separated, parameters) {code goes here}

and immediately invoke an unnamed function:

(function (comma, separated,parameters) {
code goes here
})(comma, separated, arguments)

It needs an extra set of parentheses over Go to make it work in the second case.

There is also a newer function syntax in JavaScript, the arrow function:

(comma, separated, parameters) => {
code goes here
}

We’ve replaced the word “function” with a shorter, unique, but still highly grokkable syntax. When it’s a single-line function, it doesn’t even need brackets:

(comma, separated, parameters) => code goes here

And if it only takes a single argument, it doesn’t even need parentheses:

comma => code goes here

Looks great. However arrow functions do not have exactly all the same functionality of regular functions, so you will still see the classic JS function style as well.

That’s all — again, please chime in if you’ve spotted any errors!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Mav Tipi
Mav Tipi

Written by Mav Tipi

Weakly Pseudonymous Software Engineer

No responses yet

Write a response