What are arguments? I'm pretty sure that every programming language has arguments (right?) but what are they? Are they a specific kind of function, or is it a term that covers a lot of functions?
Arguments
Started by Sionofdarkness, Jul 21 2006 11:39 AM
8 replies to this topic
#1
Posted 21 July 2006 - 11:39 AM
|
|
|
#2
Posted 21 July 2006 - 01:50 PM
An argument is a piece of information that you give to a function to work with. Depending on the argument passed, it may or may not be changed by the function.
#3
Posted 22 July 2006 - 08:35 AM
So could you pass an argument through a lot of functions, and only some of the functions would affect it, while others would not? For some reasons, this reminds me of a catalyst, although it is very different from one.
#4
Posted 23 July 2006 - 06:39 AM
If you have nested function calls, and each one passes it on to the next in the function call, yes. However, you would probably need to send it as changeable all the way down, even if some of the functions don't change it.
#5
Posted 29 July 2006 - 09:20 AM
Now I'm a little more confused. I think I'll just stay away from this topic for awhile.
#6
Posted 29 July 2006 - 11:45 AM
Are you familiar with how functions work?
#7
Posted 21 August 2006 - 11:20 AM
A little. How about giving me one of those examples? Those always seem to help a lot.
#8
Posted 21 August 2006 - 11:38 AM
Sionofdarkness said:
A little. How about giving me one of those examples? Those always seem to help a lot.
void printFunction(String argument){
System.out.println(argument);
}
This is a method which prints out the argument. To call it, another method would have the following code:
String data = "hello world"; printFunction(data);
Here you can see that the code passes in a string that contains "hello world", meaning that when the printFunction method is executed argument="hello world".
The talk of nested function calls isn't really important. it would just be, for example, printFunction having the following code:
void printFunction(String argument){
printWord(argument);
}
In that case the printFunction function is called, and it in turn calls another function 'printWord' with its own argument. So it passes "hello world" onto printWord.
#9
Posted 23 August 2006 - 06:28 PM
Functions in programming work a lot like they do in math, except programming functions get better names that f() or g().
In math, when you say f(x) = 2x, you are defining a rule: for input x, output 2 times x. In programming you have the same idea, except it can be more complicated. Because we have various data types (float, int, etc) you need to be precise about the input and output when you define your function. Also, they can do a lot more complicated stuff.
This programming function f does the same as the math function.
-----
If we look at g(x) = 2 sqrt(x), things get more complicated.
Notice how x gets passed into g, which has to hand it off to sqrt. This chain can happen for multiple levels, one function calling another calling another. If, at any point, one of them says "I'm not allowed to change x", then any changes that functions it calls try to make will be cancelled out when it refuses to pass those changes back.
In math, when you say f(x) = 2x, you are defining a rule: for input x, output 2 times x. In programming you have the same idea, except it can be more complicated. Because we have various data types (float, int, etc) you need to be precise about the input and output when you define your function. Also, they can do a lot more complicated stuff.
float f(float x)
{
float temp;
temp = 2*x;
return temp;
}
This programming function f does the same as the math function.
-----
If we look at g(x) = 2 sqrt(x), things get more complicated.
float g(float x)
{
float temp;
temp = 2*sqrt(x);
return temp;
}
float sqrt(float x)
{
// do something
}
Notice how x gets passed into g, which has to hand it off to sqrt. This chain can happen for multiple levels, one function calling another calling another. If, at any point, one of them says "I'm not allowed to change x", then any changes that functions it calls try to make will be cancelled out when it refuses to pass those changes back.


Sign In
Create Account


Back to top









