programming
syntax
return statement
coding
software development

Odd return syntax statement

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

"Odd return syntax" refers to return statements that look unusual but are valid code. Common examples include the comma operator in C/C++ (return a, b;), tuple returns in Python (return x, y), conditional returns (return x if cond else y), and the void return cast (return (void)expr;). These patterns exploit language-specific rules about how expressions are evaluated in return statements and can be confusing when encountered for the first time.

The Comma Operator in C/C++

The most commonly asked-about "odd return" involves the comma operator:

c
int foo() {
    return 1, 2, 3;  // Returns 3, not a tuple
}

The comma operator evaluates each expression left to right and returns the value of the last expression. So return 1, 2, 3; evaluates 1 (discards), evaluates 2 (discards), evaluates 3 (returns). This is equivalent to return 3;.

c
1int bar() {
2    int x = 10;
3    return x++, x;  // x++ increments x to 11, then returns 11
4}
5
6int baz() {
7    return printf("side effect\n"), 42;
8    // Prints "side effect", then returns 42
9}

The comma operator is sometimes used intentionally for side effects before returning.

Python Tuple Returns

In Python, return a, b creates and returns a tuple:

python
1def divide(a, b):
2    return a // b, a % b  # Returns a tuple (quotient, remainder)
3
4q, r = divide(17, 5)
5print(q, r)  # 3 2

This is not the comma operator — Python does not have one. The comma creates a tuple literal. These are equivalent:

python
1return x, y       # Implicit tuple
2return (x, y)     # Explicit tuple — same result
3
4# Unpacking at the call site
5quotient, remainder = divide(17, 5)

Conditional (Ternary) Return

python
# Python
def abs_val(x):
    return x if x >= 0 else -x
javascript
1// JavaScript
2function absVal(x) {
3    return x >= 0 ? x : -x;
4}
c
1// C/C++
2int abs_val(int x) {
3    return x >= 0 ? x : -x;
4}

The ternary operator inside a return evaluates the condition and returns one of two values.

Short-Circuit Return

python
# Python — returns first truthy value or last value
def get_name(user):
    return user.get("name") or user.get("username") or "Anonymous"
javascript
1// JavaScript — same pattern
2function getName(user) {
3    return user.name || user.username || "Anonymous";
4}
5
6// Nullish coalescing (null/undefined only, not falsy)
7function getCount(options) {
8    return options.count ?? 10;
9}

or/|| does not return true/false — it returns the actual operand value. return a or b returns a if truthy, otherwise b.

Return with Assignment

python
# Python 3.8+ walrus operator
def find_match(items):
    return match if (match := next((x for x in items if x > 10), None)) else -1
go
1// Go — return with named return values
2func divide(a, b float64) (result float64, err error) {
3    if b == 0 {
4        err = errors.New("division by zero")
5        return  // Returns current values of 'result' and 'err'
6    }
7    result = a / b
8    return  // Named "naked" return
9}

Go's naked return uses the named return variable values. This is valid but discouraged in long functions because it is unclear what values are being returned.

JavaScript Arrow Function Implicit Return

javascript
1// These are equivalent
2const add = (a, b) => { return a + b; };
3const add = (a, b) => a + b;  // Implicit return
4
5// Returning an object literal requires parentheses
6const makeUser = (name) => ({ name: name, active: true });
7// Without parens, { } is interpreted as a block, not an object

C++ Structured Bindings Return

cpp
1#include <tuple>
2
3// Return multiple values
4std::tuple<int, double, std::string> getData() {
5    return {42, 3.14, "hello"};  // Brace initialization
6}
7
8// At call site (C++17)
9auto [num, pi, msg] = getData();

Rust Implicit Return

rust
1// Rust: last expression without semicolon is the return value
2fn add(a: i32, b: i32) -> i32 {
3    a + b  // No semicolon = return value
4}
5
6// Adding a semicolon changes the return type to ()
7fn add_broken(a: i32, b: i32) -> i32 {
8    a + b;  // Compile error: expected i32, found ()
9}

Common Pitfalls

  • C/C++ comma operator confusion: return a, b; returns b, not a pair. To return multiple values in C++, use std::tuple or std::pair. The comma operator silently discards earlier values.
  • Python trailing comma creates tuple: return x, (with trailing comma) returns a one-element tuple (x,), not x. This is a subtle bug when the comma is accidental.
  • Go naked returns in long functions: Named return values with naked return are hard to follow when the function body is longer than a few lines. Prefer explicit return result, err.
  • JavaScript arrow function object literal: () => { key: value } is parsed as a labeled statement inside a block, not an object. Wrap in parentheses: () => ({ key: value }).
  • Rust semicolon changes return type: Adding or removing a semicolon on the last expression changes whether it is a return value or a statement. This is a common source of type mismatch errors.

Summary

  • The C/C++ comma operator in return a, b; evaluates both but only returns b
  • Python's return x, y creates a tuple — not a comma operator
  • Ternary and short-circuit operators inside returns are valid and widely used
  • Go supports naked returns with named return values (use sparingly)
  • Rust uses the last expression without a semicolon as the implicit return value
  • JavaScript arrow functions have implicit returns without braces, but object literals need parentheses

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.