← Back to Go Course | Chapter 4: Functions | Lesson 3 of 7

नामित Return Values

Named returns किसी function को ऊपर अपने answer boxes पहले से label करने देते हैं, ताकि आखिर में एक साधारण return अपने आप उनके अंदर जो भी हो उसे वापस भेज दे।
Syntax
go
func name(param type) (result type, err error) {
    // body
    return
}

Declaring Named Returns

सिर्फ return types list करने के बजाय, कोई function signature में हर return value को एक नाम दे सकता है, जो इसके purpose को document भी करता है और इसे इसकी zero value से initialized एक उपयोग करने योग्य local variable के रूप में पहले से declare भी करता है।

उदाहरण: Declaring Named Returns

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// Define the function rectangleStats
func rectangleStats(w, h float64) (area, perimeter float64) {
	area = w * h
	perimeter = 2 * (w + h)
	return
}

// main is where the program starts running
func main() {
	// Short declaration: create a, p and infer the type
	a, p := rectangleStats(4, 3)
	// Print the values followed by a newline
	fmt.Println("area:", a, "perimeter:", p)
}

Bare return Statements

जब return values named हों, बिना arguments वाला एक साधारण return अपने आप उन named variables में जो भी वर्तमान में हो उसे return कर देता है, आपको variable names दोहराने से बचाते हुए।

Note: एक bare return छोटे functions को संक्षिप्त बना सकता है, लेकिन लंबे functions के लिए एक explicit 'return area, perimeter' अक्सर ज़्यादा साफ़ होता है।

उदाहरण: Bare return Statements

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// Define the function splitName
func splitName(full string) (first, last string) {
	first = "Go"
	last = "Pher"
	return
}

// main is where the program starts running
func main() {
	// Short declaration: create f, l and infer the type
	f, l := splitName("gopher")
	// Print the values followed by a newline
	fmt.Println(f, l)
}

Named Returns with error

Named returns value/error pattern के लिए खासतौर पर आम हैं, जहां error variable को err नाम देना function body को स्वाभाविक रूप से पढ़ने योग्य बनाता है जैसे आप इसे return करने से पहले conditionals के अंदर सेट करते हैं।

उदाहरण: Named Returns with error

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"errors"
	"fmt"
)

// Define the function validateAge
func validateAge(age int) (valid bool, err error) {
	// Check whether age < 0
	if age < 0 {
		err = errors.New("age cannot be negative")
		return
	}
	valid = true
	return
}

// main is where the program starts running
func main() {
	// Short declaration: create ok, err and infer the type
	ok, err := validateAge(-3)
	// Print the values followed by a newline
	fmt.Println(ok, err)
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. लंबे functions में named returns का ज़्यादा उपयोग करना, जिससे यह track करना मुश्किल हो जाता है कि हर named value असल में कहां सेट होती है।
  2. function body के अंदर := से किसी named return variable को shadow करना, एक अलग local variable बनाते हुए जिसे एक bare return नहीं उठाएगा।
  3. यह मान लेना कि multiple return values के लिए named returns ज़रूरी हैं -- वे बस एक optional documentation/convenience feature हैं।
चैप्टर सारांश
  • Named return values function signature में ही return parameters को नाम देते हैं।
  • बिना arguments वाला एक bare return named returns की current values वापस भेजता है।
  • Named returns function के अंदर pre-declared, zero-valued local variables की तरह काम करते हैं।
  • वे छोटे functions के लिए और हर return value का मतलब document करने के लिए सबसे उपयोगी हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.