Go Language Core Beauty 4.1-function declaration

Source: Internet
Author: User
Tags hypot
This is a creation in Article, where the information may have evolved or changed.

Functions have several advantages: you can package a series of statements into a program unit, you can break down the big work into small tasks that allow different programmers to do it independently at different times and in different places; a function can hide implementation details from the user. These advantages allow the function to become one of the most important parts of the program.

Before this chapter, we've seen a lot of functions, and it's time to thoroughly discuss the features of the functions. The example used in this chapter is a web crawler, which is also the component that crawls web search engines, and they will continue to crawl the page that the link refers to, based on the links in the Crawled Web page, which allows us to learn many other features of recursive functions, anonymous functions, error handling, and functions.

A function declaration contains a function name, an omitted argument list, a list of return values, and a function body:

func name(parameter-list) (result-list) { body}

The parameter list describes the parameter names and parameter types, which are local variables of the function scope, and the values are passed in by the caller. The return value list describes the name and type of the function return value, and if the function returns only an unnamed variable or simply does not have a return value, the parentheses of the return value list can be omitted. If a function declaration does not contain a list of return values, no value is returned after the function body finishes executing:

funcHypot (x, yfloat64)float64{returnMath. SQRT (x*x + y*y)}fmt. Println (Hypot (3,4))//"5"

X and y are parameter names, 3 and 4 are the parameter values passed in when called, and the function returns a value of type float64.

The return value can also be named as a parameter, in which case the return value is declared as a local variable of the function scope and is initialized to the 0 value of the corresponding type.

If a function has a list of return values at the time of declaration, that function must be executed to the return statement, unless the function cannot execute to the end: for example, a panic or an infinite loop with no break.

As with the Hypot function, if more than one parameter or return value has the same type, we can declare it together, and the following two declarations are equivalent:

funcF (i, J, Kint, S, tstring)                 {/* ... */}funcF (iintJintKint, SstringTstring) {/* ... */}

The following 4 functions, each with 2 int parameters and an int return value, are declared in 4 different ways. Note the use of the underscore (blank operator) here:

funcAdd (xintYint)int{returnx + y}funcSub (x, yint) (Zint) {z = x-y;return}funcFirst (xint, _int)int{returnX}funcZeroint,int)int{return 0}fmt. Printf ("%t\n", add)//"func (int, int) int"Fmt. Printf ("%t\n", sub)//"func (int, int) int"Fmt. Printf ("%t\n", first)//"func (int, int) int"Fmt. Printf ("%t\n", zero)//"func (int, int) int"

The function type is the function signature, and if the argument list of the two function and the variable type of the return value list correspond one to the other, then the two functions have the same signature. The variable name of the parameter and return value and whether it is declared centrally does not affect the function signature.

The arguments for a function call must be in the order declared by the parameter. The go language is not a default parameter value, and there is no way to specify the value of the passed parameter through the parameter name, so the variable name of the parameter and return value is meaningless for the function caller in addition to the document.

In the function body, the parameter is a local variable that is initialized to the value passed by the caller. function parameters and named return values are the outermost local variables of the function, and their lexical blocks are the entire function.

Parameters are passed by value, so passing past is a copy of the original variable, and the modification of the parameter does not affect the original variable. However, if the parameter is a reference type, such as a pointer, slice, map, function, channel, and so on, the modification of the parameter may affect the original variable.

you might see a function declaration in the standard library that does not have a function body, which means that the function is not implemented with go:

package mathfunc Sin(x float64) float //该函数用汇编实现

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.