You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lessons/functions.md
+247Lines changed: 247 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -5,3 +5,250 @@ order: "3F"
5
5
section: "Learn Java"
6
6
description: "Learn how to use functions and scope"
7
7
---
8
+
9
+
## **Introduction to Methods/Functions**
10
+
11
+
Whether you a professional programmer or just a beginner it is always better to break your programme into blocks, or functions. It will help you later, first of all in debugging, secondly, it is just better to have an elegant, readable and organized code.
12
+
13
+
A function is a part of a program that has its own name. This name can be used in the program as a command (this command is called a function call). When a function is called, the commands of which it consists are executed. A function call can return a value (similar to an operation) and therefore can be used in an expression along with operations.
14
+
15
+
In java, **Method** is also called as **Behaviour**.
16
+
And **Variables** are also called as **States**.
17
+
18
+
19
+
## **Syntax**
20
+
21
+
```
22
+
public static int function()
23
+
{
24
+
//your function Body here//
25
+
}
26
+
```
27
+
28
+
## **Example**
29
+
```
30
+
public static void function()
31
+
{
32
+
System.out.println("Hello World");
33
+
}
34
+
```
35
+
The main function which calls the above function is as follows:
36
+
37
+
```
38
+
public static void main(String []args)
39
+
{
40
+
//function() called below to print "Hello World"
41
+
function();
42
+
}
43
+
```
44
+
45
+
T
46
+
he **output** is as follows
47
+
```
48
+
Hello World
49
+
```
50
+
51
+
52
+
## **Return type + program examples**
53
+
54
+
Whenever you define a method in java, you specify a return type i.e. that can be any data type, whether primitive or non-primitive and if you don't want to return a data-type you mark it as void (that means you dont want to return anything from the method).
55
+
56
+
To return a something from a method you need to use "**return**" keyword followed by the name of the variable or object.
57
+
58
+
The below example depicts to return a **Integer(int)** value.
59
+
60
+
```
61
+
public static int function()
62
+
{
63
+
int count = 0;
64
+
65
+
//your code here
66
+
67
+
//returning count
68
+
return count;
69
+
}
70
+
```
71
+
72
+
Make sure that you return the integer value or else, **Compilation fails**.
73
+
74
+
75
+
## **Parameters and Pass-By-Reference**
76
+
Unlike any other language you can also pass some parameters to the methods in java.
77
+
If your method needs some data to compute inside it then you must pass a **reference** of that data in the form of parameters of your method. Again it can be of any data-type or else you can also pass the reference of any **object**.
78
+
So, here we use **Pass-by-reference** not **Pass-by-value**.
79
+
80
+
Before passing the reference you should make your method in such a way that it should be able to take the parameters as shown in the below code.
81
+
82
+
Suppose in the below example, you need to pass the reference of two integers to a method called as **sum** and your method returns the sum of both the two integers.
83
+
84
+
```
85
+
public static int sum(int num_1, int num_2)
86
+
{
87
+
//sum of two numbers.
88
+
int sum = num_1 + num_2;
89
+
90
+
//return sum of the numbers.
91
+
return sum;
92
+
}
93
+
```
94
+
```
95
+
public static void main(String []args)
96
+
{
97
+
int a = 10;
98
+
int b = 20;
99
+
100
+
//Suppose you need to add above two numbers.
101
+
102
+
//passing the refernce of 'a' and 'b'.
103
+
int sum = sum(a, b);
104
+
105
+
System.out.println(sum);
106
+
}
107
+
```
108
+
109
+
**Execution of a Method**
110
+
111
+
## **Scope**
112
+
**Scope** of a variable is the part of the program where the variable is **accessible**. Like C/C++, in Java, all identifiers are lexically (or statically) scoped, i.e.scope of a variable can determined at compile time and independent of function call stack.
113
+
Java programs are organized in the form of classes. Every class is part of some package. Java scope rules can be covered under following categories.
114
+
115
+
**Member Variables**
116
+
117
+
- Scope of member variable is all over the class. You can use it anywhere but inside the class.
118
+
119
+
**Local Variables**
120
+
121
+
- Scope of Local variable is between the methods not outside the method, if you do so compiler throws an exception.
122
+
123
+
**Block Variables**
124
+
125
+
- The variables declared inside the block level statements cannot be accessed from outside the block. These blocks includes different types of loops, if else statements, etc.
126
+
127
+
```
128
+
class Scope
129
+
{
130
+
static int c = 10;
131
+
//the reason to make c as static is that if we want
132
+
//to use a variable inside a static method then it
133
+
//should be static
134
+
135
+
public static void main(String []args)
136
+
{
137
+
//below both are local variables.
138
+
int a = 10;
139
+
int b = 20;
140
+
141
+
//Block Statement
142
+
if(a > b)
143
+
{
144
+
//here variable d is block variable
145
+
//so d cannot be used outside if statement
146
+
int d = a + b ;
147
+
}
148
+
149
+
//if i try to print value of variable d below
150
+
//it will throw an error.
151
+
//System.out.pritnln(d);
152
+
153
+
//but we can print c here because c is a global variable
154
+
//and its scope is all ove the class.
155
+
System.out.println(c);
156
+
157
+
}// end of main function
158
+
159
+
}//end of class Scope
160
+
```
161
+
162
+
163
+
## **Shadowing**
164
+
Shadowing in Java is the practice of using variables in overlapping scopes with the **same****name** where the variable in low-level scope overrides the variable of high-level scope. Here the variable at high-level scope is shadowed by the low-level scope variable.
165
+
166
+
In other words we can also say that it will use the global variable untill it finds local variable i.e it will use the local variable when **overlapping** is met.
167
+
168
+
You can get it well by refering to the below example
169
+
170
+
```
171
+
class shadow
172
+
{
173
+
static int x = 20;
174
+
175
+
public static void main(String []args)
176
+
{
177
+
//here it preints the global variable
178
+
System.out.println(x);
179
+
180
+
int x = 40;
181
+
//here the overlapping is met
182
+
//so it will print the local variable
183
+
System.out.println(x);
184
+
}
185
+
186
+
}
187
+
```
188
+
The output is as follows:
189
+
```
190
+
20
191
+
40
192
+
```
193
+
194
+
195
+
196
+
## **Overloading**
197
+
198
+
- In method overloading, we can create methods that have the same name, but the methods differ in type, number and/or sequence of parameters.
199
+
200
+
- When an overloaded method is invoked, Java uses the type, number and/or sequence of arguments as its guide to determine which version of the overloaded method to actually call.
201
+
202
+
- When java encounters a call to an overloaded method, it simplyexecutes the version of the method whose parameters match the arguments used in the call.
203
+
204
+
**Rules of Overloading**
205
+
206
+
- Overloaded methods must change the **arguments** list i.e numbers, datatye or sequence.
207
+
- Overloaded methods can change the **return** type.
208
+
- overloaded methods can change the **access modifier**.
209
+
210
+
The below example depicts the concept of method overloading.
211
+
212
+
```
213
+
class Adder
214
+
{
215
+
static int add(int a,int b)
216
+
{
217
+
return a+b;
218
+
}
219
+
220
+
static int add(int a,int b,int c)
221
+
{
222
+
return a+b+c;
223
+
}
224
+
}
225
+
226
+
class TestOverloading
227
+
{
228
+
public static void main(String[] args)
229
+
{
230
+
System.out.println(Adder.add(11,11));
231
+
System.out.println(Adder.add(11,11,11));
232
+
}
233
+
}
234
+
```
235
+
The output is as follows
236
+
```
237
+
22
238
+
33
239
+
```
240
+
241
+
242
+
## **Conclusion**
243
+
Hence after refering the above topics, we can conclude that if you need some code to be executed again again then we must use methods to increase the code reusability and increase the performance of the program.
244
+
245
+
And also seen that how important is, the concept of overloading and the rules for it.
246
+
Shadowing is also a great and unique concept where, whenever the variables overlaps then the latest one is considered.
247
+
248
+
## **Some Examples to practice**
249
+
- Write a java program to find the area of different shapes by using the concept of method overloading.
250
+
251
+
- Write a suitable java program which depicts the concept of shadowing.
0 commit comments