Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Actual and formal parameters

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Actual and formal parameters

    hi people, just new to this site searching for a bit of help in defining the concepts of actual and formal parameters when declaring and calling methods in program.

    Now ive only started studying java so i honestly dont really understand what it means for a program to 'call' a method. so it would be good if someone could shed some light on that too.

    the piece of code that im trying to analyse here is the following:

    1 public class Scope {
    2 public static void test(int num) {
    3 if (num > 10) {
    4 int half = num / 2;
    5 System.out.println(“Half num = “ + half);
    6 } else {
    7 System.out.println(num);
    8 }
    9 }
    10 public static void main(String[] args)
    11 {
    12 int x = 5;
    13 test(x);
    14 }
    15 }

    as i say im trying to determine the actual and formal parameters used in the definition and call of the method, 'test'

    any help would be appreciated, thanks very much


    what i have at the minute (i have to answer this by reasoning it out)is the following-

    'When declaring your method at the top of a piece of code the header determines if a method returns any result or takes any parameters on. A method should either return a value or no value (known as an action method) and it can take 0 or more parameters.
    Formal parameters are variables that can be declared in the parameter list of a subprogram specification.'

    to be honest im taking a lot of that from the notes my lecturer provided (not directly) so im not so sure what that last bit means.

    again any help will be greatly appreciated

    cheers
    Last edited by javanoobie; December 4th, 2010 at 10:31 AM. Reason: editing so i dont come across as a lazy sod


  2. #2
    Junior Member JJTierney's Avatar
    Join Date
    Dec 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Actual and formal parameters

    Hey mate, i am a first year programmer. But i think i can help you a bit.

    this is your code
    public class Scope 
    {
     
    	public static void test(int num) 
    	{
    		if(num > 10) 
    		{
    			int half = num / 2;
    			System.out.println("Half num = " + half);
    		} 
    		else 
    		{
    			System.out.println(num);
    		}
    	}
     
    	public static void main(String[] args)
    	{
    		int num = 5;
    		test(num);
    	}
    }

    Now Line one (public class Scope) is simply the name of your class.

    Now, this is your method, you have named it test.
    public static void test(int num) 
    	{
    		if(num > 10) 
    		{
    			int half = num / 2;
    			System.out.println("Half num = " + half);
    		} 
    		else 
    		{
    			System.out.println(num);
    		}
    	}

    The first line of your "test" method (public static void test(int num)) you have told it that an int has to be used for this method.
    I am assuming you understand what the if and else statement does, but if not your simply saying, if the number is greater than ten, a new variable called half has to be created. Half must be an int(int half), and half is = to the number entered/2. The System.out.println("Half num = " + half); is telling it what to output.
    the else, means that if the number is not greater than ten, output will be whatever number they have entered.

    public static void main(String[] args)
    	{
    		int x = 5;
    		test(x);
    	}
    }

    This is where you are testing your method. You are creating an int called x to be passed into( public static void test(int num) ). And you are calling the method here -> test(x);. Because you called you method "test" and in the body of the code you have correctly called it.

    I hope this has helped you in some way. Its the best explanation i could come u with. Just to let you know, if int x = 13; or any odd number it will return half of the number below x. As in int x =13; output would be 12.

    If you changed all of your int's to double, it would output decimals.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Actual and formal parameters

    Consider a computer program according to this analogy:

    Everything that can be done on a computer (well, computationally) can be computed using a pencil and some paper. The code is the instructions which tells the person (computer) what to write/erase/compute on the paper.

    So your program will start running on one piece of paper. You can write down variables and values, perform mathematical computations, etc. When you call a method, that's analogous to getting a new piece of paper out, then copying the values of the parameters to that new piece of paper (well, actually either the values or the location of these values is copied, but we won't worry about this for now). The original piece of paper is then set aside, and while you're working on computations for this other method, you can't look at the original piece of paper for variables/values/etc.

    Once you've reached the end of the method (or a return statement), you copy the return value if any back to your original piece of paper. Then all the work you did on the piece of paper for that method is crumpled and tossed into the recycling bin (i.e. you can't look at anything on that paper again).

    While actual computers do things a little differently, the basic concept is the same.

    Actual parameters are what you're passing to the method when you call it. Formal parameters are what the method expects to receive, and it will complain if it doesn't get them.
    public static void main(String[] args) // args is a formal parameter
    {
        System.out.println("Hello world"); // "Hello world" is an actual parameter
    }

  4. #4
    Junior Member
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Actual and formal parameters

    thank you both very much, that cleared it up beautifully. Now having finished that question i'm onto the next one which actually involves creating a program, so you can probably imagine how lost i am with that given my above question. I dont want to come across as though im trying to use this as a homework service because im not, i do actually want to learn because il need this stuff in the years to come, but considering the following text:

    Design a program that will manage the paper consumed by the University
    reprographics department. The program should allow the reprographics manager to
    input the current paper stock level and a paper re-order level, followed by a number
    of paper transactions.

    Each transaction consists of a transaction code (char) and a
    transaction amount (int). The transaction code can be ‘A’ to add paper to stock or ‘R’
    to remove paper from stock.

    The only other valid option is ‘Q’ to quit the program.
    Before removing any paper from stock the program should check that there is
    sufficient stock and if not the program should simply print “** Insufficient Stock **”
    and NOT modify the stock level.

    After each transaction the program should print the stock level, followed by “** Reorder
    **” IF the stock level is below the re-order level. When there are no more
    transactions a ‘Q’ can be entered for the transaction code to signify the end of the
    data.

    Using test data of initial stock 2000, re-order level 500 followed by remove 600,
    remove 1000, remove 500 and finally add 500, the interaction SHOULD look like this
    Welcome to the Paper Stock Manager
    Enter initial stock level: 2000
    Enter re-order level: 500
    Enter option (A, R, Q): R
    Enter amount of paper to remove: 600
    Stock level: 1400
    Enter option (A, R, Q): R
    Enter amount of paper to remove: 1000
    Stock level: 400 ** re-order **
    Enter option (A, R, Q): R
    Enter amount of paper to remove: 500
    ** Insufficient stock **
    Stock level: 400 ** re-order **
    Enter option (A, R, Q): A
    Enter amount of paper to add: 500
    Stock level: 900
    Enter option: A, R, Q: Q
    Thank you for using the Paper Stock Manager
    Marking scheme
    i) Detailed algorithm using pseudo code introduced during module (7)
    ii) Working Java implementation (15) demonstrating use of proper programming
    constructs and elegance/simplicity of design (5)
    iii) Proper program layout /style and use of comments (4)
    iv) Printout screen shot of sample interaction using exact test data outlined above (4)

    as you can see i need to construct an algorithm, and im even stuck at that, i just cant get my head around the idea despite the good two hours i spent yesterday with my pen and relatively blank paper if someone could even point me in the right direction as regards to a simple algorithm i could use for this i might be able to make a start on coding it myself,

    thanks very much

  5. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Actual and formal parameters

    edit: i just cant decide whether i need to set each thing to the numbers specified or whether to leave them to be entered by the user, and if its the latter i honestly dont even know where to start. my notes only cover the very basics of making an algorithm

Similar Threads

  1. New to vectors passing parameters
    By osmaavenro in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 30th, 2010, 04:32 PM
  2. Replies: 6
    Last Post: May 25th, 2010, 02:15 AM
  3. formal Introduction
    By fwashington in forum Member Introductions
    Replies: 1
    Last Post: March 15th, 2010, 09:21 AM
  4. Formal I ntroduction
    By sbliss in forum Member Introductions
    Replies: 5
    Last Post: March 15th, 2010, 09:21 AM

Tags for this Thread