0% found this document useful (0 votes)
13 views

M6 Slides Java

The document discusses methods and data passing in programming. It provides examples of defining methods with different parameters, calling methods from a main function, and overloading methods. Key points include: - Methods allow breaking programs into reusable chunks of code to perform tasks. - Methods are defined with a signature specifying their name and parameters. - Methods can be called from a main function or other methods, perform a task, and return data. - The same method name can be overloaded to support different parameters.

Uploaded by

Imad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

M6 Slides Java

The document discusses methods and data passing in programming. It provides examples of defining methods with different parameters, calling methods from a main function, and overloading methods. Key points include: - Methods allow breaking programs into reusable chunks of code to perform tasks. - Methods are defined with a signature specifying their name and parameters. - Methods can be called from a main function or other methods, perform a task, and return data. - The same method name can be overloaded to support different parameters.

Uploaded by

Imad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Module 6

METHODS AND DATA PASSING

03/06/2021 CSE 1321 MODULE 5 1


Why have functions?
CREATE average, userNum1, userNum2
PRINT (“Please enter the 2 numbers”)
READ userNum1
READ userNum2

average = (userNum1 + userNum2) / 2


// a lot of other code
READ userNum1
READ userNum2
average = (userNum1 + userNum2) / 2
// more code here, then

READ userNum1
READ userNum2
average = (userNum1 + userNum2) / 2
Header/Body Example
// Header is top line
public static int Sum(int num1, int num2)

{ // Begin the body

int sum = 0;
for(int i = num1; i <= num2; i++)
{
sum += i;
}
return sum;

} // End the body

03/06/2021 CSE 1321 MODULE 4 3


Method Signature
// Signature is Sum (int num1, int num2)
public static int Sum(int num1, int num2)
{
int sum = 0;
for(int i = num1; i <= num2; i++)
{
sum += i;
}
return sum;
}

03/06/2021 CSE 1321 MODULE 4 4


Example
(main wakes back up; average sleeps)
class MyClass {
public static void main (String args[ ])
{
int num1, num2, num3;
double result1, result2;
num1 = 5; num2 = 7; num3 = 4;
result1 = average (num1, num2); // 6 sleep
result2 = average (num3, num1); // 4.5
} x y
}
num1 num2 num3
double average (int x, int y) {
5 7 4 return ( (x+y) / 2);
result1 result2 }
6 4.5
Another Quick Example
(function falls asleep; main awake)
class MyClass {
public static void main (String args[ ])
5
{
int num1, num2, num3;
num1 = 5; num2 = 7; num3 = 4;
printNum (num1);
}
}
myNum

num1 num2 num3


void printNum (int myNum) {
5 7 4 PRINT (myNum);
}
Psuedocode - A Bad Solution
(where is the repeated code?) Ps
sum ← 0
FOR (i ← 1 i <= 10 i ← i+1)
sum ← sum + i
ENDFOR
PRINT("Sum from 1 to 10 is “, sum)

sum ← 0
FOR (int i ← 20 i <= 30 i ← i+1)
sum ← sum + i
ENDFOR
PRINT("Sum from 20 to 30 is “, sum)

sum ← 0
for (int i ← 35 i <= 45 i ← i+1)
sum ← sum + i
ENDFOR
PRINT("Sum from 35 to 45 is “, sum)
03/06/2021 CSE 1321 MODULE 5 7
METHOD MAIN
BEGIN
CREATE result ← SUM(arguments: 1,10)
Ps
PRINT("Sum from 1 to 10 is:”, result)

result ← SUM(arguments: 20,30)


PRINT("Sum from 20 to 30 is:”, result)

result ← SUM(arguments: 35,45)


PRINT("Sum from 35 to 45 is:”, result)
END MAIN

METHOD SUM(parameters: num1, num2)


BEGIN
CREATE sum ← 0
FOR (i ← num1, i <= num2, i ← i+1 )
sum ← sum + i
ENDFOR
RETURN sum
END SUM
03/06/2021 CSE 1321 MODULE 5 8
Java Example – Method Sum
public static int Sum(int num1, int num2)
{
int sum = 0;
for(int i = num1; i <= num2; i++)
{
sum += i;
}
return sum;
}
public static void main(String[] args)
{
int result = Sum(1, 10);
System.out.println("Sum from 1 to 10 is " + result);
result = Sum(20, 30);
System.out.println("Sum from 20 to 30 is " + result);
result = Sum(35, 45);
System.out.println("Sum from 35 to 45 is " + result);
}

03/06/2021 CSE 1321 MODULE 5 9


Psuedocode - Method Max Ps
METHOD MAIN
BEGIN
CREATE i ← 5
CREATE j ← 2
CREATE k ← max(i, j)
PRINT("The maximum of ", i , " and ” , j ," is ", k)
END MAIN

METHOD MAX(parameters: num1, num2)


BEGIN
CREATE result
if (num1 > num2)
result ← num1
else
result ← num2

RETURN result
END MAX

03/06/2021 CSE 1321 MODULE 5 10


10
Java – Method Max
public static void main(String[] args)
{
int i = 5, j = 2;
int k = max(i, j);
System.out.println("The maximum of "+i+" and "+j+" is "+k);
}
public static int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}

03/06/2021 CSE 1321 MODULE 5 11


class Main {
public static int math(int x, int y) {
return x+y;
}
public static int math (int x) {
return x*x;
}
public static double math(double x, double y) {
return x*y;
}
public static String math (){
return "Why does this work?";
}
public static void main(String[] args) {
System.out.println(math(7.5, 9.5)); // Prints 71.25
System.out.println (math(4)); // Prints 16
System.out.println (math()); // Prints ”Why does this work?”
}
}

03/06/2021 CSE 1321 MODULE 4 12

You might also like