Just An Application

June 11, 2013

Programming With Rust — Part Four: Things We Need To Know – Function Definitions Revisited: Items, Expressions And Statements

1.0 Items

Technically a function definition is a function-item (fn_item).

Similarly a module definition is a module item (mod_item).

A name binding is one kind of view item (view_item).

An enum type definition is an enum item (enum_item).

What all these things have in common, and what makes them items, is that it is possible to process them at compile time and if necessary store the results in a program’s read-only memory.

2.0 Function Bodies: Expressions And Statements

The body of a function is a block.

A block is a sequence of statements followed by an optional expression, delimited by braces (‘{‘ … ‘}’). [1]

An expression is something which when evaluated at run-time produces a value.

A statement is something which when evaluated at run-time does not produce a value.

Terminating an expression with a semi-colon causes the value of the expression to be ignored, that is, it becomes a statement, specifically an expression-statement. [2]

A block is itself an expression. If it ends with an expression then its value is the value of that expression. If it ends with a statement then its value is the singleton instance of the unit type, that is, nothing. [3]

2.0 Function Return Types

If a function does not return a value then the return type can be omitted from the definition.

    fn nonplussed(x: uint, y: uint)

Alternatively the return type can be explicitly declared as the unit type.

    fn nonplussed(x: uint, y: uint) -> ()

Or to put it another way in Rust a function always returns something even if that something is nothing.

3.0 Function Return Values

Because the body of a function is a block, and because a block is an expression, then, by default, the return value of a function is the result of
evaluating the block which comprises the body of the function.

For example,

    fn plus(x: uint, y: uint) -> uint
    {
        x + y
    }

Note the absence of a semi-colon (‘;’). If you inadvertently add one then the expression becomes an expression-statement and the function
will no longer compile as there is no value to return.

Note that the converse is also true. You cannot return a value from a function which is declared not to return a value

For example this will not compile

    fn nonplussed(x: uint, y: uint) 
    { 
        x + y
    }

whereas this will

    fn nonplussed(x: uint, y: uint)
    { 
        x + y;
    }

4.0 Explicitly Returning A Value From A Function

It is also possible to return a value explicitly using a return expression.

For example

    fn plus(x: uint, y: uint) -> uint
    {
        return x + y
    }

In this case the return is superfluous but there are situations in which it can be useful.

Notes

[1] This is my definition of a block as currently there does not appear to be a formal definition in the Rust documentation.

[2] Empirically this does not seem to be true for all possible expressions.

[3] This is my interpretation of the semantics of a block. The Rust documentation does specify that a block is an expression but does not explicitly specify its semantics.


Copyright (c) 2013 By Simon Lewis. All Rights Reserved.

Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and owner Simon Lewis is strictly prohibited.

Excerpts and links may be used, provided that full and clear credit is given to Simon Lewis and justanapplication.wordpress.com with appropriate and specific direction to the original content.

Advertisements

2 Comments »

  1. […] program’s static memory holds the values of items processed at compile […]

    Pingback by Programming With Rust — Part Five: Things We Need To Know – Tasks And Memory | Just An Application — June 13, 2013 @ 9:59 am

  2. […] other items, static_items are processed at compile time and the values they define are stored in the […]

    Pingback by Programming With Rust — Part Nine: Let’s Build A Program | Just An Application — June 14, 2013 @ 2:16 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: