Wednesday, June 15, 2005

 

Functions in C

Almost all programming languages have some equivalent of the function. You may have met them under the alternative names subroutine or procedure.

Some languages distinguish between functions which return variables and those which don't. C assumes that every function will return a value. If the programmer wants a return value, this is achieved using the return statement. If no return value is required, none should be used when calling the function.

Here is a function which raises a double to the power of an unsigned, and returns the result.

double power(double val, unsigned pow)
{ double ret_val = 1.0;
unsigned i;
for(i = 0; i < pow; i++)
ret_val *= val;
return(ret_val);
}


The function follows a simple algorithm, multiplying the value by itself pow times. A for loop is used to control the number of multiplications, and variable ret_val stores the value to be returned. Careful programming has ensured that the boundary condition is correct too. ie .
Let us examine the details of this function.

double power(double val, unsigned pow) This line begins the function definition. It tells us the type of the return value, the name of the function, and a list of arguments used by the function. The arguments and their types are enclosed in brackets, each pair separated by commas.
The body of the function is bounded by a set of curly brackets. Any variables declared here will be treated as local unless specifically declared as static or extern types.

return(ret_val); On reaching a return statement, control of the program returns to the calling function. The bracketed value is the value which is returned from the function. If the final closing curly bracket is reached before any return value, then the function will return automatically, any return value will then be meaningless.

The example function can be called by a line in another function which looks like this
result = power(val, pow);This calls the function power assigning the return value to variable result.

Here is an example of a function which does not return a value.

void error_line(int line)
{ fprintf(stderr, "Error in input data: line %d\n", line);
}


The definition uses type void which is optional. It shows that no return value is used. Otherwise the function is much the same as the previous example, except that there is no return statement. Some void type functions might use return, but only to force an early exit from the function, and not to return any value. This is rather like using break to jump out of a loop.
This function also demonstrates a new feature.

fprintf(stderr, "Error in input data: line %d\n", line); This is a variant on the printf statement, fprintf sends its output into a file. In this case, the file is stderr. stderr is a special UNIX file which serves as the channel for error messages. It is usually connected to the console of the computer system, so this is a good way to display error messages from your programs. Messages sent to stderr will appear on screen even if the normal output of the program has been redirected to a file or a printer.

The function would be called as follows

error_line(line_number);

Comments:
Captivating blog. I love surfing the web for the
type of blogs that you do. It had me on the edge of my
seat and I kept going back to again and again!
I know that you love my work so, look up my plastic surgery tummy tuck blog.
 
Excellent blog. It was so great and I bet I will
go back to it! I get to look online for blogs like
yours is a blessing.
I can't explain, but you need to check my arizona plastic surgery blog!
 
Striking blog. I liked the site I will be back
again! Websurfing is a good way to find blogs like
yours.
Once you sign on, check for my plastic surgery denver blog.
 
Prodigious blog. Loved it so much I went to it
again! Just go online and search for blogs that are
worth the value as yours.
Please come by and see my arizona center plastic surgery tucson az blog.
 
Incredible blog. I admired your site and I will be
back once again to view it! I use much of my spare
time searching for blogs like yours.
Hey son, you need to check out my atlanta plastic surgery blog!
 
Excellent blog. It was so great and I bet I will
go back to it! I get to look online for blogs like
yours is a blessing.
Please discover my plastic surgery breast augmentation blog.
 
Exciting blog. Your site was amazing and will be
back again! I never get tired of looking for blogs
just like this one.
Look into my plastic surgery for man blog.
 
Fine blog. I found your site suitable for another
visit! And when I'm able to surf the web, I look for
blogs as great as your work.
Stop by and look at my good plastic surgery blog site.
 
Excellent blog. Your site was great and will be
finding it again!  I surf the net for blogs like
yours.
If your look to uncover information, please visit my priscilla presley plastic surgery blog
 
Super blog. I enjoyed the site and when I have the
time, shall visit the site again. Finding blogs this
good on the internet.
Where you been? You have got to look at my bad credit plastic surgery financing blog!
 
Suitable blog, its very good. I liked the site its
from so much I have to visit it again! I surf the web
for blogs like yours in my spare time.
Stop by and look at my plastic surgery photo blog site.
 
Hot blog. When I get to seraching on the web for
blogs, its got to be like yours! And the site is off
the hook! I just kept coming back!
Please come by and see my plastic surgery statistics blog.
 
Super blog. I enjoyed the site and when I have the
time, shall visit the site again. Finding blogs this
good on the internet.
I was in love with your pro and cons of plastic surgery blog site.
 
