Go Function And Concepts

Go Language - Functions And Related Concepts

Functions :

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.

Syntax
func function_name( [parameter list] ) [return_types]
{
	body of the function
}
  • func − declaration of a function.
  • Function Name − actual nameof the function
  • Parameters − A parameter is like a placeholder. Parameters are optional that is, a function may contain no parameters.
  • Return Type − A function may return a list of values. The return types is the list of data types of the values the function returns. Some functions perform the desired operations without returning a value.
  • Function Body − contains a collection of statements.

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..

Calling a Function :

Pass the required parameters with its function name.

CODE/PROGRAM/EXAMPLE
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)
		}

Returning multiple values from Function :

A Go function can return multiple values

CODE/PROGRAM/EXAMPLE
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)
		}

Function Arguments :

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 − Inside a block or a function
  • global variables − Outside functions ()
  • formal parameters − Defining in function parameters.

Local Variables

  • Variables declared inside a block or function.
  • Used only by statements inside that function.
  • Local variables are not known to functions outside.
CODE/PROGRAM/EXAMPLE
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 :

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.

CODE/PROGRAM/EXAMPLE
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.

CODE/PROGRAM/EXAMPLE
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 :

Formal parameters are local variables with-in function and take preference over global variables.

CODE/PROGRAM/EXAMPLE
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;
		}

Defer :

A defer statement defers the execution of a function until the surrounding function returns.

CODE/PROGRAM/EXAMPLE
package main
import "fmt"
func main() {
	defer fmt.Println("world")
	fmt.Println("hello")
	}
#golang_functions #go_func #golang_init_function #golang_Calling_a_Function #golang_Returning_multiple_values_from_Function #golang_Function_Arguments #golang_variable_Scope #golang_Formal_Parameters

(New page will open, for Comment)

Not yet commented...