![]() |
|
||||||
ParametersWe can pass parameters to a function and use them within that function. This is how we do it. Instead of () being empty this time, we define our parameters. We expect two parameters to be passed to this function, which we call a and b We can then return a value, we return the value of a added to b. So we could do: When we call sum = our_sum(2, 3); we pass two values to the function, 2 and 3. The function then handles these as a and b, we add them together and return the result, which we then assign to sum and print out. Old StyleSometimes you may see functions written in the old style. C has evolved over the years but it's important to see what the old style looks like incase you ever read code that uses it, here's our function in old style. It is the same as our function, it executes exactly the same, i just thought you should see it incase you ever come across it. ProgramTo conclude the functions sestion we're going to write a little program. Now we have covered quite a few concepts we can put them together. Let's write a program that let's the user enter three names (maximum 30 chars) and three ages, then print out the combined ages of the people and write the info to a text file named "data.txt". Pretty simple but it will allow us to put a few things together. There we have it, i'm not going to go through and explain the code. If you've been reading the chapters properly you'll be well aware of how that works. It isn't difficult, and it's kind of pointless, though things are getting a little more interesting. Let's go on and discuss pointers with functions.
No Comments for this page. |
|
||||||||||||||||||||||||||||||