Which of the following is a correct function definition for adding two integers?

Enhance your programming skills with the RECF Programming Test. Features flashcards and multiple choice questions with hints and explanations. Prepare for success!

Multiple Choice

Which of the following is a correct function definition for adding two integers?

Explanation:
A function that adds two integers must specify a return type of int, have a name, accept two int parameters, and include a body that returns the computed sum. The correct form is: int add(int a, int b) { return a + b; } This matches all parts: it returns an int, is named add, takes two integers, and the body computes a + b and returns that value. The other forms fail for specific reasons: one omits the proper function body structure, which invalidates the function definition; another uses a void return type but tries to return a value, which isn’t allowed; the last uses a different operation and a different name, so it doesn’t implement the requested addition.

A function that adds two integers must specify a return type of int, have a name, accept two int parameters, and include a body that returns the computed sum. The correct form is:

int add(int a, int b) { return a + b; }

This matches all parts: it returns an int, is named add, takes two integers, and the body computes a + b and returns that value.

The other forms fail for specific reasons: one omits the proper function body structure, which invalidates the function definition; another uses a void return type but tries to return a value, which isn’t allowed; the last uses a different operation and a different name, so it doesn’t implement the requested addition.