The Go Language – Exploring Functions – part 6 – 6.1

Go Lang - function declarations
Go Lang - function declarations

Hi All,

We are continuing Go language series.

In following previous articles we explored introduction to Go, wrote our first Go program, went through basic data types in Go, exploring strings in Go and exploring Constants

In this article we will discuss Functions in Go, since Function is broad topic we will multiple articles for Functions. Today is first article.

Take away from this article

  • What are functions in Go Lang
  • Various function declarations in Go
  • Function examples
  • Demo of – Call by value
  • Demo of – Call be address

What are Functions

  • Functions are set of statements executed together
  • Functions can be called multiple times from anywhere in the program
  • Repeated functionality can be wrapped into functions
  • Functions improve code readability
  • Evry Go program must have one function – main()

Function Declaration

  • Function declaration have
    • func” reserved keyword
    • function name
    • parameter list – name and type of parameters
    • result list –
      • type of the values which function returns
      • optional – in case function doesn’t return any value
    • function body – statements to be executed in function

func functionname (parameter-list) (result-list){
    statements to be executed / function body
}

Examples

  • Simple function without parameter and without result

Here we have function which don’t have any parameter and don’t have any return value

func simpleFunction() {

	fmt.Println("LIFE IS BEAUTIFUL")

} //simpleFunction

We can call above function as

//main function
func main() {
	fmt.Println("Calling functions")

	//simple function
	simpleFunction()
} //main

Output will be

Calling functions
LIFE IS BEAUTIFUL

  • Function with parameter but no result

Here our function have one parameter of type string

//function demo - simple function, with parameter and without return result
func funcwithParameterNoResult(quote string) {
	fmt.Println(quote)
}

We can call above function as

//main function
func main() {
	fmt.Println("Calling functions - funcwithParameterNoResult")

	//simple function
	//simpleFunction()

	//function with parameter but no return result
	funcwithParameterNoResult("LIFE IS BEAUTIFUL")
} //main

Output will be

Calling functions - funcwithParameterNoResult
LIFE IS BEAUTIFUL

  • Function with parameter and with result

Here, we are passing parameter of type string and return value string

//function demo - simple function, with parameter and with return result
func funcwithParameterwithResult(quote string) string {
	fmt.Println(quote)
	return quote
}

We are calling our function from main function

//main function
func main() {
	fmt.Println("Calling functions - funcwithParameterNoResult")

	//simple function
	//simpleFunction()

	//function with parameter but no return result
	//funcwithParameterNoResult("LIFE IS BEAUTIFUL")

	//function with parameter with return result
	fmt.Println(funcwithParameterwithResult("LIFE IS BEAUTIFUL"))
} //main

output will be

Calling functions - funcwithParameterNoResult
LIFE IS BEAUTIFUL
LIFE IS BEAUTIFUL

  • Function with multiple return values

In following function we are passing one string parameter – quote and returning two results – parameter and its length

//function demo - simple function, with parameter and with multiple return result
func funcwithParameterwithMultipleReturns(quote string) (string, int) {
	fmt.Println(quote)
	//returning two results - parameter - quote and number of characters
	return quote, len(quote)
}

Calling above function from main function

//main function
func main() {
	fmt.Println("Calling functions - funcwithParameterwithMultipleReturns")

	//simple function
	//simpleFunction()

	//function with parameter but no return result
	//funcwithParameterNoResult("LIFE IS BEAUTIFUL")

	//function with parameter with return result
	//fmt.Println(funcwithParameterwithResult("LIFE IS BEAUTIFUL"))

	//function with parameter with mulltiple return result
	fmt.Println(funcwithParameterwithMultipleReturns("LIFE IS BEAUTIFUL"))
} //main

and output will be

Calling functions - funcwithParameterwithMultipleReturns
LIFE IS BEAUTIFUL
LIFE IS BEAUTIFUL 17

Complete code

//main package
package main

//importing required packages
import (
	"fmt" // importing package for println
) //import

