Function के Basics
In this page:
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
// 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))
}
Login to try C/C++/Java/PHP code in the editor
Multiple Parameters
Functions कई parameters ले सकते हैं, हर एक अपने नाम और type के साथ declared, या जब लगातार parameters एक जैसा type share करें तो दोहराव घटाने के लिए साथ group किए गए।
उदाहरण: Multiple Parameters
// 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))
}
Login to try C/C++/Java/PHP code in the editor
Functions with No Return Value
कोई function जिसे कोई result वापस नहीं देना उसमें return type बस छोड़ दिया जाता है, और इसके body के अंदर एक bare return (या function के अंत तक पहुंचना) इससे बाहर निकल जाता है।
उदाहरण: Functions with No Return Value
// 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")
}
Login to try C/C++/Java/PHP code in the editor
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
// 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))
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि Go को हर नाम के बाद parameter type चाहिए, और यह कि लगातार एक जैसे-typed parameters एक type annotation share कर सकते हैं।
- किसी function को file में declared होने से पहले call करने की कोशिश करना -- Go में अनावश्यक, क्योंकि top-level declarations का order मायने नहीं रखता, कुछ scripting languages के विपरीत।
- 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: