473,388 Members | 1,399 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,388 software developers and data experts.

Suggest a better code

let there be a function
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int add(int x, int y) or int add(x,y)
  3. main()
  4. {
  5. int x,y,sum;
  6. sum=add(x,y);
  7. }
  8. add(x,Y) or add(int x, int y)
  9. {
  10. int a;
  11. a= x+ y;
  12. return a;
  13. }
  14.  
Kindly refer to the line numbers in bold and suggest the better code in that corresponding line number
Aug 10 '10 #1

✓ answered by donbock

Please use CODE tags so there are line numbers.

Mistake #1. The second line of your code snippet is the function prototype for add. There needs to be a semicolon at the end of that line.

Mistake #2. The prototype for add says it returns an int, but the definition doesn't specify any return value. You want to keep the prototype and definition consistent.

Mistake #3. The C Standard requires that main be defined to return int.

Allowable Variation #1. You are not required to specify the argument names in a function prototype. That is, the following two prototypes are equivalent. However, there are two advantages to keeping the argument names in the prototype. (1) An easy way to create the function prototype is to cut-and-paste the function definition. All you have to do is add a semicolon. In this case it is extra work to remove the argument names. (2) Well-chosen argument names can serve as comment regarding what each argument does.
Expand|Select|Wrap|Line Numbers
  1. int add(int x, int y);
  2. int add(int, int);
Deprecated Variation #2. C lets you omit the type in most places where you would normally specify the type in a declaration or definition because it assumes a default type of int; this includes the return value and perhaps argument types in function prototypes and definitions. However, making use of this feature has been deprecated for over 30 years. The most recent version of the C Standard formally announced that this feature might be removed from the next release of the Standard. People will talk about you behind your back. DON'T USE THIS FEATURE!

13 1640
donbock
2,426 Expert 2GB
What do you think is the difference between the two options?
What do you see as the advantages and disadvantages of each option?
Aug 11 '10 #2
Oralloy
988 Expert 512MB
Why does this sound like you're asking us to answer a test question for you?
Aug 11 '10 #3
Hi Oralloy,
No these are not test questions. As I go through the chapters in C I have doubts either in some concepts or in some tutorial questions. Just I want to make that clear.That is the reason for these questions and definitely I am not appearing for any test at present.
Aug 11 '10 #4
1. The C compiler will treat the actual arguments and formal arguments as two different variables. So in my point of view, even if we are using the same variables that has been used in the main function(in this case X & Y) in the Called function(in this case add(x,Y)) we should declare the variables again i.e. add(int x, int y).
2. In the function prototype is it enough to specify the return type of the function alone or we should even specify the datatype of the arguments of the func tion.
Aug 11 '10 #5
donbock
2,426 Expert 2GB
As I go through the chapters in C I have doubts either in some concepts or in some tutorial questions.
Please provide some context of how each of these options is presented in the chapters of your C book.
Aug 11 '10 #6
Actually in my book I had this code as an example of functions.

#include <stdio.h>
int add(int x, int y)
main()
{
int x,y,sum;
sum=add(x,y);
}
add(int x, int y)
{
int a;
a= x+ y;
return a;
}

But after seeing the above code I had a doubt of whether we can represent the same in a different way and therefore i raised the question.
Aug 11 '10 #7
donbock
2,426 Expert 2GB
Please use CODE tags so there are line numbers.

Mistake #1. The second line of your code snippet is the function prototype for add. There needs to be a semicolon at the end of that line.

Mistake #2. The prototype for add says it returns an int, but the definition doesn't specify any return value. You want to keep the prototype and definition consistent.

Mistake #3. The C Standard requires that main be defined to return int.

Allowable Variation #1. You are not required to specify the argument names in a function prototype. That is, the following two prototypes are equivalent. However, there are two advantages to keeping the argument names in the prototype. (1) An easy way to create the function prototype is to cut-and-paste the function definition. All you have to do is add a semicolon. In this case it is extra work to remove the argument names. (2) Well-chosen argument names can serve as comment regarding what each argument does.
Expand|Select|Wrap|Line Numbers
  1. int add(int x, int y);
  2. int add(int, int);
