Â
Definition of functions:-
A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. It is a unit of code that is often defined by its role within a greater code structure. Specifically, a function contains a unit of code that works on various inputs, many of which are variables, and produces concrete results involving changes to variable values or actual operations based on the inputs.
*Note:- Functions are also called as Methods, so don't get confused when methods are used instead of functions.
Need of functions in Programming:-
1. To improve the readability of code.
2. Improves the reusability of the code, same function can be used in any program rather than writing the same code from scratch.
3. Debugging of the code would be easier if you use functions, as errors are easy to be traced.
4. Reduces the size of the code, duplicate set of statements are replaced by function calls.
5. We can divide a large program into multiple small modules.
6. Function Hide the complexities.
Types of Functions in C programming language:-
1. Pre-defined Functions
2. User-defined Functions
Pre-defined Functions:-
These are the functions that are built into Language to perform operations & are stored in the Standard Function Library.
Some examples of pre-defined functions in C++ are print, println, etc.
User-defined Functions:-
User-defined functions are defined by the user to perform specific tasks. There are four different patterns to define a function −
-Functions with no argument and no return value.
-Functions with no argument but a return value.
-Functions with argument but no return value.
-Functions with argument and a return value.
Syntax of declaring functions/methods in Java:
1. Functions with no argument and no return value.
class class_name
{
public static datatype function_name()
{
// code
}
}
2. Functions with no argument but a return value.
class class_name
{
public static datatype function_name()
{
// code
return return_value;
}
}
3. Functions with argument but no return value.
class class_name
{
public static datatype function_name(datatype argument_name)
{
// code
}
}
4. Functions with argument and a return value.
class class_name
{
public static datatype function_name(datatype argument_name)
{
// code
return return_value;
}
}
Syntax of Calling functions/methods in Java:
1. Functions with no argument and no return value.
class class_name
{
public static void main(String[] args)
{
function_name();
}
}
2. Functions with no argument but a return value.
class class_name
{
public static void main(String[] args)
{
int variable_name = function_name();
System.out.println(variable_name);
}
}
// OR
class class_name
{
public static void main(String[] args)
{
System.out.println(function_name());
}
}
3. Functions with argument but no return value.
class class_name
{
public static void main(String[] args)
{
function_name(argument_name);
}
}
4. Functions with argument and a return value.
class class_name
{
public static void main(String[] args)
{
int variable_name = function_name(argument_name);
System.out.println(variable_name);
}
}
// OR
class class_name
{
public static void main(String[] args)
{
System.out.println(function_name(argument_name));
}
}
Examples for functions in Java:
In all the below examples we are going to see how to write a program to find sum of 1 to n natural numbers using the above four patterns of functions.
1. Functions with no argument and no return value.
// 1. 'no argument' and 'no return '
import java.util.*;
class Main
{
public static void show_sum()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number till where you want the sum: ");
int num = sc.nextInt();
int sum = 0;
for (int i = 1; i <= num; i++)
{
sum += i;
}
System.out.println("Sum of 1 to " + num + " is: " + sum);
}
public static void main(String[] args)
{
show_sum();
}
}
2. Functions with no argument but a return value.
// 2. 'no argument' but 'return '
import java.util.*;
class Main
{
public static int show_sum()
{
int num;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number till where you want the sum: ");
num = sc.nextInt();
int sum = 0;
for (int i = 1; i <= num; i++)
{
sum += i;
}
return sum;
}
public static void main(String[] args)
{
System.out.println("Sum of numbers is: " + show_sum());
}
}
3. Functions with argument but no return value.
// 3. 'argument' but 'no return '
import java.util.*;
class Main
{
public static void show_sum(int num)
{
int sum = 0;
for (int i = 1; i <= num; i++) {
sum += i;
}
System.out.println("Sum of 1 to " + num + " is: " + sum);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number till where you want the sum: ");
int num = sc.nextInt();
show_sum(num);
}
}
4. Functions with argument and a return value.
// 4. 'argument' and 'return '
import java.util.*;
class Main
{
public static int show_sum(int num)
{
int sum = 0;
for (int i = 1; i <= num; i++)
{
sum += i;
}
return sum;
}
public static void main(String[] args)
{
int num;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number till where you want the sum: ");
num = sc.nextInt();
System.out.println("Sum of 1 to " + num + " is: " + show_sum(num));
}
}
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP