Search
Left Quote    By failing to prepare, you are preparing to fail.
- Benjamin Franklin    
Right Quote
 
[login] | [Register]
 

C Tutorial

by: Jester
Page: 9 of 10
(View All)

Parameters


We 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 Style

Sometimes 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.


Program

To 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.



1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  10  |  
Next »


No Comments for this page.

You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

27,406 Views
0 Total Comments
3.33 Rating of 5 (3 Votes)

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

· Beginning Visual C++ 6.0
Your Article Here



"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank