Skip to content

Commit 72f501a

Browse files
committed
add function return egs
1 parent af4c728 commit 72f501a

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import "fmt"
4+
5+
/**
6+
* User defined type Profile act as struct type
7+
*/
8+
type Profile struct {
9+
name string
10+
username string
11+
message string
12+
}
13+
14+
/**
15+
* Define a CreateMessage function;
16+
* Either specify string for each or just make string at last parameter and first 2 will be asssumed as string
17+
* Also specify the return type which in case here is string
18+
*
19+
* func <function-name>([param1, param2, ...]) <return-type> { ... }
20+
*/
21+
func CreateMessage(name, username, message string) string {
22+
return message + ", " + name + " (" + username + ")"
23+
}
24+
25+
/**
26+
* Define a Greeting function;
27+
*/
28+
func Greeting(github Profile) {
29+
fmt.Println(CreateMessage(github.name, github.username, github.message))
30+
}
31+
32+
func main() {
33+
34+
var github = Profile{"Ashwin Hegde", "hegdeashwin", "Welcome to Go world!"}
35+
36+
/**
37+
* Call the function and pass the data to the function
38+
*/
39+
Greeting(github)
40+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import "fmt"
4+
5+
/**
6+
* User defined type Profile act as struct type
7+
*/
8+
type Profile struct {
9+
name string
10+
username string
11+
message string
12+
}
13+
14+
/**
15+
* Define a CreateMessage function;
16+
* Either specify string for each or just make string at last parameter and first 2 will be asssumed as string
17+
* Also specify the return type which in case here is string
18+
*
19+
* func <function-name>([param1, param2, ...]) (<return-type1>, <return-type2>) { ... }
20+
*/
21+
func CreateMessage(name, username, message string) (string, string) {
22+
23+
/**
24+
* return <first-return>, <second-return>, ...
25+
*/
26+
return "\n" + message + " " + name, "You are authorize to access the system: " + username + "\n"
27+
}
28+
29+
/**
30+
* Define a Greeting function;
31+
*/
32+
func Greeting(github Profile) {
33+
welcome, info := CreateMessage(github.name, github.username, github.message)
34+
35+
fmt.Println(welcome) // Commenting this line would throw an error "welcome declared and not used"
36+
fmt.Println(info)
37+
}
38+
39+
func main() {
40+
41+
var github = Profile{"Ashwin Hegde", "hegdeashwin", "Welcome to Go world!"}
42+
43+
/**
44+
* Call the function and pass the data to the function
45+
*/
46+
Greeting(github)
47+
}

0 commit comments

Comments
 (0)