Function returning a value Learn programming Java

Lesson:

Functions


Exercise:

Function returning a value


Objetive:

Create a program whose Main must be like this:

public static void Main()
{
int x= 3;
int y = 5;
Console.WriteLine( Sum(x,y) );
}

"Sum" is a function that you must define and that will be called from inside Main. As you can see in the example, it must accept two integers as parameters, and it must return an integer number (the sum of those two numbers).


Code:

public class Main
{
	public static int Sum(int a, int b)
	{
		int result;
		result = a + b;
		return result;
	}
	public static void main(String[] args)
	{
		int x = 3;
		int y = 5;
		System.out.println(Sum(x, y));
	}
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.