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

Post 4 to 6 paragraphs on the topic to the week 4 discussion forum by Wednesday

ID: 3852857 • Letter: P

Question

Post 4 to 6 paragraphs on the topic to the week 4 discussion forum by Wednesday evening.


Discussion Question week 6: Discuss the following topics on methods. Provide examples where necessary to elaborate your thoughts. Select two of these topics to discuss.

What is the purpose of the call stack and how are methods handled with relation to the call stack?

Discuss the scope of variables between Main and called methods. What are the limitations and the advantages of the scope rules? Provide an example.

Research and describe the functions and uses of the random class. Provide an example.

OR

Provide at least three (3) best practices when creating methods in C# and explain why they are "best".

Explanation / Answer

A call stack is mainly used to keep track of the point to which active subroutine should return control when executing. Call stack works as a tool to debug an application when the method to be traced can be called. This forms a better alternative than adding tracing code to all methods that call the given method. Whenever an exception is thrown anywhere in the user code, the Common Language Runtime will unwind the call stack and search for the catch block to determine the specific exception type.

The method-call stack is a data structure that works behind the scenes to support the method call/return mechanism. It also supports the creation, maintenance and destruction of each called method’s local variables.

Variables inside methods are only accessible inside this method, ie an_integer inside the main-method cannot be referenced outside the main method. Variables can even have narrower scopes, for exammple inside loops.

Variables outside methods are called fields. It depends on its access modifier where it can be seen. Private fields for example are only avaiable inside this class, public fields can be accessed from anywhere.

Scope of a variable is the visibility of that variable within the program or within function or block. C allows us to declare variables anywhere in the program. We can declare them whenever necessary. But the problem with this type of declare is that their life span.This is called scope of the variable.

method 1

#include

void main(){

int intNum, intNum1;

int intResult;

intNum = 50;

intNum1 = 130;

intResult = intNum + intNum1;

printf("Sum of two number is : %d", intResult);

}

method 2

#include

void main(){

int intNum1, intNum2;

intNum1 = 50;

intNum2 = 130;

int intResult = intNum1 + intNum2;

printf("Sum of two number is : %d", intResult);

}