Fine blog. I found your site suitable for another
visit! And when I'm able to surf the web, I look for
blogs as great as your work.
Stop by and visiit my risk of plastic surgery blog!
 
I took pleasure in the site and I will go back!
Surfing online for blogs like this one is worth my
time. Sensational blog.
I want you to stop and compare with my plastic surgery detroit blog.
 
Hot blog. When I get to seraching on the web for
blogs, its got to be like yours! And the site is off
the hook! I just kept coming back!
I wish I was like you, but I'll go and peep your plastic surgery nj blog.
 
I took pleasure in the site and I will go back!
Surfing online for blogs like this one is worth my
time. Sensational blog.
Everyday of the month you need to peep my bad credit plastic surgery financing blog.
 
Unbelievable blog. I can hardly wait to vist this
site again.I'm consistently looking up blogs like
this.
I hope you can look through my awful plastic surgery blog.
 
I took pleasure in the site and I will go back!
Surfing online for blogs like this one is worth my
time. Sensational blog.
Please consider looking at my plastic surgery st louis blog.
 
Exciting blog. The site out did itself and will be
back! I love surfing the internet for blogs that are
exactly like this blog.
Check out my awful plastic surgery blog, you won't be sorry!
 
Striking blog. I liked the site I will be back
again! Websurfing is a good way to find blogs like
yours.
Hey playa! You need to check out my lil kim plastic surgery blog.
 
Exciting blog. Your site was amazing and will be
back again! I never get tired of looking for blogs
just like this one.
My good plastic surgery blog, is something you need to peep out!
 
Excellent blog.  I go though the web in search of
blogs like this one. Its so good, that I plan on
returning to its site!
Come as you are and look at my plastic surgery st louis blog.
 
Great blog.  I just like the site and I will get a
visit again! In my spare time I usually try and look
for blogs just as neat as yours.
I was in love with your long island plastic surgery blog site.
 
Hype blog. And I admire your site and plan on
returning to it! When I web surf it always helps me to
find great blogs.
Please proceed to my plastic surgery nj blog when you find the time.
 
Unbelievable blog. I can hardly wait to vist this
site again.I'm consistently looking up blogs like
this.
Please discover my plastic surgery fort worth blog.
 
Captivating blog. I love surfing the web for the
type of blogs that you do. It had me on the edge of my
seat and I kept going back to again and again!
I wish I was like you, but I'll go and peep your good plastic surgery blog.
 
Exciting blog. Your site was amazing and will be
back again! I never get tired of looking for blogs
just like this one.
Search for my plastic surgery san diego blog, it will leave you speechless.
 
Great blog.  I just like the site and I will get a
visit again! In my spare time I usually try and look
for blogs just as neat as yours.
Please proceed to my newport beach plastic surgery blog when you find the time.
 
Unique blog my friend, I can hardly wait to vist
this site again. I just worship the site its comes
from! Believe me in my extra time I'm consistently
looking up blogs like this.
Check out the blog site with my plastic surgery cost in it!
 
Super blog. I web surf when I have the time for
blogs like this one.Your site was nice and will be
visited again!
No matter when you are, just stop by and check for my male plastic surgery blog site.
 
Exciting blog. Your site was amazing and will be
back again! I never get tired of looking for blogs
just like this one.
I hope you had a chance to check out my plastic surgery st louis blog.
 
Delightful blog. I devote my spare time just
looking for great blogs such as yours. I treasure this
site and will go back!
It may look like it was hard work, but my atlanta plastic surgery blog was simple.
 
Hype blog. And I admire your site and plan on
returning to it! When I web surf it always helps me to
find great blogs.
Please consider looking at my plastic surgery minneapolis blog.
 
Unbelievable blog. I can hardly wait to vist this
site again.I'm consistently looking up blogs like
this.
Sweetie, go and search my plastic surgery georgia blog for what you need.
 
Fascinating blog. I loved the site you did a good
job on it, I will be back! I surf the net for blog
like this one.
Want to see top notch work, peep my plastic surgery st louis blog site for the bomb work!
 
Enchanting blog. Your site was off the chain and I
will return! I peep the web for blogs just like this
one.
Hey why don't you peep my demi moore plastic surgery blog site.
 
Hot blog. When I get to seraching on the web for
blogs, its got to be like yours! And the site is off
the hook! I just kept coming back!
Check out my blog, please!
 
Excellent blog. Your site was great and will be
finding it again!  I surf the net for blogs like
yours.
Hey why don't you peep my plastic surgery breast implant blog site.
 
