Skip to content

Commit 9c42f41

Browse files
authored
fixed
1 parent be69a12 commit 9c42f41

File tree

1 file changed

+19
-63
lines changed

1 file changed

+19
-63
lines changed

lessons/functions.md

Lines changed: 19 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ section: "Learn Java"
66
description: "Learn how to use functions and scope"
77
---
88

9-
## **Introduction to Methods/Functions**
10-
119
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.
1210

1311
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**.
1614
And **Variables** are also called as **States**.
1715

1816

19-
## **Syntax**
17+
### **Syntax**
2018

21-
```
19+
```java
2220
public static int function()
2321
{
2422
//your function Body here//
2523
}
2624
```
2725

28-
## **Example**
29-
```
26+
### **Example**
27+
28+
```java
3029
public static void function()
3130
{
3231
System.out.println("Hello World");
3332
}
3433
```
3534
The main function which calls the above function is as follows:
3635

37-
```
36+
```java
3837
public static void main(String []args)
3938
{
4039
//function() called below to print "Hello World"
4140
function();
4241
}
4342
```
4443

45-
T
46-
he **output** is as follows
47-
```
44+
The **output** is as follows
45+
```js
4846
Hello World
4947
```
5048

@@ -57,7 +55,7 @@ To return a something from a method you need to use "**return**" keyword followe
5755

5856
The below example depicts to return a **Integer(int)** value.
5957

60-
```
58+
```java
6159
public static int function()
6260
{
6361
int count = 0;
@@ -72,59 +70,18 @@ public static int function()
7270
Make sure that you return the integer value or else, **Compilation fails**.
7371

7472

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**
10174

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**
11075

11176
## **Scope**
11277
**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.
11378
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.
11479

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.
12683

127-
```
84+
```java
12885
class Scope
12986
{
13087
static int c = 10;
@@ -161,17 +118,17 @@ class Scope
161118

162119

163120
## **Shadowing**
121+
164122
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.
165123

166124
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.
167125

168126
You can get it well by refering to the below example
169127

170-
```
128+
```java
171129
class shadow
172130
{
173131
static int x = 20;
174-
175132
public static void main(String []args)
176133
{
177134
//here it preints the global variable
@@ -196,9 +153,7 @@ The output is as follows:
196153
## **Overloading**
197154

198155
- 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-
200156
- 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-
202157
- 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.
203158

204159
**Rules of Overloading**
@@ -209,7 +164,7 @@ The output is as follows:
209164

210165
The below example depicts the concept of method overloading.
211166

212-
```
167+
```java
213168
class Adder
214169
{
215170
static int add(int a,int b)
@@ -240,14 +195,15 @@ The output is as follows
240195

241196

242197
## **Conclusion**
198+
243199
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.
244200

245201
And also seen that how important is, the concept of overloading and the rules for it.
246202
Shadowing is also a great and unique concept where, whenever the variables overlaps then the latest one is considered.
247203

248204
## **Some Examples to practice**
249-
- Write a java program to find the area of different shapes by using the concept of method overloading.
250205

206+
- Write a java program to find the area of different shapes by using the concept of method overloading.
251207
- Write a suitable java program which depicts the concept of shadowing.
252208

253209

0 commit comments

Comments
 (0)