Basic methods of parameter transfer and invocation of functions in go language _golang

Source: Internet
Author: User
Tags function definition printf

Passing a function argument by value is a method call that copies the actual value of the parameter to the form parameter of the function. In this case, the parameter changes within the function have no effect on the parameters.

By default, the Go programming language uses a method called a value to pass parameters. In general, this means that the code in the function cannot change the parameters used to invoke the function. Consider the definition of function swap () as follows.

Copy Code code as follows:

/* function definition to swap the values * *
Func Swap (int x, int y) int {
var temp int

temp = x/* Save the value of x * *
x = y/* put y into x * *
y = temp/* Put temp into y * *

return temp;
}


Now let's call the function swap () by making the actual value the following example:
Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {
/* local variable definition * *
var a int = 100
var b int = 200

Fmt. Printf ("Before swap, value of a:%d\n", a)
Fmt. Printf ("Before swap, value of B:%d\n", b)

/* Calling a function to swap the values * *
Swap (A, B)

Fmt. Printf ("After swap, value of a:%d\n", a)
Fmt. Printf ("After swap, value of B:%d\n", b)
}
Func swap (x, y int) int {
var temp int

temp = x/* Save the value of x * *
x = y/* put y into x * *
y = temp/* Put temp into y * *

return temp;
}


Let's put the above code in a C file, compile and execute it, and it will produce the following results:

Before swap, value of a:100
before swap, value of b:200 after
swap, value of a:100 after
swap, value of B: 200

This indicates that the parameter values have not been changed, although they have been changed within the function.

By passing function arguments, that is, a reference method call that is the address of the copy parameter to the formal parameter. Inside the function, the address is the actual parameter used in the access call. This means that changes to parameters affect the parameters that are passed.

The value to pass through a reference, the pointer to the parameter is passed to the function just like any other value. So, accordingly, it is necessary to declare the parameter of the function to be a pointer type like the following function swap (), and its exchange two integer variable's value to point to its parameter.

Copy Code code as follows:

/* function definition to swap the values * *
Func swap (x *int, y *int) {
var temp int
temp = *x/* Save the value at address x * *
*x = *y/* put y into x * *
*y = temp/* Put temp into y * *
}

Now, let's call function swap () by referencing the values in the following example:
Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {
/* local variable definition * *
var a int = 100
var b int= 200

Fmt. Printf ("Before swap, value of a:%d\n", a)
Fmt. Printf ("Before swap, value of B:%d\n", b)

/* Calling a function to swap the values.
* &a indicates pointer to a IE. Address of variable A and
* &B indicates pointer to B ie. Address of variable B.
*/
Swap (&a, &b)

Fmt. Printf ("After swap, value of a:%d\n", a)
Fmt. Printf ("After swap, value of B:%d\n", b)
}

Func swap (x *int, y *int) {
var temp int
temp = *x/* Save the value at address x * *
*x = *y/* put y into x * *
*y = temp/* Put temp into y * *
}


Let's put the above code in a C file, compile and execute it, and it will produce the following results:

Before swap, value of a:100
before swap, value of b:200 after
swap, value of a:200 after
swap, value of B: 100

This indicates that the function of the change and the difference from the external embodiment of the value invocation cannot reflect the function.

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.