473,654 Members | 3,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

We could omit curly brackets in function declaration?

STF
While reading the C++ tutorial in this page:
http://www.cplusplus.com/doc/tutorial/tut2-2.html

I'm astonished to learn that we could omit curly brackets in function
declaration for single instruction as is written in the paragraph:
"statement is the function's body. It can be a single instruction or a
block of instructions. In the latter case it must be delimited by curly
brackets {}."

But I'm not sure if this is true. At least with GCC under Linux
and MinGW used within Dev-C++ for Windows, I'm unable to compile a file
having something like this:

int test123(int a)
return a;

So, could someone tell me if what's written in that webpage is
correct or not.

TIA

STF

Jul 23 '05 #1
6 6643
STF wrote:
While reading the C++ tutorial in this page:
http://www.cplusplus.com/doc/tutorial/tut2-2.html

I'm astonished to learn that we could omit curly brackets in function
declaration
...definition..
for single instruction as is written in the paragraph:
"statement is the function's body. It can be a single instruction or a
block of instructions. In the latter case it must be delimited by curly
Notice the "delimited" . Not "surrounded ".
brackets {}."

But I'm not sure if this is true. At least with GCC under Linux
and MinGW used within Dev-C++ for Windows, I'm unable to compile a file
having something like this:

int test123(int a)
return a;

So, could someone tell me if what's written in that webpage is
correct or not.


The wording is just bad. The function body must be _surrounded_ by the
curly braces. It's a requirement of the C++ syntax. In the Standard the
"function body" is defined as a "compound statement", which means it has
to have curly braces around it.

What the author of that tutorial meant by "delimited by curly brackets"
is a puzzle.

V
Jul 23 '05 #2
STF wrote:
I'm astonished to learn that we could omit curly brackets in function
declaration for single instruction as is written in the paragraph:
"statement is the function's body. It can be a single instruction or a
block of instructions. In the latter case it must be delimited by curly
brackets {}."

It's not true. The stuff after the parameters in the function
definition is not syntacitally "statement" . It is "compound-statement".
A compound statement is a pair of braces with zero or more other
statements inside.

That tutorial seems to have a number of problems.
Jul 23 '05 #3
ben
The wording is just bad. The function body must be _surrounded_ by the
curly braces. It's a requirement of the C++ syntax. In the Standard the
"function body" is defined as a "compound statement", which means it has
to have curly braces around it.


Something like this should work:

int f(void)
try
{
//...

return 1;
}
catch (..)
{
//...
return 0;
}

Regards,
ben
Jul 23 '05 #4
ben wrote:
The wording is just bad. The function body must be _surrounded_ by the
curly braces. It's a requirement of the C++ syntax. In the Standard the
"function body" is defined as a "compound statement", which means it has
to have curly braces around it.

Something like this should work:

int f(void)
try
{
//...

return 1;
}
catch (..)
{
//...
return 0;
}


And note that try needs a compound statement as well.
Jul 23 '05 #5


Victor Bazarov wrote:
STF wrote:
While reading the C++ tutorial in this page:
http://www.cplusplus.com/doc/tutorial/tut2-2.html

I'm astonished to learn that we could omit curly brackets in function
declaration


..definition..
> for single instruction as is written in the paragraph:
"statement is the function's body. It can be a single instruction or a
block of instructions. In the latter case it must be delimited by curly


Notice the "delimited" . Not "surrounded ".
brackets {}."

But I'm not sure if this is true. At least with GCC under Linux
and MinGW used within Dev-C++ for Windows, I'm unable to compile a file
having something like this:

int test123(int a)
return a;

So, could someone tell me if what's written in that webpage is
correct or not.


The wording is just bad. The function body must be _surrounded_ by the
curly braces. It's a requirement of the C++ syntax. In the Standard the
"function body" is defined as a "compound statement", which means it has
to have curly braces around it.

What the author of that tutorial meant by "delimited by curly brackets"
is a puzzle.

V


Jul 23 '05 #6
Indeed! I've tried the following code with MinGW

int f(void)
try { return 1; }
catch (char * str) { return 0; }

and it compiles but not

int test123(int a)
return a;

Jul 23 '05 #7

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

Similar topics

21
12945
by: deko | last post by:
Do I need to use curly brackets in PHP if .. else statements? other constructs? Does it matter? What are Best Practices? Why? thanks in advance... This seems to work WITHOUT curly brackets: if(ereg("Win", getenv("HTTP_USER_AGENT"))) $visos = "Windows"; elseif((ereg("Mac", getenv("HTTP_USER_AGENT"))) || (ereg("PPC",
8
6834
by: Ken in Melbourne Australia | last post by:
If I use the curly bracket syntax (referred to as the complex syntax) within a string, how do I get to call a function within it? The php manual says that the first (or previous) character for the curly bracket has to be a dollar sign '$'. This is fine for variables, arrays and some objects but doesn't allow me to call a function such as addslashes() or trim() before I return the string in the variable.
1
1906
by: inquirydog | last post by:
Hello- I, the inquirydog have a question: Using the AVT mechanism (curly brackets in an attribute value) is a great way to decrease the size of your xml document and make it more readable. Can one also include the results of a call-template within curly brackets? Ideally it would be great if you could just call the template as a function: <tagname attributename="{mytemplate('abcd')}">
1
3928
by: matro | last post by:
hi there, I have the following struct in my class: class CTbStDialog : public CDialog { public: int fooVar, fooVar2; ... //other foo variables...
4
8335
by: lwoods | last post by:
In PHP you can do this: $x="test"; $y=$x{2}; echo $y; ....will display "s". Where in the PHP doc is this type of behavior for "curly brackets" defined?
2
5015
by: Zain Homer | last post by:
Someone please let me know if I'm sending this to the wrong email alias... I'm wondering why we can't use the glob module to glob with curly brackets like we can from the command line (at least in tcsh). Is there a shell standard for which the python glob module has been designed which prevents the implimentation of the {}'s? For example:
3
5360
by: Grande News | last post by:
Hi, I've got some PHP code that looks like this $cura= ord($data{$pnum*2}); $curb = ord($data{$pnum*2+1}); Notice the curly braces -- does using them make any difference, or will square brackets do the same thing? Thanks B
0
8375
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7306
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.