Q:

Golang Functions | Find Output Programs | Set 1

belongs to collection: Golang Find Output Programs

0

This section contains the Golang functions find output programs (set 1) with their output and explanations.

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)
}  

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)
}  

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()
}

Program 4:

package main

import "fmt"

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

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

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)
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

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

 

Answer Program 2:

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:

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.


Answer Program 4:

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.


Answer Program 5:

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().

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Golang Find Output Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Golang Functions | Find Output Programs | Set 2... >>
<< Golang for Loop | Find Output Programs | Set 3...