Skip to playerSkip to main content
  • 4 months ago
Sigma batch
Transcript
00:00Now let's discuss another syntax of the functions in which there are parameters
00:06Parameters are our function's input
00:09How did we decide the output? We decided the output from a return type
00:14In this function, there is a thing called parameters that tells us what should be input
00:20In this syntax, first we write the return type function
00:23Then we write the name function
00:25And we write all the parameters that we want to take into the function as input
00:30So here, this parenthesis and this parenthesis are our function's input
00:37Then we write our function's body
00:39In which we write all our code and logic
00:42And in the last, we have a return statement
00:44Return statement can be changed depending on how our function's return type
00:48Let's discuss a sample program
00:51What do we want to do? We want to make a function
00:54In the function, we are going to give input a and a number b
00:58And we are going to output sum of a, b
01:02How do we write this code?
01:04First we write our code in the main function
01:06What do we want to do? We will make a scanner class
01:09System.in
01:12We will import our package
01:15Java.util.all
01:19Now what do we want to do?
01:20First we will input our first number
01:23Then we will use second number
01:25Then we are going to calculate
01:27The sum of a variable
01:29Int sum is equal to a plus b
01:31And what do we do?
01:33Finally we will print this sum
01:35Sum is sum
01:37Now what is the whole work
01:39We have to shift this work
01:41We have to shift this function
01:43So let's write a function
01:45Also, how many functions in a class
01:47We can make a function of a print hello world
01:49We will make a function of a sum
01:51Now we can make a function of multiple functions
01:53In a single class
01:55First we will write public static
01:57First we will write public static
01:59Now when we start this function
02:01We will keep the return type
02:02We will keep the return type
02:03That we are not doing anything
02:04And we will give it the name
02:05Calculate sum
02:08In the sum
02:09In the sum
02:10We will not send anything to input
02:11What do we do?
02:12We will take the whole code
02:14And take it here
02:16And what will we do?
02:18Main function simply call
02:20And what will we do?
02:22Main function simply call
02:24Calculate sum
02:25Calculate sum
02:27Now let's run this code
02:29The main function has called
02:31Calculate sum
02:32We are here
02:33In this Calculate sum
02:35It has made a scanner class
02:37So first we need a first number
02:39A
02:40We will pass 5
02:41Then we need a second number
02:42B
02:43We will pass 10
02:44And then we will have
02:46Calculate sum
02:47Which is going to be 15
02:49Now we will change some changes
02:51What changes are
02:53This scanner
02:54And this input
02:55We will shift the same
02:56In the main function
02:58In the main function
02:59The main function will scan
03:00The main function will see
03:01A and B
03:02But this A and B
03:04How will reach this sum function?
03:05They will reach the parameters
03:07Where do we write the parameters?
03:09We will write the parameters
03:10In the parentheses
03:11In the parentheses
03:12First we will write our first parameter
03:14Its type
03:15Int
03:16And name A
03:17Then our second parameter
03:18Int B
03:19Int
03:20Int
03:21So
03:22So our first parameter is int a, second parameter is int b
03:27These two are in our calculate sum function
03:30If I remove them, the parameters from here
03:34What will happen? There will be an error
03:36Why will it happen? Because this function doesn't know what a and b
03:40Then I will know when I pass these two a and b
03:43I will pass these parameters to this function
03:45If I name number 1 or number 2
03:51So my sum will calculate for number 1 and number 2
03:57So I have written a definition of a function
03:59Which calculates the sum
04:01These two numbers will take number 1 and number 2
04:02They will calculate the sum and print it in the print
04:05What do I have to do in the main function?
04:08I have taken a and b
04:10Now I have to call the calculate sum
04:12But with the calculate sum, I have to pass my two integer values
04:17Because the calculate sum requires two numbers
04:20So what will I do? I will pass a first and then b
04:23Let's save and run again
04:25So first number is 5, second number is 10 and sum is 15
04:31So what did I do? I have passed my numbers in this function
04:36How did I pass it?
04:37I have to pass it in parenthesis
04:39First number is 1st number and then second number
04:41And in my function
04:43I received those numbers as parameters
04:45And then I have removed the sum and print it in the sum
04:49Now this is the work of the sum to print it
04:51I can do this in the main function
04:53How can I do this?
04:55So what do I do here?
04:57I will write int sum is equal to calculate sum
05:02And this function I will return type and write int
05:05And here I will return sum
05:09So what did I do?
05:10I have made a function that calculates sum
05:13And now I have made a function of number 1 and number 2
05:15Which also comes in the input
05:16And the sum is calculated and returned
05:19So I have returned sum
05:21Who did I return?
05:22Who did I call it?
05:23Who did I call it?
05:24The main function has called it
05:25That's why the calculate sum
05:27The return type of sum is int
05:29Because it is a integer value sum
05:31How did it return?
05:33How did it return?
05:34It returned from this statement
05:36And from this assignment operator
05:38Now I can store it somewhere
05:40So I have sum name the other variable
05:43This sum variable
05:45And this sum variable
05:46Both are different
05:47Because these are different functions
05:49We will study scopes
05:51In this chapter
05:53We will understand the details
05:55How these variables are different
05:57They are different
05:58Same variable
05:59So I have stored this variable
06:01And then I have done that sum
06:03Let's save it
06:05And run
06:07So first number 5
06:08Second number 10
06:10So I have printed sum is 15
06:12And how did I do this?
06:13I have simply done these 3 lines
06:15Where there is a function
06:16Which takes some input
06:18Some work
06:19And then
06:20It returns some output
06:22And then
06:23And then
06:24Also
06:26So
Be the first to comment
Add your comment

Recommended