Public Group

This topic is 3632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I AM TRYING TO GET A BIT OF THE C++ but then I stop to a simplyest question in the world: how to make a function? I can begin like this: I want to make for example: int digit; digit = -14; if(digit >= 0)digit = digit; if(digit < 0)digit = digit * -1; And convert to this line: digit = function(digit) And I dont know how to make this but I know that that can be made by headers. PLZ help!

##### Share on other sites
You don't need a header for this.

A function in C++ looks like this:
return-type function-name( parameter-list ) {  code }

So for example, the function you want might be:
int absolute( int x ) {  if( x >= 0)     return x;  if( x < 0 )    return x * -1;}

This isn't necessarily the most optimal implementation of that function, simply the form that maps most closely to what you've provided. In fact there is a standard version of that function available to you (abs()).

It sounds like you're relatively new to C++ and don't have a firm grasp of some of the basic concepts (such as what a header file is and what it does) that are typically expected to be understood prior to working with functions and the like.

I recommend you Google for "Thinking in C++" and/or "C++: A Dialog." These are both free texts about C++ and are generally considered decent learning material. You should read them -- in particular their sections on functions. They will help you quite a bit.

##### Share on other sites
Quote:
 Original post by jpetrieI recommend you Google for "Thinking in C++" and/or "C++: A Dialog." These are both free texts about C++ and are generally considered decent learning material. You should read them -- in particular their sections on functions. They will help you quite a bit.

Anyway I know that header is a some sort of list of custom functions and the abs value was an exapmle. I created even a average value of entered in cmd following "C++ Beginner's Guide" but there was told "your not experienced for more complicated stuff" and the file ends :)

##### Share on other sites
Quote:
 Original post by xfrontAnyway I know that header is a some sort of list of custom functions and the abs value was an exapmle.

Not really.

A header is a way of factoring out declarations of functions, as well as declarations or full definitions of types, in order to avoid writing them at the top of every translation unit.

##### Share on other sites
Quote:

As a colloquial slang term, yes. "Google for X" is shorthand for "go to www.google.com and type X into the search box and hit enter." When you have to ask people to do that often, saving yourself the keystrokes helps. Google themselves apparently dislike the usage of their brand as a verb, but I value my wrists more than their pride.

It will produce for you, for example, this link.

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
17
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002297
×