In this video you will learn about functions in java.
Functions / Methods in Java
A function/method is a block of code that performs a specific task. A function only runs when it is called.
Dividing a complex problem into smaller functions/methods makes our program easy to understand and reusable.
Functions are used to perform certain actions, and they are important for reusing code.
They allow to define the function once, and use it many times.
We can have any number of functions in a program.
There are two types of function:
Standard Library Functions: Predefined in Java classes inside packages.
User-defined Function: Created by users as per need.
Java User-defined Function
JAVA allows the programmer to define their own function and use the function when needed.
When the function is invoked from any part of the program, it executes the code defined in the body of the function.
Every user defined function has two parts:
i)Function definition
ii)Function call
Function definition
The function definition has following syntax:
returntype functionname(datatype parametername)
{ //code goes here
return value;
}
Function call
The function call is used to activate the function definition. The function call looks like as follows:
functionname(argumentifany);
The function is always called from another function.
Depending on the type, a function can take argument or can return value using return keyword. We will discuss them later.
We can create user defined function in following four ways:
i)Function without argument, without return value
ii)Function with argument, without return value
iii)Function without argument, with return value
iv)Function with argument, with return value
Function without argument, without return value
It is the simplest type of UDF. In this type of function all input, calculation and output are done in function body(definition).
The definition of this type of function looks like as follows:
void functionname(void)
{ //code goes here
}
The call of this type of function looks as follows:
functionname();
Note:
a) In OOP a static method can use other static members of that class directly.
b) The static method cannot use non-static members of the class directly.
c) In java, the main method is always static, so it can use other static methods of the class.
d) we qualify methods with static keyword, so that they can be used directly inside the main.
Q: WAP to create a function which prints sum of two numbers
Learn C Language from beginning
• learn c programming in Hinglish.
Learn C++ from beginning
• Introduction to c++ | Creating first ...
Learn Java from beginning
• Java Tutorial For Beginners In Hindi ...
Learn Python from beginning
• introduction to python, download and ...
Learn PHP from beginning
• Introduction to php | getting started...
Connect me on linkedin
/ tarrunverrma
Connect me on instagram
/ the_ultimate_coding_stuff
Follow me on twitter
/ tarrunverrma
Join my telegram channel
t.me/tarunvermasir
#tarunsir #javatutorial #learnprogramming #coding #java #function