Updated April 10, 2023
Definition of Go Functions
There will be the main function; the main function is a mandatory function for any go program to get executed. Other than the main function, we can write many other custom functions (the name given by us); any function in go language start with func keyword; once go compiler reads the func, it understands that there is a function creation is going on and the function can contain the argument (parameters pass to function), body(function body where we will write the logic for the execution of the program), a function return type(it defined type of data we are going to return from the function).
How does Functions work in Go Language?
To explain the Go function’s working, let us first take one simple format of function and explain each part of the format.
func name_of_function(params)(return_type_of_function){
// Here we will write the logic(function body)
//
}
- func: Here, func is a keyword in Go language; once Go compiler reads about the func keyword, it understands that it is a function creation, and it will expect the name of the function.
- name_of_function: In this case, we can take any custom name of the function; generally, we name the function which is most suitable for the logic which we are writing inside the function body. For example, if we calculate the marks of the student inside the function, we can give the mark calculation name to the function. You can think of any name for custom function except for the name of the main function, which we can not change, and it will always be main.
- Params: params are the arguments which we can pass to the function; for example, if we are calculating total marks of the students inside the function, then we can pass the marks of all subject and name of the student for adding all the marks for getting the sum of total marks for the student which it gets in the arguments.
- return_type_of_function: Return type of function in Go language we need to mention, b defining return type of the function we are giving prior notice to the go compiler that we are going to return the particular data types. For example, we are calculating the total marks of any students; then we can expect numeric integer value as the return of the function so we can define the type of the function as the int.
- function body: Function body is the place where we are going to write the logic; this logic can be the calculation of the total marks of any students or any calculation
In the go language, we can not run any function or program without the main function; even any custom function also depends on the main function. We do not require to call the main function; the main is the section that gets executed before anything, and if we want to call any custom function, we can do it inside the main function itself.
Types of Functions in Go
We can define say there are two types of the function in the Go language main function and user-defined function. Main functions are the functions that are mandatory for any program for execution. In go language, we do not require to call the main function manually; the start of the execution of the program main function will execute first. The second type of function in the Go language is the user-defined function; any user-defined function gets called from the main function.
1. Main Function
The main function is the mandatory function for any program in the Go language; for example, if we want to calculate addition, we need to first write the main function, and from main, either we can write logic inside the main function, or we can define another function where we can write the logic ;
Example
We can explain the below example in the following steps,
- First, we have the main package of the Go language.
- Next, we have defined a function main, which is a system-defined function. We can not change this name.
- Inside the main function, we have written the body of the function with logic to calculate the sum of two numbers.
- Finally, we have printed the sum of the numbers.
Code:
package main
import "fmt"
func main() {
var a=10
var b=20
var sum =a+b
fmt.Println("The sum of the numbers is ", sum)
}
Output:
2. Function Defined by User
These are the functions which we define according to our requirements; for example, if we wanted to calculate the sum of the marks of the subject, then we can define a separate function, and inside that function, we can perform the addition, and the function will be called from the main function.
Example
We can explain the below example in the following ways,
- First, we have the main package of the Go language.
- Next, we have defined a function main, which is a system-defined function. We can not change this name.
- Inside the main function, we are calling another function to calculate the sum of the number.
- Inside the function total marks function, we have taken three arguments as the three subject marks.
- We have defined the argument types as the integer.
- And we have defined the return type of the function as the integer.
- Finally, we are doing the addition of the numbers and returning the sum of the marks to the main function, where the main function will print the output sum of the numbers.
Code:
package main
import "fmt"
func totalmarks(sub1,sub2,sub3 int) int {
return sub1+sub2+sub3
}
func main() {
var sub1=30
var sub2=20
var sub3 =30
var total_mark =totalmarks(sub1,sub2,sub3)
fmt.Println("The total marks obtained by student is ", total_mark)
}
Output:
Recommended Articles
This is a guide to Go Functions. Here we also discuss the introduction and how do functions work in the go language? along with examples and its code implementation. you may also have a look at the following articles to learn more –