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)
}
Answer Program 1:
Output:
Explanation:
The above program will generate a syntax error because we did not define the function sum properly. The correct program is given below,
Answer Program 2:
Output:
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,
Program 3:
Output:
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:
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:
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