Skip to content

Commit f57c61a

Browse files
Update Functions & Methods Topic
1 parent 90784a8 commit f57c61a

9 files changed

Lines changed: 292 additions & 39 deletions

File tree

docs/programming-languages/functions-methods/_c.mdx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ add_all_int(3 ,2,4,7);
9595
// Generic/Template
9696
// ------------------------------------
9797
98-
// TODO
98+
// No Native Support.
99+
// Common options are macros, void pointers, or separate functions per type.
100+
101+
#define max(a, b) ((a) > (b) ? (a) : (b))
102+
103+
int result = max(10, 20);
99104
```
100105

101106
```C

docs/programming-languages/functions-methods/_cpp.mdx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,19 @@ add_all_int(3 ,2,4,7);
135135
// Generic/Template
136136
// ------------------------------------
137137
138-
// TODO
138+
template <typename T>
139+
T identity(T value)
140+
{
141+
return value;
142+
}
143+
144+
template <typename T>
145+
T max(T first, T second)
146+
{
147+
return first > second ? first : second;
148+
}
149+
150+
auto result = max(10, 20);
139151
```
140152

141153
```Cpp

docs/programming-languages/functions-methods/_csharp.mdx

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,100 @@
33
// Definition
44
// ------------------------------------
55
6-
// TODO
6+
[access_modifier] [return_type] FunctionName([parameters])
7+
{
8+
// ...
9+
}
710

811
// ------------------------------------
912
// Declaration Example
1013
// ------------------------------------
1114
12-
// TODO
15+
void MyFunction(float x)
16+
{
17+
// ...
18+
}
19+
20+
float MyFunction()
21+
{
22+
return 10.5f;
23+
}
24+
25+
User MyFunction(int id)
26+
{
27+
return new User();
28+
}
1329

1430
// ------------------------------------
1531
// Named Parameters (Keyword Arguments)
1632
// ------------------------------------
1733
18-
// TODO
34+
void PrintUser(string name, int age)
35+
{
36+
// ...
37+
}
38+
39+
PrintUser(name: "Ada", age: 36);
40+
PrintUser(age: 36, name: "Ada");
1941

2042

2143
// ------------------------------------
2244
// Optional Parameters
2345
// ------------------------------------
2446
25-
// TODO
47+
// Optional parameters must appear after required parameters.
48+
void PrintUser(string name, int age = 0)
49+
{
50+
// ...
51+
}
52+
53+
PrintUser("Ada");
54+
PrintUser("Ada", 36);
2655

2756

2857
// ------------------------------------
2958
// Default Argument for Parameters
3059
// ------------------------------------
3160
32-
// TODO
61+
void Connect(string host, int port = 443, bool useSsl = true)
62+
{
63+
// ...
64+
}
3365

3466

3567
// ------------------------------------
3668
// Variable Number of Arguments to a Function Parameters
3769
// ------------------------------------
3870
39-
// TODO
71+
int AddAll(params int[] numbers)
72+
{
73+
int result = 0;
74+
75+
foreach (int number in numbers)
76+
{
77+
result += number;
78+
}
79+
80+
return result;
81+
}
82+
83+
AddAll(1, 2, 3);
4084
```
4185

4286
```Cs
4387
// ------------------------------------
4488
// Generic/Template
4589
// ------------------------------------
4690
47-
// TODO
91+
T Identity<T>(T value)
92+
{
93+
return value;
94+
}
95+
96+
T Max<T>(T first, T second) where T : IComparable<T>
97+
{
98+
return first.CompareTo(second) >= 0 ? first : second;
99+
}
48100
```
49101

50102
```Cs

docs/programming-languages/functions-methods/_go.mdx

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,99 @@
33
// Definition
44
// ------------------------------------
55

6-
// TODO
6+
func functionName([parameters]) [returnType] {
7+
// ...
8+
}
79

810
// ------------------------------------
911
// Declaration Example
1012
// ------------------------------------
1113

12-
// TODO
14+
func myFunction(x float32) {
15+
// ...
16+
}
17+
18+
func getNumber() float32 {
19+
return 10.5
20+
}
21+
22+
func add(first int, second int) int {
23+
return first + second
24+
}
25+
26+
func swap(first string, second string) (string, string) {
27+
return second, first
28+
}
1329

1430
// ------------------------------------
1531
// Named Parameters (Keyword Arguments)
1632
// ------------------------------------
1733

18-
// TODO
34+
// No Native Support.
35+
// Use structs when argument names matter.
36+
37+
type CreateUserOptions struct {
38+
Name string
39+
Age int
40+
}
41+
42+
func CreateUser(options CreateUserOptions) {
43+
// ...
44+
}
1945