Unique blog my friend, I can hardly wait to vist
this site again. I just worship the site its comes
from! Believe me in my extra time I'm consistently
looking up blogs like this.
Please take a journey to my apply online for credit card blog.
 
I took pleasure in the site and I will go back!
Surfing online for blogs like this one is worth my
time. Sensational blog.
Everyday of the month you need to peep my the las vegas center for plastic surgery blog.
 
Terrific blog. I search the internet everytime I
get a moment to find blogs. Its better than cold iced
tea and I have to visit it one more time!
Please examine my plastic surgery before and after picture blog as soon as you can.
 
Creative blog.  I thought it was the best site out
and we had to go back to it! Every chance I get on the
net I search for blogs just like your site.
I wish I was like you, but I'll go and peep your charlotte plastic surgery blog.
 
Awesome blog. I'm loving the site and I have to
return there soon! Going online, when I have the time,
I look for blogs close to your work!
Hey son, you need to check out my plastic surgery florida blog!
 
Exciting blog. The site out did itself and will be
back! I love surfing the internet for blogs that are
exactly like this blog.
Oh please, check for the blog site with my plastic surgery minneapolis!
 
Creative blog. I just kept looking at it over and
over! Im always looking for blogs like this!
I will give you access to jump to my arizona plastic surgery blog.
 
Creative blog.  I thought it was the best site out
and we had to go back to it! Every chance I get on the
net I search for blogs just like your site.
Search for my plastic surgery statistics blog, it will leave you speechless.
 
Striking blog. I liked the site I will be back
again! Websurfing is a good way to find blogs like
yours.
In an efford of finding the right info, check for my plastic surgery before and after picture blog site.
 
Energizing blog. It blew me away and I loved your
site. when I have the time to surf the net, i try
finding blogs as good as your site.
Please take a journey to my plastic surgery texas blog.
 
Remarkable blog. I take the neccesary time to find
blogs that are just as good as the ones you do.
Check out my plastic surgery miami blog, you won't be sorry!
 
Incredible blog. I admired your site and I will be
back once again to view it! I use much of my spare
time searching for blogs like yours.
Stop by and look at my plastic surgery new york blog site.
 
Fine blog. I found your site suitable for another
visit! And when I'm able to surf the web, I look for
blogs as great as your work.
Hey son, you need to check out my american board of plastic surgery blog!
 
Creative blog. I just kept looking at it over and
over! Im always looking for blogs like this!
Search for my priscilla presley plastic surgery blog, please!
 
Extraordinary blog. Your site was hip and fresh
and we'll visit it again! I love surfing the internet
for blogs.
Everyday of the month you need to peep my plastic surgery texas blog.
 
Unusual blog. I liked the site its from so much I
have to visit it again! I surf the web for blogs like
yours in my spare time.
I hope you had a chance to check out my costa rica plastic surgery blog.
 
Enchanting blog. Your site was off the chain and I
will return! I peep the web for blogs just like this
one.
Please take a journey to my hollywood plastic surgery blog.
 
Energizing blog. It blew me away and I loved your
site. when I have the time to surf the net, i try
finding blogs as good as your site.
In my spare time I will look for your instant credit card blog.
 
Remarkable blog. I take the neccesary time to find
blogs that are just as good as the ones you do.
I know that you love my work so, look up my plastic surgery loan blog.
 
Prodigious blog. Loved it so much I went to it
again! Just go online and search for blogs that are
worth the value as yours.
Please come by and see my plastic surgery blog.
 
I peep the web for blogs just like this one.
Airtight blog. Your site was off the chain and I will
return!
Look who checking out my free credit card blog?
 
Excellent blog. Your site was great and will be
finding it again!  I surf the net for blogs like
yours.
Look who checking out my plastic surgery san diego blog?
 
Great blog. I'm always finding blog like yours. It
got my attention and I will go to the site again!
I hope you had a chance to check out my vaginal plastic surgery blog.
 
Exciting blog. Your site was amazing and will be
back again! I never get tired of looking for blogs
just like this one.
Once you sign on, check for my plastic surgery blog.
 
Unbelievable blog. I can hardly wait to vist this
site again.I'm consistently looking up blogs like
this.
Check out my bad celebrity plastic surgery picture blog, you won't be sorry!
 
Great blog.  I just like the site and I will get a
visit again! In my spare time I usually try and look
for blogs just as neat as yours.
In an efford of finding the right info, check for my plastic surgery center blog site.
 
