Win a copy of Kotlin in Action this week in the Kotlin forum!
    Bookmark Topic Watch Topic
  • New Topic

Scope and Parameters in a program  RSS feed

 
irish kelly
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Report post to moderator
Can someone help me in finding 1) the line numbers were variable scope is visible and 2) the actual and formal parameters in the program.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hello and welcome to the Ranch! Please UseCodeTags (<- click) when posting your code. With code tags and proper indentation, your code will be easier to read.

1)
I don't know what's the variable you're asking for, so I will comment all of them in terms of their scope:
  • num is visible throughout entire test method.
  • half is visible inside the if-block, where it is declared, and not the else-block or the rest of test.
  • x is visible throughout entire main method.


  • As you can see, variables declared within a block of code (local variables for the block), are accessible only within the block in which they are declared. Local variable continues to exist until the end of the block in which it is declared, including all the blocks nested within the block containing the declaration. Here is another example:


    2)
    What you refer to as formal parameter is actually commonly known as a parameter (at least in the books I've read). A parameter has a name and a type and appears in the parameters list in the definition of the method. A parameter actually defines a type of the value that can be passed to the method when it is invoked. In your case it's num.
    On the other hand actual parameter, or an argument is an actual value that is passed to the method when it is invoked. The value of the argument is referenced by the parameter name during method's execution. In your case it's x.
     
    irish kelly
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Thank You

    Also I really need a hand, I have to write the java for the following; the user enters a number and the integers of that number are added together, for example I enter "234" and the programs produces "2+3+4=9"

    Really grateful for any help received.
     
    Kemal Sokolovic
    Bartender
    Posts: 825
    5
    Java Python Ruby
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Have you tried anything so far? What do you think, how could that be done?
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12542
    48
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Report post to moderator
    all programming is done the same way. You break the problem down into very simple parts, and work on ONE at a time.

    For example, This program requires you to to at LEAST four things:

    1) Get input from a user
    2) convert a number into into individual digits.
    3) Sum a bunch of numbers
    4) print out a number

    It is key to realize that each and every one of those can - and more importantly should - be done separately from the others.

    So, work on one at a time. Only when you have gotten whichever one you do first to work should you start to think about working on the second.
     
    irish kelly
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Im done the code but I have a problem.... instead of adding all the integers and returning the answer, the program instead returns the last intetger as the answer.

     
    Punit Jain
    Ranch Hand
    Posts: 1085
    3
    • Mark post as helpful
    • send pies
    • Report post to moderator

    while (number >=100)
    {
    counter=number%10;
    sum = sum+counter;
    number=number/10;
    }

    Trace your code and check your condition in while loop.

     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16027
    87
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Report post to moderator
    You've also asked that new question here - let's discuss this new problem in that other topic.
     
      Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!