GoLang

Explain Functions in Go Programming Language | Go Programming Tutorial

Explain Functions in Go Programming Language | Go Programming Tutorial

Explain Functions in Go Programming Language | Go Programming Tutorial

In this article, you will learn:

  • What are the Functions of the Go Programming Language?
  • What is a Function Declaration?
  • What is Function Calling?
  • What are Function Arguments?

What are the Functions of the Go Programming Language?

Functions are generally the block of statements present in a program that can be reused. It saves the excessive use of memory, acts as a time saver, and more importantly, it structures the code well and provides better readability of the code. In simple words, a function is a set of statements that executes some specific task and returns the result. It can also execute some particular task without returning anything. 

What is a Function Declaration?

Function declaration means a way to declare a function. 

SYNTAX:

func name_of_the_function(list-of-Parameters) (Return_type){

   // body of the func

}

The declaration of the function contains:

  • func: It is a keyword that is used in declaring a function in Go Programming Language.
  • Function_name: It writes the name of the function. 
  • Parameter-list: It contains the name along with the type of the function parameters. 
  • Return_type: It is optional and it contains the return type of the function. 

What is Function calling?

A function call happens when the user wants to execute the function. The function is called to use its functionality. 

What are Function Arguments?

The arguments passed to a function are called actual arguments. The arguments received by a function are called formal arguments.

Point To Be Noted: By default, Go language uses call by value method only in the functions. 

Go programming supports two ways to pass arguments to the function:

  • Call by value: In this parameter, the values of actual arguments are copied to the function’s formal parameters and the two types of arguments are stored in different memory locations. So any changes that happen inside functions are not reflected in the values of the actual parameters.
  • Call by reference: It passes the address of the actual parameter to the formal parameter. So, whatever changes happen inside the function will reflect on the value of the actual parameters.
Yamini  Sharma

I am working as a professional engineer in "HIghRadius Technologies Pvt. Ltd." Passionate about learning and exploring new technologies. Enthusiast in writing and sharing what I have in my knowledge bucket.