Unusual blog. I liked the site its from so much I
have to visit it again! I surf the web for blogs like
yours in my spare time.
Hey why don't you peep my cosmetic plastic surgery blog site.
 
Super blog. I enjoyed the site and when I have the
time, shall visit the site again. Finding blogs this
good on the internet.
I hope you had a chance to check out my risk of plastic surgery blog.
 
Sensational blog. I took pleasure in the site and I
will go back! Surfing online for blogs like this one
is worth my time.
Please go over my vaginal plastic surgery blog.
 
Nice blog. I seen the site and I adored the work,
that I want to visit it more each day! I like
searching for blogs that have the same content like
this one!
Once you sign on, check for my plastic surgery mexico blog.
 
I peep the web for blogs just like this one.
Airtight blog. Your site was off the chain and I will
return!
Check out the blog site with my plastic surgery fort worth in it!
 
I took pleasure in the site and I will go back!
Surfing online for blogs like this one is worth my
time. Sensational blog.
I beg of you, just check out my plastic surgery specialist blog.
 
Unique blog my friend, I can hardly wait to vist
this site again. I just worship the site its comes
from! Believe me in my extra time I'm consistently
looking up blogs like this.
Please come by and see my plastic surgery center blog.
 
Exciting blog. Your site was amazing and will be
back again! I never get tired of looking for blogs
just like this one.
In my spare time I will look for your cosmetic plastic surgery blog.
 
Charming blog. Your site was off the chain and I
will return! When I get the time I look for blog like
this one.
I want you to stop and compare with my celebrity plastic surgery blog.
 
Terrific blog. I search the internet everytime I
get a moment to find blogs. Its better than cold iced
tea and I have to visit it one more time!
I want you to stop and compare with my plastic surgery san francisco blog.
 
Great blog. I'm always finding blog like yours. It
got my attention and I will go to the site again!
I can't explain, but you need to check my plastic surgery for man blog!
 
Enchanting blog. Your site was off the chain and I
will return! I peep the web for blogs just like this
one.
I wish I was like you, but I'll go and peep your teen and plastic surgery blog.
 
Captivate blog. I surf the web for blogs this
nature.The site are wonderful and will be returned to
again!
Click on my plastic surgery houston blog before its to late.
 
Remarkable blog. I take the neccesary time to find
blogs that are just as good as the ones you do.
I hope you can look through my picture of plastic surgery gone wrong blog.
 
Awesome blog. I'm loving the site and I have to
return there soon! Going online, when I have the time,
I look for blogs close to your work!
My plastic surgery kansas city blog, is something you need to peep out!
 
Exciting blog. The site out did itself and will be
back! I love surfing the internet for blogs that are
exactly like this blog.
I will give you access to jump to my plastic surgery breast enlargement blog.
 
Creative blog.  I thought it was the best site out
and we had to go back to it! Every chance I get on the
net I search for blogs just like your site.
Go and find my plastic surgery georgia blog.
 
Awesome blog. I'm loving the site and I have to
return there soon! Going online, when I have the time,
I look for blogs close to your work!
In my spare time I will look for your plastic surgery baltimore blog.
 
Striking blog. I liked the site I will be back
again! Websurfing is a good way to find blogs like
yours.
You got me! I will check out your plastic surgery st louis blog a.s.a.p!
 
Astonshing blog. I relished in the site and you
know I will be going to it again! Surfing the internet
hepls me to find blogs that arfe just as good.
Jump into my plastic surgery gone bad blog.
 
Irresistible blog. I look for blogs like this one
when time allows me to. I enjoyed the site and I'll
check it next time!
I will give you access to jump to my plastic surgery st louis blog.
 
Exciting blog. The site out did itself and will be
back! I love surfing the internet for blogs that are
exactly like this blog.
Go by and hit my plastic surgery prices blog, you'll be glad you did.
 
Fascinating blog. I loved the site you did a good
job on it, I will be back! I surf the net for blog
like this one.
Please consider looking at my newport beach plastic surgery blog.
 
Fine blog. I found your site suitable for another
visit! And when I'm able to surf the web, I look for
blogs as great as your work.
Hey son, you need to check out my plastic surgery san francisco blog!
 
Creative blog.  I thought it was the best site out
and we had to go back to it! Every chance I get on the
net I search for blogs just like your site.
Stop by and visiit my plastic surgery center blog!
 
Fine blog. I found your site suitable for another
visit! And when I'm able to surf the web, I look for
blogs as great as your work.
Go by and hit my bad celebrity plastic surgery picture blog, you'll be glad you did.
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?