×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang Functions | Find Output Programs | Set 1

This section contains the Golang functions find output programs (set 1) with their output and explanations.
Submitted by Nidhi, on August 17, 2021

Program 1:

package main  

import "fmt"  

func int sum(a int, b int){
    return a+b
}

func main() {
    var result = 0
    
    result = sum(10,20)
    fmt.Println("Sum is: ",result)
}  

Output:

./prog.go:5:10: syntax error: unexpected sum, expecting (

Explanation:

The above program will generate a syntax error because we did not define the function sum properly. The correct program is given below,

package main

import "fmt"

func sum(a int, b int) int {
	return a + b
}
func main() {
	var result = 0

	result = sum(10, 20)
	fmt.Println("Sum is: ", result)
}

// Output: Sum is:  30

Program 2:

package main  

import "fmt"  

func sum(var a int, var b int)int{
    return a+b
}

func main() {
    var result = 0
    
    result = sum(10,20)
    fmt.Println("Sum is: ",result)
}  

Output:

./prog.go:5:10: syntax error: unexpected var, expecting )

Explanation:

The above program will generate a syntax error because we used the var keyword for the function parameter. The correct program is given below,

package main

import "fmt"

func sum(a int, b int) int {
	return a + b
}
func main() {
	var result = 0

	result = sum(10, 20)
	fmt.Println("Sum is: ", result)
}

// Output: Sum is:  30

Program 3:

package main

import "fmt"

type Student struct {
	id   int
	age  int
	name string
}

func (std Student) StudentInfo() {
	fmt.Println("Id  : ", std.id)
	fmt.Println("Age : ", std.age)
	fmt.Println("Name: ", std.name)
	fmt.Println()
}

func main() {
	student1 := Student{101, 16, "SARA"}
	student2 := Student{102, 17, "MARA"}

	fmt.Println("Student Information:")
	student1.StudentInfo()
	student2.StudentInfo()
}

Output:

Student Information:
Id  :  101
Age :  16
Name:  SARA

Id  :  102
Age :  17
Name:  MARA

Explanation:

Here, we created a structure Student. Then we created the function StudentInfo() to print the student information. After that, In the main() function, we created the two student objects and printed the student's information on the console screen.


Program 4:

package main

import "fmt"

func PrintNum(num int) {
	if num == 20 {
		return
	}
	fmt.Println("Num  : ", num)
}

func main() {
	PrintNum(10)
	PrintNum(20)
}

Output:

Num  :  10

Explanation:

Here, we created two functions PrintNum() and main(). The PrintNum() function is used to print a specified integer number. In the main() we called PrintNum() function to print a specified number.


Program 5:

package main

import "fmt"

func PrintNum(num int) {
	if num == 20 {
		return 1
	}
	fmt.Println("Num  : ", num)
}

func main() {
	PrintNum(10)
	PrintNum(20)
}

Output:

./prog.go:7:3: too many arguments to return
	have (number)
	want ()

Explanation:

The above program will generate syntax error because we tried to return value from void function PrintNum().

Golang Find Output Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.