Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function signature with no function body

Tags:

go

When viewing the source for the math.Ceil method, I found this syntax where there's an exported function signature with no body, and a non-exported version of the same signature that includes the implementation:

// Ceil returns the least integer value greater than or equal to x.
//
// Special cases are:
//  Ceil(±0) = ±0
//  Ceil(±Inf) = ±Inf
//  Ceil(NaN) = NaN
func Ceil(x float64) float64

func ceil(x float64) float64 {
    return -Floor(-x)
}

I assume this is some syntax which allows you to easily export a local function. Is that correct? And why would one do this instead of just having a single exported function and using it within the package?

like image 512
the system Avatar asked Jan 07 '13 03:01

the system


People also ask

What makes up a function signature?

Function Signature A function's signature includes the function's name and the number, order and type of its formal parameters. Two overloaded functions must not have the same signature. The return value is not part of a function's signature.

What is signature of a function example?

A function signature (or type signature, or method signature) defines input and output of functions or methods. A signature can include: parameters and their types. a return value and type.

What is a function signature in C++?

C++ Reference. Programming Terms. Function Signatures. A function signature consists of the function prototype. What it tells you is the general information about a function, its name, parameters, what scope it is in, and other miscellaneous information.

Does function signature include return type?

For functions that are specializations of function templates, the signature includes the return type. For functions that are not specializations, the return type is not part of the signature.


1 Answers

According to the Go language specification.

A function declaration may omit the body. Such a declaration provides the signature for a function implemented outside Go, such as an assembly routine.

In this case, the Ceil function is implemented by an architecture specific assembly file for 386 in floor_386.s. Both the amd64 and arm architectures each have an assembly file that implements Ceil() as well, but those assembly files are just glue to call the unexported ceil() function.

like image 76
Stephen Weinberg Avatar answered Oct 25 '22 11:10

Stephen Weinberg