//main function
func main() {
	fmt.Println("Calling functions - funcwithParameterwithMultipleReturns")

	//simple function
	simpleFunction()

	//function with parameter but no return result
	funcwithParameterNoResult("LIFE IS BEAUTIFUL")

	//function with parameter with return result
	fmt.Println(funcwithParameterwithResult("LIFE IS BEAUTIFUL"))

	//function with parameter with mulltiple return result
	fmt.Println(funcwithParameterwithMultipleReturns("LIFE IS BEAUTIFUL"))
} //main

//function demo - simple function, with out parameter and without return result
func simpleFunction() {

	fmt.Println("LIFE IS BEAUTIFUL")

} //simpleFunction

//function demo - simple function, with parameter and without return result
func funcwithParameterNoResult(quote string) {
	fmt.Println(quote)
} //funcwithParameterNoResult

//function demo - simple function, with parameter and with return result
func funcwithParameterwithResult(quote string) string {
	fmt.Println(quote)
	return quote
} //funcwithParameterwithResult

//function demo - simple function, with parameter and with multiple return result
func funcwithParameterwithMultipleReturns(quote string) (string, int) {
	fmt.Println(quote)
	//returning two results - parameter - quote and number of characters
	return quote, len(quote)
} //funcwithParameterwithMultipleReturns

Go Lang - function declarations
fig : Go Lang – function declarations

Call by value

  • By default Go Lang uses call by value to pass arguments to functions
  • By this method, actual values of parameters are copied to function parameters
  • Change in function parameters does not change the value of actual parameters
  • Demo :
    • In below demonstration, we are writing one function – which updates value of parameter
    • We are printing the value of argument before calling function and after calling function – which prints same value but not the updated value in function

//main package
package main

//importing - required packages
import (
	"fmt" // package for println()
) //import

// main function
func main() {
	fmt.Println("Exploring functions in Go - demonstration of Call by value")

	var parameterValue int = 9

	//value of parameterValue before calling function - callByValue
	fmt.Println("value of parameterValue before calling function - callByValue", parameterValue)

	//calling function - callByValue with parameter value - 9
	callByValue(parameterValue)

	//value of parameterValue before calling function - callByValue
	fmt.Println("value of parameterValue after calling function - callByValue", parameterValue)

	//calling function - callByValue with parameter value - 9
	callByValue(parameterValue)

} //main

// function to update the parameter value
func callByValue(parameter int) {

	//updating the value of parameter / argument
	parameter = 10
} //callByValue

Exploring functions in Go - demonstration of Call by value
value of parameterValue before calling function - callByValue 9
value of parameterValue after calling function - callByValue 9 

Go Lang - demo of functions - passing parameter value as address of parameter
fig : Go Lang – demo of functions – passing parameter value as address of parameter

Go doesnt support Call by reference since it dont have reference variable

But we can use address of variable to pass as value

Demo:

  • We will pass address of parameter using “&” operator to pass as a value to function
  • Using pointer operator (*) we can update the value

//main package
package main

//importing - required packages
import (
	"fmt" // package for println()
) //import

// main function
func main() {

	fmt.Println("Exploring functions in Go - demonstration of Call by reference")

	var parameterValue int = 9

	//value of parameterValue before calling function - callByRefDemo
	fmt.Println("value of parameterValue before calling function - callByRefDemo", parameterValue)

	//calling function - callByRefDemo with parameter value - 9
	callByRefDemo(&parameterValue)

	//value of parameterValue after calling function - callByRefDemo
	fmt.Println("value of parameterValue after calling function - callByRefDemo", parameterValue)

} //main

// function to update the parameter value
func callByValue(parameter int) {

	//updating the value of parameter / argument
	parameter = 10
} //callByValue

// function to update the parameter value
func callByRefDemo(parameter *int) {
	*parameter = 18
} //callByRefDemo

Output will be

Exploring functions in Go - demonstration of Call by value
value of parameterValue before calling function - callByRefDemo 9
value of parameterValue after calling function - callByRefDemo 18

Go Lang - demo of functions - passing parameter value as address of parameter
fig : Go Lang – demo of functions – passing parameter value as address of parameter

Thanks for reading !!! Any discussion / suggestions / thoughts / questions are welcome!!!

HAVE A GREAT TIME AHEAD !!! LIFE IS BEAUTIFUL 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

1 Response

  1. May 29, 2022

    […] The Go Language – Exploring Functions – part 6 – 6.1 […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading