Skip to content

Commit 18d66ce

Browse files
authored
Merge pull request #56 from Vivek-Prajapatii/DSA-branch
function & scope
2 parents 88bcb06 + 9c42f41 commit 18d66ce

File tree

1 file changed

+203
-0
lines changed

1 file changed

+203
-0
lines changed

lessons/functions.md

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,206 @@ order: "3F"
55
section: "Learn Java"
66
description: "Learn how to use functions and scope"
77
---
8+
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.
10+
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.
12+
13+
In java, **Method** is also called as **Behaviour**.
14+
And **Variables** are also called as **States**.
15+
16+
17+
### **Syntax**
18+
19+
```java
20+
public static int function()
21+
{
22+
//your function Body here//
23+
}
24+
```
25+
26+
### **Example**
27+
28+
```java
29+
public static void function()
30+
{
31+
System.out.println("Hello World");
32+
}
33+
```
34+
The main function which calls the above function is as follows:
35+
36+
```java
37+
public static void main(String []args)
38+
{
39+
//function() called below to print "Hello World"
40+
function();
41+
}
42+
```
43+
44+
The **output** is as follows
45+
```js
46+
Hello World
47+
```
48+
49+
50+
## **Return type + program examples**
51+
52+
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).
53+
54+
To return a something from a method you need to use "**return**" keyword followed by the name of the variable or object.
55+
56+
The below example depicts to return a **Integer(int)** value.
57+
58+
```java
59+
public static int function()
60+
{
61+
int count = 0;
62+
63+
//your code here
64+
65+
//returning count
66+
return count;
67+
}
68+
```
69+
70+
Make sure that you return the integer value or else, **Compilation fails**.
71+
72+
73+
## **Parameters and Pass-By-Value**
74+
75+
76+
## **Scope**
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.
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.
79+
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.
83+
84+
```java
85+
class Scope
86+
{
87+
static int c = 10;
88+
//the reason to make c as static is that if we want
89+
//to use a variable inside a static method then it
90+
//should be static
91+
92+
public static void main(String []args)
93+
{
94+
//below both are local variables.
95+
int a = 10;
96+
int b = 20;
97+
98+
//Block Statement
99+
if(a > b)
100+
{
101+
//here variable d is block variable
102+
//so d cannot be used outside if statement
103+
int d = a + b ;
104+
}
105+
106+
//if i try to print value of variable d below
107+
//it will throw an error.
108+
//System.out.pritnln(d);
109+
110+
//but we can print c here because c is a global variable
111+
//and its scope is all ove the class.
112+
System.out.println(c);
113+
114+
}// end of main function
115+
116+
}//end of class Scope
117+
```
118+
119+
120+
## **Shadowing**
121+
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.
123+
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.
125+
126+
You can get it well by refering to the below example
127+
128+
```java
129+
class shadow
130+
{
131+
static int x = 20;
132+
public static void main(String []args)
133+
{
134+
//here it preints the global variable
135+
System.out.println(x);
136+
137+
int x = 40;
138+
//here the overlapping is met
139+
//so it will print the local variable
140+
System.out.println(x);
141+
}
142+
143+
}
144+
```
145+
The output is as follows:
146+
```
147+
20
148+
40
149+
```
150+
151+
152+
153+
## **Overloading**
154+
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.
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.
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.
158+
159+
**Rules of Overloading**
160+
161+
- Overloaded methods must change the **arguments** list i.e numbers, datatye or sequence.
162+
- Overloaded methods can change the **return** type.
163+
- overloaded methods can change the **access modifier**.
164+
165+
The below example depicts the concept of method overloading.
166+
167+
```java
168+
class Adder
169+
{
170+
static int add(int a,int b)
171+
{
172+
return a+b;
173+
}
174+
175+
static int add(int a,int b,int c)
176+
{
177+
return a+b+c;
178+
}
179+
}
180+
181+
class TestOverloading
182+
{
183+
public static void main(String[] args)
184+
{
185+
System.out.println(Adder.add(11,11));
186+
System.out.println(Adder.add(11,11,11));
187+
}
188+
}
189+
```
190+
The output is as follows
191+
```
192+
22
193+
33
194+
```
195+
196+
197+
## **Conclusion**
198+
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.
200+
201+
And also seen that how important is, the concept of overloading and the rules for it.
202+
Shadowing is also a great and unique concept where, whenever the variables overlaps then the latest one is considered.
203+
204+
## **Some Examples to practice**
205+
206+
- Write a java program to find the area of different shapes by using the concept of method overloading.
207+
- Write a suitable java program which depicts the concept of shadowing.
208+
209+
210+

0 commit comments

Comments
 (0)