Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Types in Go
  • Go Keywords
  • Go Control Flow
  • Go Functions
  • GoLang Structures
  • GoLang Arrays
  • GoLang Strings
  • GoLang Pointers
  • GoLang Interface
  • GoLang Concurrency
Open In App
Next Article:
Named Return Parameters in Golang
Next article icon

Named Return Parameters in Golang

Last Updated : 25 Jan, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Prerequisite: Functions in Golang
In Golang, Named Return Parameters are generally termed as the named parameters. Golang allows giving the names to the return or result parameters of the functions in the function signature or definition. Or you can say it is the explicit naming of the return variables in the function definition. Basically, it eliminates the requirement of mentioning the variables name with the return statement. By using named return parameters or named parameters one can only use return keyword at the end of the function to return the result to the caller. This concept is generally used when a function has to return multiple values. So for the user comfort and to enhance the code readability, Golang provide this facility. 
 

Declaring the Named Return Parameters


To declare the named result or return parameters, just use the return type part of the function signature. Below is the general syntax to declare a function in Golang.
Syntax to declare a function without named return arguments:
 

func function_name(Parameter-list)(Return_type){     // function body..... }


Here, the Return_Type is optional and it contains the types of values that function returns. If you are using Return_Type in your function, then it is necessary to use a return statement in your function.
Syntax to declare a function with named return arguments:
 

func function_name(Parameter-list)(result_parameter1 data-_type, result_parameter2 data_type, ....){ 
// function body.....
return 
} 
 


Here, (result_parameter1 data-_type, result_parameter2 data_type, ....) is the named return argument list along with their type. You can declare n number of named return parameters.
 

Named-Return-Parameters-Golang1


Example: In the below program, the func calculator(a, b int) (mul int, div int) line of code contains the named return arguments. The return statement at the end of function doesn't contain any parameters. Go compiler will automatically returns the parameters.
 

C
// Golang program to demonstrate the // use of Named Return Arguments  package main  import "fmt"  // Main Method func main() {      // calling the function, here     // function returns two values     m, d := calculator(105, 7)      fmt.Println("105 x 7 = ", m)     fmt.Println("105 / 7 = ", d) }  // function having named arguments func calculator(a, b int) (mul int, div int) {      // here, simple assignment will     // initialize the values to it     mul = a * b     div = a / b      // here you have return keyword     // without any resultant parameters     return } 

Output:
 

105 x 7 =  735 105 / 7 =  15


 

Important Points


 

  • If the type of all the named return arguments is common or same then you can specify the common data type. Compare the below code with the example that you read above to get a better understandability.
     
// function having named arguments func calculator(a, b int) (mul, div int) {

  • Here, mul and div variables are both int type. So you can also declare named arguments with common data type like the function variables(i.e. a and b)
  • Using Named return parameters will enhance the code readability as one can know about the return parameters by just reading the function signature.
  • After using named return parameters the return statement is generally termed as Naked or Bare return.
  • By default, Golang defines all the named variables with the zero value and function will able to use them. In case function doesn't modify the values then automatically zero value will return.
  • If you will use short declaration operator(:=) to initialize the named return parameters, it will give an error as they are already initialized by the Go compiler. So you can use simple assignment(=) to assign the values to named return parameters.
     
// function having named arguments func calculator(a, b int) (mul int, div int) {      // here, it will give an error         // as parameters are already defined         // in function signature     mul := a * b     div := a / b      // here you have return keyword     // without any resultant parameters     return }
  • Named return arguments or bare return statements are only good for the short function signature. For longer functions, explicitly return the result parameters(not use named return parameters) to maintain the readability of the code.
  • In the case of named return arguments, bare or naked return statement is a must.


 


Next Article
Named Return Parameters in Golang

A

anshul_aggarwal
Improve
Article Tags :
  • Go Language
  • Golang

Similar Reads

    string package in Golang
    Go language provides a string package that holds different types of functions to manipulate UTF-8 encoded strings. To access the function of the string package you need to import a string package in your program with the help of the import keyword.   FunctionDescriptionfunc CompareThis function is u
    8 min read
    strconv package in Golang
    Go language provides a strconv package that implements conversions to and from string representations of basic data types. To access the functions of the strconv package you need to import the strconv package in your program with the help of the import keyword. .strconv-golang-table { border-collaps
    5 min read
    Interfaces in Golang
    In Go, an interface is a type that lists methods without providing their code. You can’t create an instance of an interface directly, but you can make a variable of the interface type to store any value that has the needed methods.Exampletype Shape interface { Area() float64 Perimeter() float64}In t
    3 min read
    Packages in Golang
    Packages are the most powerful part of the Go language. The purpose of a package is to design and maintain a large number of programs by grouping related features together into single units so that they can be easy to maintain and understand and independent of the other package programs. This modula
    5 min read
    Multiple Interfaces in Golang
    In Go language, the interface is a collection of method signatures and it is also a type means you can create a variable of an interface type. In Go language, you are allowed to create multiple interfaces in your program with the help of the given syntax: type interface_name interface{ // Method sig
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences