Examples
There are two ways to pass arguments to the function in Golang.
- Pass by Value or Call by Value.
- Pass By Reference or Call By Reference.
By default, Golang passes the arguments to the function via call by value.
Call By Value
Actual parameter values are copied to function formal parameters in this parameter passing, and the two types of parameters are kept in different memory locations. As a result, any changes made inside functions are not reflected in the caller's actual parameters.
Code
package main
import "fmt"
// function which changes the value
func change(X int) {
X = 100
}
// Main function
func main() {
var X int = 31
fmt.Printf("Before the Function Call, value of X is = %d", X)
// call by value
change(X
fmt.Printf("\n After the Function Call, value of X is = %d", X)
}
Output
Here, we can see the value of X before and after the function call is the same. It cannot be changed using the change function.
Call by Reference
We pass the variable's address rather than its values when invoking it. We will utilise the concept of pointers here. Because the actual and formal parameters both refer to the same locations, any type of changes performed within the function are reflected in the caller's actual parameters.
Code
package main
import "fmt"
// function which changes the value
func change(X *int) {
*X = 100
}
// Main function
func main() {
var X int = 31
fmt.Printf("Before the Function Call, value of X is = %d", X)
// call by Reference
change(&X)
fmt.Printf("\n After the Function Call, value of X is = %d", X)
}
Output
Features
Functions and methods in Go can return multiple values, which is a unique feature. This form can be used to fix a pair of problematic C programming idioms: in-band error returns like -1 for EOF and altering an argument given by the address.
A negative count indicates a write error in C, with the error code hidden in a volatile location. Write can return a count and an error in Go.
The Write method on files from package os has the following signature:
func (file *File) Write(b []byte) (n int, err error)
For returning multiple values directly after the function parameter parentheses, we must provide the return types of the values inside the parenthesis. For example;
package main
import "fmt"
func swap(a, b string) (string, string) {
return b, a
}
func main() {
x, y := swap("Ninjas", "Coding")
fmt.Println(x, y)
}
When the above code is compiled and run, the following result is obtained:
Anonymous Function
An anonymous function is a unique feature of the Go programming language. A function with no name is known as an anonymous function. It comes in handy when you need to write an inline function.
Code
// to create an anonymous function
package main
import "fmt"
func main() {
// Anonymous function
func(){
fmt.Println("Example of Anonymous Function.")
}()
}
Output
Parameter Without a Type
When a function's parameters all have the same type, the type can be omitted for some of them; that is, the type can only be stated once.
Code
package main
import "fmt"
func sum(a int, b int) int {
return a + b
}
func sub(a, b int) int {
return a - b
}
func main() {
fmt.Println(sum(6, 2))
fmt.Println(sub(6, 2))
}
Output
Frequently Asked Questions
Is Golang a functional or object-oriented language?
Go (or "Golang") is a post-OOP programming language that borrows the Algol/Pascal/Modula language family's structure (packages, types, and functions). Object-oriented paradigms, however, are still crucial in Go for designing a programme clearly and understandably.
Is Golang compiled or interpreted?
Golang is a compiler-based language that may be readily compiled on a development machine for any target operating System, including Linux and Mac. When a Golang project is built, it becomes a self-contained executable that can be run on the target machine without any other software.
Is Go lacking in classes?
Although Go does not have classes in the classic sense, it does have struct types, which are far more powerful than their C counterparts.
Why does Go lack inheritance?
Because Golang lacks classes, inheritance is accomplished using struct embedding. We can't directly extend structs; instead, we employ the composition idea, in which the struct is used to create other objects. So, in Golang, there is no concept of inheritance.
Conclusion
This article has discussed Function Arguments' definition, examples, and features in Go. We ran through how to pass arguments. We also discussed the Parameter without a name and the Parameter without a type. You can learn related concepts like Object-oriented paradigms, operating systems, classes, inheritance, etc., at the Coding Ninjas Studio blogs vertical.
We hope this article has helped you. You can check out this article on Exploring the dynamics of Golang. To understand the usefulness of Golang, read this article Why Golang is so famous among engineers.
Recommended Readings:
Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations, and much more!!
We wish you Good Luck! Keep coding and keep reading Ninja!!