Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Discuss the following: (C++) 1.Describe the reasons for breaking down a program

ID: 3862436 • Letter: D

Question

Discuss the following: (C++)

1.Describe the reasons for breaking down a program into functions.

2.Describe the various parts of a function. Give a small example that exhibits usage of all parts of a function.

3.Explain the “Program Output” listed on page 317 of the text book in section 6.5 that explains Passing Data by Value.

4.Explain the “Program Output” listed on page 350 of the text book in section 6.13 that explains Using Reference Variables as parameters.

Note on Question 3 and 4 above: It is very critical that you thoroughly understand the difference between “pass by value” and “pass by reference”.

5.What are Overloaded functions? Why do we need to overload a function? Give an example.

6.What is the lifetime of a Local and a Global Variable? When would you choose to define a variable as global?

7.Can you return a value from a function? Can it be any data type?

Explanation / Answer

1. Programs are broken down into functions for modularity and reusability. A fuction is based on some particular purpose and it makes easier to understand the code. A function can also be called several times from different places in the code. Thus, it can be reused and the same code need not be written multiple times.

2. The parts of a function are-

return type, function name, argument list and function body. For example, consider the function below:

float division(int a, int b)

{

return (float)a/(float)b;

}

Here, return type is float, division is function name, a & b are arguments and the part inside { } is the function body.

3 & 4 does not have required info of page 317.

5. Overloaded functions are multiple functions with the same name but different signatures. A function signature is it's return type and arguments. Overloaded functions have the same name, but vary in their return types and argument list or types.

Overloaded functions are used to perform similar tasks but with minor variations. For example, addition can be done on integers as well as float. So the addition function can be overloaded to have either int or float arguments. The programmers using a function need not to worry of the return type or arguments as the appropriate instance of the overloaded function is automatically invoked based on the type and number of arguments.

6. The lifetime of a local variable is till the execution of a function invocation completes. The lifetime of a global variable is till the program executes completely. Global variables are generally used to define constant values that are used throughout the program and does not change.

7. Functions can return any data types including objects, pointers.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote