Go Language - Functions And Related Concepts
A function known as method, sub-routine, or procedure is a collective statement that together perform a task and requires explicit returns. Go program have at least one main() function, we can divide code into separate functions, but logically, each function performs a specific task.
func function_name( [parameter list] ) [return_types] { body of the function }
A function declaration instruct compiler about a function name, return type, and parameters. A function definition provides the actual body of the function. The Go library provides numerous built-in. For example, the function len() takes arguments of various types and returns length of the type..
Pass the required parameters with its function name.
package main import "fmt" /* define a function to add two numbers*/ func addition(a int, b int) int { return a + b } func main() { /* Calling a function & passing twonumbers as parameters*/ result := addition (1, 2) fmt.Println("1+2 =", result) }
A Go function can return multiple values
package main import"fmt" func values() (int, int) { return 3, 7 } func main() { a, b := values () fmt.Println(a) fmt.Println(b) _, c := vals() fmt.Println(c) }
A function can be with or without arguments.
The formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit.
While calling a function, there are two ways that arguments can be passed to a function
Call by Value :
The actual value of an argument is copied into the formal parameter of the function. changes made to the parameter inside the function have no effect on the argument. By default, Golanguses call by value to pass arguments.
Call by Reference :
The address of an argument is passed into the formal parameter. Inside the function, the address is used to access theactual argument. This means that changes made to the parameter affect the argument.
Scope :
A scope is a region of the program where a defined variable can exist and beyond that the variable cannot be accessed. There are three places where variables can be declared in Go programming language −
Local Variables
package main import"fmt" func main(){ /* local variable */ var a,b,c int /* actual initialization */ a =20 b =20 c =a +b fmt.Printf("value of a = %d, b = %d and c = %d\n",a,b,c) }
Global variables are declared outside of a function.
Global variables hold their value throughout the lifetime of the program and can be accessed inside any functions defined inprogram.
package main import"fmt" /* global declaration */ var g int func main(){ /* declaring local variable */ var a,b int a =10 b =20 g =a +b fmt.Printf("value of a = %d, b = %d and g = %d\n",a,b,g) }
In the below example local and global variables have same name but the preference is given to local variable inside function.
package main import"fmt" /* global declaration */ var a int=30 func main(){ /* local variable declaration */ var a int=20 fmt.Printf("value of a = %d\n",a) }
Formal parameters are local variables with-in function and take preference over global variables.
package main import"fmt" /* global declaration */ var a int=30; func main(){ /* local variable declaration in main function */ var a int=10 var b int=20 var c int=0 fmt.Printf("value of a in main() = %d\n",a); c =sum(a,b); fmt.Printf("value of c in main() = %d\n",c); } /* function to add two integers */ func sum(a,b int)int{ fmt.Printf("value of a in sum() = %d\n",a); fmt.Printf("value of b in sum() = %d\n",b); returna +b; }
A defer statement defers the execution of a function until the surrounding function returns.
package main import "fmt" func main() { defer fmt.Println("world") fmt.Println("hello") }
All Rights Reserved. © 2024 BookOfNetwork