Deprecated Variation #2. C lets you omit the type in most places where you would normally specify the type in a declaration or definition because it assumes a default type of int; this includes the return value and perhaps argument types in function prototypes and definitions. However, making use of this feature has been deprecated for over 30 years. The most recent version of the C Standard formally announced that this feature might be removed from the next release of the Standard. People will talk about you behind your back. DON'T USE THIS FEATURE!
Aug 11 '10 #8
Oralloy
988 Expert 512MB
@vensriram

No insult intended. Your question just sounded like one of the test problems from my language survey class from oh so many years ago.

The response @donbock gave you is quite good. The only things I would add are

1) The C language has continually moved toward clarity of definition and declaration, where other languages like Perl have moved toward type transparency and run-time dispatch. There are also languages like Haskal (which I've never used) which have compile time type inference. They all serve their purpose well, although they're obviously oriented toward solving different classes of problems. Given the choice, select the language which best solves your problem.

2) C programmers have come to expect the what I call "fully quallified new-style function prototypes and definitions". I know "less is more", and by declaring less, you (in theory) have reduced chance of error. Personally, I don't think three letters are going to cause your program's code to bloat or introduce any additional errors. It may, however, illuminate ones that are already there.

Think about the expressional clarity of this function:
Expand|Select|Wrap|Line Numbers
  1. fac(double d)
  2. {
  3.   if (1 > d) return 1;
  4.   else return d * d-1;
  5. }
If you saw this in code you had to work with, what would you conclude?
Aug 11 '10 #9
That s ok, i understood.
Then from your code whether u want me to find the utility of the code?
Aug 12 '10 #10
Thank you donbock for your detailed description
Aug 12 '10 #11
Oralloy
988 Expert 512MB
@vensriram,

Sorry I disappeared on you for several days. Life reached out and pulled me away from browsers and my e-mail for almost a whole week.

What I was trying to illustrate with my code example was how misleading it can be to leave out implicit information in code. Basically, what I wrote looks like a factorial function, but in fact is a far different beast. The basic observation is that you should be clear (to humans) in what you intend the code to do, because it is probably very clear to the compiler what it needs to do.

BTW, that subtle lack of parenthesis in the second return is a modified form of a real life calculation that a co-worker of mine spent ten minutes writing and a week failing to debug. In his case, it was operator precedence of bit shift versus bitwise boolean operations. He wrote something like this:
Expand|Select|Wrap|Line Numbers
  1. /* swab2 - reverse two byte value */
  2. int swab2(int v)
  3. {
  4.   return (v & 0xff << 8) | (v & 0xff00 >> 8);
  5. }
  6.  
Anyway, I've seen so many of that sort of error over the years that I just fully parenthesize expressions and don't worry about precedence.

Cheers. Thanks for the good intellectual exercise.
Aug 16 '10 #12
Yes, I understood that because at any point of time it just multiplies n and (n-1). Factorial can be done through recursion. Thank you for a very informative discussion.
Aug 20 '10 #13
Oralloy
988 Expert 512MB
And thank you, as well. I'm glad of the chance to discuss my thoughts and perhaps learn from others as well.

Cheers!
Aug 23 '10 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
8
by: Larry Smith | last post by:
If Not dtrMyDataReader.IsDBNull(dtrMyDataReader.GetOrdinal("Customer")) Then strCustomer = dtrMyDataReader("Customer").ToString() End If Where "Customer" is a string data type.
88
by: Peter Olcott | last post by:
Cab you write code directly in the Common Intermediate language? I need to optimize a critical real-time function.
3
by: shuisheng | last post by:
Dear All, Assume I have a base class of Shape, and derived classes of Sphere, Cube, Cylinder. When user input 1, it creates a new default sphere; 2, a new default cube; and 3 a new default...
23
by: Schannah | last post by:
I'm trying to create a design which mimics the Radiohead website in the action on this page, but the problem is that they use PHP for the effect and I have no idea about PHP. I'm very amateur: fairly...
7
by: John | last post by:
For my code of radix sort, I need to initialize 256 buckets. My code looks a little clumsy: radix=] for i in range(255): radix.append() any better code to initalize this list?
11
by: devnew | last post by:
hello while trying to write a function that processes some numpy arrays and calculate euclidean distance ,i ended up with this code (though i used numpy ,i believe my problem has more to do with...
7
n8kindt
by: n8kindt | last post by:
this is what i have: Right$(,(Len()-1)) it works but i'm just curious... is there a built-in function that is better than this?
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.