2046

2147
// ------------------------------------
2248
// Optional Parameters
2349
// ------------------------------------
2450

25-
// TODO
51+
// No Native Support.
52+
// Use zero values, structs, or separate functions.
2653

2754

2855
// ------------------------------------
2956
// Default Argument for Parameters
3057
// ------------------------------------
3158

32-
// TODO
59+
// No Native Support.
60+
// Assign defaults inside the function when needed.
3361

3462

3563
// ------------------------------------
3664
// Variable Number of Arguments to a Function Parameters
3765
// ------------------------------------
3866

39-
// TODO
67+
func addAll(numbers ...int) int {
68+
result := 0
69+
70+
for _, number := range numbers {
71+
result += number
72+
}
73+
74+
return result
75+
}
76+
77+
addAll(1, 2, 3)
4078
```
4179

4280
```Go
4381
// ------------------------------------
4482
// Generic/Template
4583
// ------------------------------------
4684

47-
// TODO
85+
// Since Go 1.18
86+
func Identity[T any](value T) T {
87+
return value
88+
}
89+
90+
func Contains[T comparable](values []T, target T) bool {
91+
for _, value := range values {
92+
if value == target {
93+
return true
94+
}
95+
}
96+
97+
return false
98+
}
4899
```
49100

50101
```Go

docs/programming-languages/functions-methods/_java.mdx

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,89 @@
33
// Definition
44
// ------------------------------------
55

6-
// TODO
6+
[access_modifier] [return_type] functionName([parameters]) {
7+
// ...
8+
}
79

810
// ------------------------------------
911
// Declaration Example
1012
// ------------------------------------
1113

12-
// TODO
14+
void myFunction(float x) {
15+
// ...
16+
}
17+
18+
float myFunction() {
19+
return 10.5f;
20+
}
21+
22+
User myFunction(int id) {
23+
return new User();
24+
}
1325

1426
// ------------------------------------
1527
// Named Parameters (Keyword Arguments)
1628
// ------------------------------------
1729

18-
// TODO
30+
// No Native Support.
31+
// Use an object, builder, or clear method names when argument meaning is not obvious.
32+
33+
createUser(new CreateUserRequest("Ada", 36));
1934

2035

2136
// ------------------------------------
2237
// Optional Parameters
2338
// ------------------------------------
2439

25-
// TODO
40+
// No Native Support.
41+
// Use method overloading.
42+
43+
void connect(String host) {
44+
connect(host, 443);
45+
}
46+
47+
void connect(String host, int port) {
48+
// ...
49+
}
2650

2751

2852
// ------------------------------------
2953
// Default Argument for Parameters
3054
// ------------------------------------
3155

32-
// TODO
56+
// No Native Support.
57+
// Use method overloading or assign defaults inside the method.
3358

3459

3560
// ------------------------------------
3661
// Variable Number of Arguments to a Function Parameters
3762
// ------------------------------------
3863

39-
// TODO
64+
int addAll(int... numbers) {
65+
int result = 0;
66+
67+
for (int number : numbers) {
68+
result += number;
69+
}
70+
71+
return result;
72+
}
73+
74+
addAll(1, 2, 3);
4075
```
4176

4277
```Java
4378
// ------------------------------------
4479
// Generic/Template
4580
// ------------------------------------
4681

47-
// TODO
82+
public static <T> T identity(T value) {
83+
return value;
84+
}
85+
86+
public static <T extends Comparable<T>> T max(T first, T second) {
87+
return first.compareTo(second) >= 0 ? first : second;
88+
}
4889
```
4990

5091
```Java

docs/programming-languages/functions-methods/_javascript.mdx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,12 @@ console.log(addAll(1,2,3));
118118
// Generic/Template
119119
// ------------------------------------
120120

121-
// TODO
121+
// No Native Support.
122+
// Javascript functions are dynamically typed.
123+
124+
function identity(value) {
125+
return value;
126+
}
122127
```
123128

124129
```Javascript

docs/programming-languages/functions-methods/_python.mdx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,16 @@ def my_function(parameter1, parameter2, *args, **kwargs):
128128
# Generic/Template
129129
# ------------------------------------
130130

131-
# TODO
131+
from typing import TypeVar
132+
133+
T = TypeVar("T")
134+
135+
def identity(value: T) -> T:
136+
return value
137+
138+
# Since Python 3.12, type parameters can also be declared inline.
139+
def first[T](values: list[T]) -> T | None:
140+
return values[0] if values else None
132141
```
133142

134143
```Python

0 commit comments

Comments
 (0)