0
The Use of Arguments in Functions
Im sort of new to coding and i was wondering whats the purpose of arguments in functions? And can you give an example of a type of prgram it would be used in. For example (this is in python) Def Function_Name(x) What is the use of having x in the parenthesis? Thank you!
3 odpowiedzi
- 1
Functions are little sub programs of your program that you can write to reuse code (using the function several times) and making your program more readable (by partitioning into 'chapters', so to say).
Let's say your code is a hospital and your function is a laboratory. How does that work?
You send a blood sample to the lab; the personal there examine it; in the end you get a result.
An argument is like that blood sample. You say to your function: 'Apply whatever you do to this!'
And then with a return statement, the 'lab' returns the 'report'.
EDIT: And I'd really like to know what's going on in the mind of someone who downvotes another post after it obviously helped the OP understand (see that little hook?).
0
I am going to put this in my own words. I am quite sure you can find better answers on the web though.
Functions with parameters are usually written when the output depends on some kind of input. An easy example would be a math function which calculates the factorial of a given number.
Int FactorialOf(int n){
if (n==1){
return 1;
}
else {
return n * FactorialOf(n-1);
}
}
so now you can call this function for different inputs like so:
FactorialOf(5); //returns 120
FactorialOf(3); //returns 6
0
just imagine the power of X in the function we could throw a million names at it, every name dragged out a list as the X passed over to the function and it performs its work, soon have a million names displayed or written to a file etc the defined functions just looking for X to do its work we can tell the computer for every X in the phone book (x being a name) do something and sit back let the functions work as we pass them x from ekswwhere