Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Return Values from Functions | Functions
course content

Course Content

Introduction to GoLang

Return Values from FunctionsReturn Values from Functions

Being able to pass data into functions is valuable, but it's equally advantageous to retrieve data from functions. This is where the return keyword becomes essential.

The return keyword allows functions to send data back to the point in the code where they were invoked. Here is the syntax for a function with a return statement:

The data_to_be_returned is where we specify an expression or a value. The returned_datatype represents the anticipated data type for the value that will be returned. This will become clearer through an example.

The subsequent program illustrates the implementation of a return statement via a function that computes and returns both the sum and product of two given integer arguments:

go

index.go

Please note that within the Println function, we have myFunc(5, 7), and the program above produces the output 47, which results from the calculations performed by the myFunc() function. This demonstrates that the return statement conveys specific data back to the location where the function was invoked. Additionally, we can store the returned data in a variable:

go

index.go

Note

A function doesn't require parameters to include a return statement.

A function cannot contain any code after a return statement, and typically, Go does not allow multiple return statements:

go

index.go

Nonetheless, it's possible to employ the return statement within conditional structures to selectively return values:

go

index.go

What will be the output of the following program?

Select the correct answer

Everything was clear?

Section 4. Chapter 4
course content

Course Content

Introduction to GoLang

Return Values from FunctionsReturn Values from Functions

Being able to pass data into functions is valuable, but it's equally advantageous to retrieve data from functions. This is where the return keyword becomes essential.

The return keyword allows functions to send data back to the point in the code where they were invoked. Here is the syntax for a function with a return statement:

The data_to_be_returned is where we specify an expression or a value. The returned_datatype represents the anticipated data type for the value that will be returned. This will become clearer through an example.

The subsequent program illustrates the implementation of a return statement via a function that computes and returns both the sum and product of two given integer arguments:

go

index.go

Please note that within the Println function, we have myFunc(5, 7), and the program above produces the output 47, which results from the calculations performed by the myFunc() function. This demonstrates that the return statement conveys specific data back to the location where the function was invoked. Additionally, we can store the returned data in a variable:

go

index.go

Note

A function doesn't require parameters to include a return statement.

A function cannot contain any code after a return statement, and typically, Go does not allow multiple return statements:

go

index.go

Nonetheless, it's possible to employ the return statement within conditional structures to selectively return values:

go

index.go

What will be the output of the following program?

Select the correct answer

Everything was clear?

Section 4. Chapter 4
some-alt