How can we implement f(x)=10 in c++;

Sorry but I am not sure I understand your question.

Do you want a function f that takes a parameter x and always returns 10?

Or do you want to compare the result of f(x) to 10?

The former could be achieved like so:

   int f(int)
       return 10;

Assuming the values involved are integers. Note that the parameter (a single int value) has no name. This is because although it is required to be passed, it is not referred to in the function definition.

Maybe a better formulation would be:

   int f(double)
       return 10;

As a double parameter should allow most if not all arithmetic types to be converted to it without loss of data.

The latter, comparing f(x) to 10, case can be achieved like so:

   f(x) == 10

or even:

   10 == f(x)

Note we use two = symbols to represent test for equality, the result of which is a bool (Boolean) value so can be used where bool values are expected such as if statement conditions. A single = symbol is used to mean assignment:

   y = f(x);

This is different to some languages, such as Pascal, in which a single = is used for equality and another operator, := in the case of Pascal, is used for assignment, or = is used for both equality and assignment.

Note also that the use of the assignment operator in C and C++ is an expression and yields a result (the value assigned) that may be convertible to a bool, and so:

   if ( a = 10 )

is valid but usually incorrect C and C++. Some compilers will warn of such usages. Some forms do not work, such as:

   if ( 10 = a )

This does not work because we cannot assign to a literal value, thus sometimes we write code like:

  if ( 10 == a )

rather than

   if ( a == 10 )

Just to guard against accidentally forgetting one =, resulting in a valid (i.e. it compiles and builds) but incorrect (in that the test will yield incorrect results) program.

Hope this helps. If not and you wished for some other information please post a follow-up question with more details of what you are trying to do.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.


©2016 About.com. All rights reserved.