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:
Naming the Return Values of a Function in Golang
Next article icon

Naming the Return Values of a Function in Golang

Last Updated : 10 May, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
A return value helps to retain the final output of the function after it performs the instructions given in its body. Functions in Golang exhibit variety in return values and rely on the programmer to decide whether to name them or not. Golang introduces a concept of "Naked Return" allowing the use of return keyword without explicitly stating the return values in the function body provided that the return values are declared in the function header. However, the variable name must be the same as the one defined in the function header. C
package main  import "fmt"  // This function returns a string message by taking a parameter // "name" of string type. The Naked return concept is being // displayed here since the return statement not specify the return // of message variable. The returning of the message variable // has already being defined  func greeting(name string) (message string) {      // Since the variable is already      // defined in function variable     // there is no need to declare it      // again in the function body     // using " := " operator.      // The name "message" must be same as      // the one declared in the function header.     message = "Hello, " + name          // This statement returns message variable     // without explicitly stating its name     return  }  // Driver code func main() {      // The return value of greeting      // function is stored in msg variable     msg := greeting("GFG")          // Printing output     fmt.Println(msg)  } 
Output:
  Hello, GFG  
Also, the function can return a variable without specifying its name in the function header but only its datatype. C
package main  import "fmt"  // This function returns a string datatype // Since the return variable is not  // defined in the function header, // There is an explicit need to specify // the variable to return  func greeting(name string) string {     message := "Hello, " + name     return message }  // Driver code func main() {     msg := greeting("GFG")     fmt.Println(msg) } 
Output:
  Hello, GFG  
A function can also return multiple values and receive multiple parameters. However, to work with a function that returns multiple values, the number of variables that the function returns must be equal to the number of variables that hold the return values when the function execution finishes. C
package main  import "fmt"  // The below function returns four // variables after performing operations // on the two parameters that were passed to it  func operations(a, b int) (sum, diff, prod, div int) {      // The four variables to be returned are defined here      sum, diff, prod, div = a+b, a-b, a*b, a/b      // returning the above-defined four variables.     return }  // Driver code func main() {     a, b := 5, 3      // Since the function returns four values,     // Four receiver variables must be used.     sum, diff, prod, div := operations(a, b)     fmt.Println(sum, diff, prod, div) } 
Output:
  8 2 15 1  

Next Article
Naming the Return Values of a Function in Golang

T

TanmayThaakur
Improve
Article Tags :
  • Go Language

Similar Reads

    Golang Program that Uses Named Return Values and Defaults
    Golang functions have special functionality that allows them to provide the name to the return values. These named return values can be used as arguments or variables. The named return values also use the default values for the data types like 0 for int type etc. To understand this concept let's tak
    1 min read
    Function Returning Multiple Values in Go Language
    In Go language, you are allowed to return multiple values from a function, using the return statement. Or in other words, in function, a single return statement can return multiple values. The type of the return values is similar to the type of the parameter defined in the parameter list. Syntax: fu
    3 min read
    How to Find the Sin and Cos Value of a Number in Golang?
    Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. You can find the value of sin and cos of the specified number with the help of Sincos() function provided by the math package. So, you need to a
    2 min read
    main and init function in Golang
    The Go language reserve two functions for special purpose and the functions are main() and init() function.main() functionIn Go language, the main package is a special package which is used with the programs that are executable and this package contains main() function. The main() function is a spec
    2 min read
    reflect.Int() Function in Golang with Examples
    Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Int() Function in Golang is used to get the v's underlying value, as an int64. To access this function, one needs
    1 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