Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

Java Paramaters, Explains it to me.

views
     
TSNamelessNinja
post Jul 4 2016, 10:44 AM, updated 8y ago

Getting Started
**
Junior Member
206 posts

Joined: Jun 2016
From: Planet Earth


Hi guys, I've been writing programs in awhile, the only think I noticed is that, I rarely using parameters/arguments in my code.

Can anyone briefly explain what is a parameter or a argument?
WongGei
post Jul 4 2016, 12:35 PM

Regular
******
Senior Member
1,206 posts

Joined: Dec 2007
From: Kuala Lumpur
Parameter or arguments is the input to a function.
Just like a program, function can be written to behave different base on the input.
Parameter or arguments is the place for one to pass input into the functions.
LuffyPSP
post Jul 12 2016, 09:30 PM

Droid, droid droid droid!
*******
Senior Member
3,771 posts

Joined: Apr 2010
From: Kota Bharu


Its kinda late from the date i figure it could help someone in the future. This is a javascript example but should be the same for Java/PHP or whatever, minus some datatype restrictions and annotations.

**my code probably broken, but it should give you the idea

CODE
//define a function with expected input parameters.
function myFunction(parameter1, parameter2) {
   var a = parameter1;
   var b = parameter2;
   var result = a+b;
   console.log(result);
}



below is like your main method(in Java) but in javascript doesnt matter lah.

suppose you have a return value or arguments, eg: say you have these 2 values,
CODE
var p1 = 5;
var p2 = 2;


then call the function,

CODE
myFunction(p1, p2);
//the above function will execute with value 5 and 2 inserted in the parameter1 and parameter2 arguments


I used to write redundant codes but after i understood how functions took the arguments, i can just call the same function over and over with only 1 line of code.

like,
CODE
public function 1(p1, p2){
myFunction(p1, p2);
}

public function 2(p1,p2){
myFunction(p1, p2);
}


and so on... write once, use how many you want. less lines and cleaner codes.

This post has been edited by LuffyPSP: Jul 12 2016, 09:31 PM
TSNamelessNinja
post Jul 13 2016, 08:30 AM

Getting Started
**
Junior Member
206 posts

Joined: Jun 2016
From: Planet Earth


QUOTE(WongGei @ Jul 4 2016, 12:35 PM)
Parameter or arguments is the input to a function.
Just like a program, function can be written to behave different base on the input.
Parameter or arguments is the place for one to pass input into the functions.
*
thanks!

QUOTE(LuffyPSP @ Jul 12 2016, 09:30 PM)
Its kinda late from the date i figure it could help someone in the future. This is a javascript example but should be the same for Java/PHP or whatever, minus some datatype restrictions and annotations.

**my code probably broken, but it should give you the idea

CODE
//define a function with expected input parameters.
function myFunction(parameter1, parameter2) {
   var a = parameter1;
   var b = parameter2;
   var result = a+b;
   console.log(result);
}

below is like your main method(in Java) but in javascript doesnt matter lah.

suppose you have a return value or arguments, eg: say you have these 2 values,
CODE
var p1 = 5;
var p2 = 2;


then call the function,

CODE
myFunction(p1, p2);
//the above function will execute with value 5 and 2 inserted in the parameter1 and parameter2 arguments


I used to write redundant codes but after i understood how functions took the arguments, i can just call the same function over and over with only 1 line of code.

like,
CODE
public function 1(p1, p2){
myFunction(p1, p2);
}

public function 2(p1,p2){
myFunction(p1, p2);
}


and so on... write once, use how many you want. less lines and cleaner codes.
*
wow, thanks! very detail!

xboxrockers
post Jul 18 2016, 11:37 PM

On my way
****
Senior Member
661 posts

Joined: Dec 2011



Thanks , Me also get to refresh !

 

Change to:
| Lo-Fi Version
0.0138sec    0.53    5 queries    GZIP Disabled
Time is now: 28th March 2024 - 06:39 PM