Functions in Go Language

Learn via video courses
Topics Covered

Overview

A function is simply a group of programming statements, When executed together they perform tasks in a step-by-step manner. functions also eliminate repetition of code and make the code reusable making them clean and ordered.

Introduction

Functions are a group of programming statements. Functions provide us with the concept of reusability which promotes code reusability, Reducing the usage of memory and development time.   The function allows you to extract a commonly used block of code into a single component.

In Golang, Every code execution starts with the main() function which acts as an entry point for the execution flow.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Syntax

Syntax

Function Declaration

The declaration of the function contains

  • func: It is a keyword in the language, which is used to create a Golang function.
  • function_name: It is the name of the function.
  • Parameter-list: It contains the name and the type of the function inputs.
  • Return_type: Returns types are optional in Golang. It contains details about what will a function return if we add a return type to our function, It is mandatory to pass a return value in our function.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Function Calling

Golang Function can be called in a below way.

Example

We will create an example of simple addition. A function add is created with the func keyword before it. the func keyword tells Go that this piece of code is a function. Later we invoked the function in the main() using its name.

Output:

Try on Playground.

Function Arguments

Call by Value

When we pass values of variables to a function as parameters, those functions are known as Call By Value. Changes made to the function variable won't update the original variables as we pass copies of those.

Output:

Try on Playground

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Call by Reference

While Calling a function, If we pass the address of a variable instead of a value, Those functions are known as Call by Reference. Changes made to function variables update the original variables as we are passing the addresses of those.

Output:

Try on Playground

More Examples

Let's see more examples of the Golang function.

Golang Functions Returning Multiple Values

Output:

Try on Playground.

The Return Values of a Function can be Named in Golang

Output:

Try on Playground.

Golang Passing Address to a Function

Output:

Try on Playground.

Anonymous Functions in Golang

Output:

Try on Playground.

Conclusion

  • Understood the concept of functions and studied the syntax and declaration of functions.
  • Created a vast number of function examples.
  • understood the concept of call by value and call by reference with examples.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more