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
+19-63Lines changed: 19 additions & 63 deletions
Original file line number
Diff line number
Diff line change
@@ -6,8 +6,6 @@ section: "Learn Java"
6
6
description: "Learn how to use functions and scope"
7
7
---
8
8
9
-
## **Introduction to Methods/Functions**
10
-
11
9
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
10
13
11
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.
@@ -16,35 +14,35 @@ In java, **Method** is also called as **Behaviour**.
16
14
And **Variables** are also called as **States**.
17
15
18
16
19
-
## **Syntax**
17
+
###**Syntax**
20
18
21
-
```
19
+
```java
22
20
publicstaticint function()
23
21
{
24
22
//your function Body here//
25
23
}
26
24
```
27
25
28
-
## **Example**
29
-
```
26
+
### **Example**
27
+
28
+
```java
30
29
publicstaticvoid function()
31
30
{
32
31
System.out.println("Hello World");
33
32
}
34
33
```
35
34
The main function which calls the above function is as follows:
36
35
37
-
```
36
+
```java
38
37
publicstaticvoid main(String []args)
39
38
{
40
39
//function() called below to print "Hello World"
41
40
function();
42
41
}
43
42
```
44
43
45
-
T
46
-
he **output** is as follows
47
-
```
44
+
The **output** is as follows
45
+
```js
48
46
Hello World
49
47
```
50
48
@@ -57,7 +55,7 @@ To return a something from a method you need to use "**return**" keyword followe
57
55
58
56
The below example depicts to return a **Integer(int)** value.
59
57
60
-
```
58
+
```java
61
59
publicstaticint function()
62
60
{
63
61
int count =0;
@@ -72,59 +70,18 @@ public static int function()
72
70
Make sure that you return the integer value or else, **Compilation fails**.
73
71
74
72
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.
73
+
## **Parameters and Pass-By-Value**
101
74
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
75
111
76
## **Scope**
112
77
**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
78
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
79
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.
80
+
1.**Member Variables** - Scope of member variable is all over the class. You can use it anywhere but inside the class.
81
+
2.**Local Variables** - Scope of Local variable is between the methods not outside the method, if you do so compiler throws an exception.
82
+
3.**Block Variables** - 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
83
127
-
```
84
+
```java
128
85
classScope
129
86
{
130
87
staticint c =10;
@@ -161,17 +118,17 @@ class Scope
161
118
162
119
163
120
## **Shadowing**
121
+
164
122
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
123
166
124
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
125
168
126
You can get it well by refering to the below example
169
127
170
-
```
128
+
```java
171
129
classshadow
172
130
{
173
131
staticint x =20;
174
-
175
132
publicstaticvoidmain(String []args)
176
133
{
177
134
//here it preints the global variable
@@ -196,9 +153,7 @@ The output is as follows:
196
153
## **Overloading**
197
154
198
155
- 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
156
- 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
157
- 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
158
204
159
**Rules of Overloading**
@@ -209,7 +164,7 @@ The output is as follows:
209
164
210
165
The below example depicts the concept of method overloading.
211
166
212
-
```
167
+
```java
213
168
classAdder
214
169
{
215
170
staticintadd(inta,intb)
@@ -240,14 +195,15 @@ The output is as follows
240
195
241
196
242
197
## **Conclusion**
198
+
243
199
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
200
245
201
And also seen that how important is, the concept of overloading and the rules for it.
246
202
Shadowing is also a great and unique concept where, whenever the variables overlaps then the latest one is considered.
247
203
248
204
## **Some Examples to practice**
249
-
- Write a java program to find the area of different shapes by using the concept of method overloading.
250
205
206
+
- Write a java program to find the area of different shapes by using the concept of method overloading.
251
207
- Write a suitable java program which depicts the concept of shadowing.
0 commit comments