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

Function के Basics

एक function एक labeled recipe है जिसे आप एक बार लिखते हैं, ताकि जब भी वह काम फिर से करना हो आप इसे नाम से मांग सकें।
Syntax
go
func name(param type) returnType {
    // body
    return value
}

Declaring a Function

एक function func keyword, एक नाम, parentheses में एक parameter list (हर एक का एक नाम और type), और एक optional return type से declare होता है। body तब चलता है जब program में कहीं और function call किया जाता है।

उदाहरण: Declaring a Function

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

// Import the fmt package
import "fmt"

// Define the function square
func square(n int) int {
	// Send n * n back to the caller
	return n * n
}

// main is where the program starts running
func main() {
	// Print the values followed by a newline
	fmt.Println(square(6))
}

Multiple Parameters

Functions कई parameters ले सकते हैं, हर एक अपने नाम और type के साथ declared, या जब लगातार parameters एक जैसा type share करें तो दोहराव घटाने के लिए साथ group किए गए।

Note: एक जैसा type share करने वाले लगातार parameters आखिरी को छोड़कर बाकी सभी पर type छोड़ सकते हैं, जैसे 'func add(a, b int) int'।

उदाहरण: Multiple Parameters

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

// Import the fmt package
import "fmt"

// Define the function rectangleArea
func rectangleArea(width, height float64) float64 {
	// Send width * height back to the caller
	return width * height
}

// main is where the program starts running
func main() {
	// Print the values followed by a newline
	fmt.Println(rectangleArea(4.5, 3.0))
}

Functions with No Return Value

कोई function जिसे कोई result वापस नहीं देना उसमें return type बस छोड़ दिया जाता है, और इसके body के अंदर एक bare return (या function के अंत तक पहुंचना) इससे बाहर निकल जाता है।

उदाहरण: Functions with No Return Value

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

// Import the fmt package
import "fmt"

// Define the function logMessage
func logMessage(msg string) {
	// Print the values followed by a newline
	fmt.Println("[LOG]", msg)
}

// main is where the program starts running
func main() {
	logMessage("server started")
}

Every Path Must Return

अगर किसी function का signature एक return type declare करता है, Go compiler चाहता है कि function के हर संभव execution path का अंत एक return statement से हो -- बिना matching else के एक if जो भी return करता है वह compile नहीं होगा।

उदाहरण: Every Path Must Return

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

// Import the fmt package
import "fmt"

// Define the function sign
func sign(n int) string {
	// Check whether n > 0
	if n > 0 {
		// Send "positive" back to the caller
		return "positive"
	} else if n < 0 {
		// Send "negative" back to the caller
		return "negative"
	}
	return "zero"
}

// main is where the program starts running
func main() {
	// Print the values followed by a newline
	fmt.Println(sign(-5))
}
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. यह भूल जाना कि Go को हर नाम के बाद parameter type चाहिए, और यह कि लगातार एक जैसे-typed parameters एक type annotation share कर सकते हैं।
  2. किसी function को file में declared होने से पहले call करने की कोशिश करना -- Go में अनावश्यक, क्योंकि top-level declarations का order मायने नहीं रखता, कुछ scripting languages के विपरीत।
  3. function signature में return type declared होने पर हर code path पर value return न करना, जो एक compile error है।
चैप्टर सारांश
  • Functions 'func name(params) returnType { }' से declared होते हैं।
  • एक जैसे type के parameters एक single type annotation share कर सकते हैं।
  • declared return type वाले function में हर code path को एक value return करनी चाहिए।
  • किसी file में top-level function order मायने नहीं रखता -- Go चलने से पहले सभी declarations resolve कर लेता है।
🔒

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.