Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

Functions

This is a simple Go program that defines two functions for addition and demonstrates their usage in the main function.

// Importing the "fmt" package, which provides functions for formatted I/O.
import "fmt"

// Function plus takes two integers (a and b) as parameters and returns their sum.
func plus(a int, b int) int {
    return a + b
}

// Function plusPlus takes three integers (a, b, and c) as parameters and returns their sum.
func plusPlus(a, b, c int) int {
    return a + b + c
}

// The main function, which serves as the entry point for the program.
func main() {
    // Calling the plus function with arguments 1 and 2, storing the result in the variable "res".
    res := plus(1, 2)
    
    // Printing the result of the addition operation.
    fmt.Println("1+2 =", res)

    // Calling the plusPlus function with arguments 1, 2, and 3, storing the result in the variable "res".
    res = plusPlus(1, 2, 3)
    
    // Printing the result of the addition operation.
    fmt.Println("1+2+3 =", res)
}

Output

1+2 = 3
1+2+3 = 6

Now, let's break down the code and explain each part:

  1. Function Declarations:

    • plus(a int, b int) int: This function takes two integers (a and b) as parameters and returns their sum.

    • plusPlus(a, b, c int) int: This function takes three integers (a, b, and c) as parameters and returns their sum.

  2. Main Function:

    • main(): This is the entry point of the program.

    • res := plus(1, 2): Calls the plus function with arguments 1 and 2, and stores the result in the variable res.

    • fmt.Println("1+2 =", res): Prints the result of the addition operation.

    • res = plusPlus(1, 2, 3): Calls the plusPlus function with arguments 1, 2, and 3, and stores the result in the variable res.

    • fmt.Println("1+2+3 =", res): Prints the result of the addition operation.

PreviousRangeNextMultiple Return Values

Last updated 1 year ago

Was this helpful?