473,387 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

About functions

What's the actual use of declaring any function globally
for eg
#include<stdio.h>
int add() //function
void main()
{
}
Oct 5 '08 #1
3 1252
beginers of c wrote:
What's the actual use of declaring any function globally
for eg
#include<stdio.h>
int add() //function
void main()
{
}
So you only have to declare it once, rather than separately in each
subroutine. If you only have one subroutine in the file (as above), then
it doesn't make much difference.
Oct 5 '08 #2
beginers of c said:
What's the actual use of declaring any function globally
for eg
#include<stdio.h>
int add() //function
int add(); /* a function declaration that is not also a
definition requires a terminating semicolon */
void main()
int main(void)
The reason for declaring a function is to give the compiler the information
it needs to do basic type-checking of the return value of that function.
If you make the declaration a full prototype:

int add(void);

or perhaps:

int add(int, int); /* returns the sum of two ints */

then the compiler can type-check the argument expressions as well.

The reason for doing this at file scope is that a declaration at block
scope only applies from the point of the declaration to the end of the
smallest enclosing block. Since functions are very often called from more
than one place, it is convenient to declare them at file scope so that you
only have to declare them once rather than in every function from which
they're called, and there is no real downside to doing things this way, so
you might as well take the win. (There *is* a downside to declaring
*objects* at file scope - if you want to know more about this, just ask.)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 5 '08 #3
On Sun, 5 Oct 2008 11:37:48 -0700 (PDT), beginers of c
<ss*******@gmail.comwrote:
>What's the actual use of declaring any function globally
for eg
#include<stdio.h>
int add() //function
If you don't include the parameter types, not much. If you include
the parameter types (and add the required ;), this becomes a prototype
and the compiler can syntax-check your function calls.
>void main()
And you might as well define main properly if you intend to ask for
help here.
>{
}
--
Remove del for email
Oct 5 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
39
by: Marco Aschwanden | last post by:
Hi I don't have to talk about the beauty of Python and its clear and readable syntax... but there are a few things that striked me while learning Python. I have collected those thoughts. I am...
28
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
51
by: Casper Bang | last post by:
My question is fundamental I beleive but it has been teasing me for a while: I have two classes in my app. The first class is instantiated as a member of my second class. Within this first class,...
4
by: Tony Johansson | last post by:
Hello experts! I'm reading a book about C++ and there is something about inline that the book says that is unclear for me. The book says the following "Because inline functions are expanded at...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
14
by: Alan Silver | last post by:
Hello, I have spent ages trawling through Google, looking for information about global functions in ASP.NET and I'm still not clear about the best way to go about this (or not). I am writing...
4
by: Christoph Scholtes | last post by:
Hi, I have some questions about header files: Say I have a file functions.c which contains a couple of functions. I have declared some structs in this file too. The structs are defined in...
22
by: Neil Gould | last post by:
Or... when is a script not a script? I have several modules for managing different aspects of our club's website, most of which are multi-page. Does setting such things as server.ScriptTimeout...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.