Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Passing Arrays into Functions | Arrays and Slices
Introduction to GoLang

Passing Arrays into FunctionsPassing Arrays into Functions

A valuable feature is the ability to pass arrays into functions so that we can use these arrays within other scopes, especially when they are locally declared.

The general syntax for an array argument in a function is as follows:

Implementing this in a function would look something like this:

go

index.go

The following example demonstrates passing an array into a function and using it:

go

index.go

In Go (Golang), arrays are always passed by value into functions. This means that when we pass an array into a function, a local copy of the original array is created inside the function. Consequently, any modifications made to the array inside the function won't impact the original array because the data being accessed inside the function is a copy of the original array.

The following program illustrates that making changes to the array within the function does not affect the original array:

go

index.go

Note

In contrast to passing by value, there's the concept of passing by reference, where a reference to the array or variable is passed into the function. Consequently, modifying the value inside the function also modifies the source. In some programming languages, arrays are passed by reference by default.

What is the appropriate code for the parameter of the myFunc function:

Виберіть правильну відповідь

Все було зрозуміло?

Секція 5. Розділ 8
course content

Зміст курсу

Introduction to GoLang

Passing Arrays into FunctionsPassing Arrays into Functions

A valuable feature is the ability to pass arrays into functions so that we can use these arrays within other scopes, especially when they are locally declared.

The general syntax for an array argument in a function is as follows:

Implementing this in a function would look something like this:

go

index.go

The following example demonstrates passing an array into a function and using it:

go

index.go

In Go (Golang), arrays are always passed by value into functions. This means that when we pass an array into a function, a local copy of the original array is created inside the function. Consequently, any modifications made to the array inside the function won't impact the original array because the data being accessed inside the function is a copy of the original array.

The following program illustrates that making changes to the array within the function does not affect the original array:

go

index.go

Note

In contrast to passing by value, there's the concept of passing by reference, where a reference to the array or variable is passed into the function. Consequently, modifying the value inside the function also modifies the source. In some programming languages, arrays are passed by reference by default.

What is the appropriate code for the parameter of the myFunc function:

Виберіть правильну відповідь

Все було зрозуміло?

Секція 5. Розділ 8
some-alt