Your First Program


Collapse Content

Now we will do the same simple problem by coding it. You are again given two variables a and b. You will need to add them together again, but you will also have to return their sum.

To return something, just type return. For example, to return the number 102, you can just type return 102;. Here are some more examples:

  • return a;
  • return 5 - 3;
  • return b + 11;

Instructions

  1. Complete the code below so it returns the sum of a and b.
  2. When you think its correct, click on "Submit" at the bottom to run the code.

The Code

In each programming challenge, you will be given a task. You will be provided with code and need to focus on the part that looks like this:

public static int doStuff(int a, int b){
    //your code here
}

(int a, int b) means you are given two integers a and b as input. You should replace the entire line //your code here with your own code (// marks a comment in code.) Make sure you return something with the keyword return.

Note: In Java, ordinary lines of code need to end with a semi-colon ; so remember to put one at the end of your line of code.

Input/Output Details

In these programming challenge, your code will automatically run on different "test cases" or sets of numbers. You need to return the correct output for each given input of a and b.

You can view some of the input and correct output in the table below, and can compare your results with the correct ones.

Challenge

You are given two variables a and b as input. Can you return the sum of a and b?

(For example, if a is 2 and b is 3, your program should return 5.)

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

  • This bit is rather scary after you finish reading the whole thing. It is however very simple.

  • It looks very hard and difficult but just you wait and see when you apply it

  • correct me if i'm wrong
    "return" has no mathematical function. It is to like "return 5+5" to return the sum of 5+5 to the screen so when the program runs it shows the sum of 5+5 ?
    I know I'm not clear but if someone understands pls reply :P

  • return is used in a method to return a value to the code that called it (see next page). This is similar to how a mathematical function returns a value.

  • It will be easy if you read the article carefully

  • its not as complicated as it looks

All Node Comments
Contact Us
Sign in or email us at [email protected]