Functions , Time complexity and Space complexity || Java +DSA course || Blog 4

 

A Function is basically a chunk of code which can used used whenever needed and can be called multiple times.

It helps us to break our code into small parts so that it is easy to access. Once we made a function, we can forget how it is working and just call it when it needs to execute. 

Overview:

  • Function
  • Time complexity
  • Space complexity

Lets make
a function for adding two number.here , we make


By using return the value of numer1+number2 is returned outside the loop and saved in g.further the g is printed. You can even take user input by sc.nextInt();.

a,b are saved as number1 and number2 in functionname() function.

import java.util.*;
public class Main{

public static int addNumber(int number1,int number2){
return number1 + number2;//returning 30 and saving it in summNumber
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int a = 10;
int b = 10;
int summNumber = addNumber(a, b);//calling function using addNumber()
                     and giving arguments(a, b). saving the sum in summNumber
System.out.println(summNumber);//printing sumNumber
sc.close();
}
}


 

The output is 20.

Function helps us to keep our code neat and tidy and organized just like books.

Functions are saved as stack in the memory.

Time complexity

We hate it when a website takes even more than 2 to 3 seconds to run / load page.So as we can see time is important enough for code to run. Time depends on the operations we run.

Time complexity(n) is directly proportional to inputs. The more input the more time taken by code. Time can be in the for, of logn , n^2 ,n^3, 2n, n, etc.

3 cases are seen in time complexity:

Best case(Omega Notation, Ω(n))

Average case(Theta Notation, Θ(n))

Worst case(Big O Notation ,O(n))

Best case is of course where the time taken by code is the least. Average case is the average of time taken and the least is like the last point which will never be exceeded. So best is always 1 .Ω(1), average case is written as Θ(n+1/2). Worst case is just O(n)

Space Complexity

Space complexity refers to the total amount of memory space used by an algorithm/program, including the space of input values for execution.

This occurs due to various variables as it uses new new memories every time new arguments are given.To solve this, we should use arrays. We will learn about arrays in our next blog.

____________________________________________________________________________________

Blog 3 : https://javaanddsa.blogspot.com/2022/06/conditional-statements-and-loops-in.html#more

Blog 2 : https://javaanddsa.blogspot.com/2022/06/data-types-in-java-java-dsa-course-blog.html#more

Blog 1 : https://javaanddsa.blogspot.com/2022/05/introduction-to-java-java-dsa-course.html


Did you like our Blog4?

            Please tell us in the comment section

Comments

  1. A learned useful functions due to this blog

    ReplyDelete
  2. Time complexity is easy to learn from you

    ReplyDelete

Post a Comment

Popular posts from this blog

LinkedList Problems for Beginners DSA || Java and DSA course || Blog 17

Introduction to Java || Java + DSA course || Blog 1

LinkedList in DSA || Java and DSA course || Blog 16