Jump to content

Arguments

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
Sionofdarkness

Sionofdarkness

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 384 posts
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?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Sionofdarkness

Sionofdarkness

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 384 posts
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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Sionofdarkness

Sionofdarkness

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 384 posts
Now I'm a little more confused. I think I'll just stay away from this topic for awhile.

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Are you familiar with how functions work?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
Sionofdarkness

Sionofdarkness

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 384 posts
A little. How about giving me one of those examples? Those always seem to help a lot.

#8
amac

amac

    Newbie

  • Members
  • Pip
  • 8 posts

Sionofdarkness said:

A little. How about giving me one of those examples? Those always seem to help a lot.
Arguments are what you give to functions. For example

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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.

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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog