濮阳杆衣贸易有限公司

主頁 > 知識庫 > Go語言中函數(shù)的參數(shù)傳遞與調(diào)用的基本方法

Go語言中函數(shù)的參數(shù)傳遞與調(diào)用的基本方法

熱門標簽:?兓? 電銷機器人可以補救房產(chǎn)中介嗎 騰訊外呼系統(tǒng)價格 電梯外呼訪客系統(tǒng) 成都呼叫中心外呼系統(tǒng)平臺 百度地圖標注搜索關(guān)鍵詞 浙江人工智能外呼管理系統(tǒng) 最短的地圖標注 谷歌便利店地圖標注

按值傳遞函數(shù)參數(shù),是拷貝參數(shù)的實際值到函數(shù)的形式參數(shù)的方法調(diào)用。在這種情況下,參數(shù)在函數(shù)內(nèi)變化對參數(shù)不會有影響。

默認情況下,Go編程語言使用調(diào)用通過值的方法來傳遞參數(shù)。在一般情況下,這意味著,在函數(shù)內(nèi)碼不能改變用來調(diào)用所述函數(shù)的參數(shù)。考慮函數(shù)swap()的定義如下。

復(fù)制代碼 代碼如下:

/* 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;
}


現(xiàn)在,讓我們通過使實際值作為在以下示例調(diào)用函數(shù)swap():
復(fù)制代碼 代碼如下:

 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;
}


讓我們把上面的代碼放在一個C文件,編譯并執(zhí)行它,它會產(chǎn)生以下結(jié)果:

Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :100
After swap, value of b :200

這表明,參數(shù)值沒有被改變,雖然它們已經(jīng)在函數(shù)內(nèi)部改變。

通過傳遞函數(shù)參數(shù),即是拷貝參數(shù)的地址到形式參數(shù)的參考方法調(diào)用。在函數(shù)內(nèi)部,地址是訪問調(diào)用中使用的實際參數(shù)。這意味著,對參數(shù)的更改會影響傳遞的參數(shù)。

要通過引用傳遞的值,參數(shù)的指針被傳遞給函數(shù)就像任何其他的值。所以,相應(yīng)的,需要聲明函數(shù)的參數(shù)為指針類型如下面的函數(shù)swap(),它的交換兩個整型變量的值指向它的參數(shù)。

復(fù)制代碼 代碼如下:

/* 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 */
}

現(xiàn)在,讓我們調(diào)用函數(shù)swap()通過引用作為在下面的示例中傳遞數(shù)值:
復(fù)制代碼 代碼如下:

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 */
}


讓我們把上面的代碼放在一個C文件,編譯并執(zhí)行它,它會產(chǎn)生以下結(jié)果:

Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :200
After swap, value of b :100

這表明變化的功能以及不同于通過值調(diào)用的外部體現(xiàn)的改變不能反映函數(shù)之外。

您可能感興趣的文章:
  • Golang中的自定義函數(shù)詳解
  • 深入解析golang編程中函數(shù)的用法
  • Golang 如何實現(xiàn)函數(shù)的任意類型傳參

標簽:上海 宜昌 邢臺 七臺河 雅安 盤錦 紹興 眉山

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Go語言中函數(shù)的參數(shù)傳遞與調(diào)用的基本方法》,本文關(guān)鍵詞  語,言中,函數(shù),的,參數(shù),傳遞,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Go語言中函數(shù)的參數(shù)傳遞與調(diào)用的基本方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于Go語言中函數(shù)的參數(shù)傳遞與調(diào)用的基本方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    恭城| 湛江市| 石城县| 房山区| 育儿| 农安县| 海伦市| 旌德县| 津南区| 沅江市| 卢龙县| 汉阴县| 都昌县| 博乐市| 屏南县| 仁布县| 哈尔滨市| 龙胜| 即墨市| 宜兰市| 疏附县| 芜湖县| 乐清市| 阳山县| 钦州市| 那曲县| 屏东县| 阿图什市| 顺平县| 福建省| 红桥区| 通海县| 台州市| 文水县| 彭山县| 涟水县| 金川县| 靖宇县| 大丰市| 邯郸市| 英吉沙县|