(what does implemented function mean? and how do i write one?)

i need explanation with example of your own!!

i`m studying for
my midterm..
and i can`t find implemented functions in my notes!!

i really don`t know how to write them...

can anyone explain to me??


be kind it`s not a homework code :D :`s
Last edited on
erm.. an implemented function is one with some code in it :)

e.g.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// header file
class wibble
{
public:

	int addTwoNumbers(int x, int y); // <-- FUNCTION DECLARATION

};


// .cpp file - the IMPLEMENTED FUNCTION

int wibble::addTwoNumbers(int x, int y)
{

	int sum = 0;

	sum = x + y;

	return sum;

}
so you mean by public that it gets to be used by all functions that ask for x & y?? and it`s not changeable until i define a value for x or y inside a function?


but would it help me if a question is asked to used to find the( rad, mass, ...etc equations ) for multiple things!!

like squares , circles ,triangles and so on...)??

like if i want to call my implemented function in my main functions several of times do i use a loop??

mutexe
(^w^)
you should drop the word implemented in that context really as you cant call an unimplemented function

so you mean by public that it gets to be used by all functions that ask for x & y?? and it`s not changeable until i define a value for x or y inside a function?


not sure what you mean by this, i could have left it private scope and the implementation would be the same.
i don`t even know what i`m talking about ;`C
Based on your other posts and your last comment, I assume you are a beginner at C++. However, you have been starting threads in the General C++ Programming forum (I think). You have not been using the Beginners forum. So it is reasonable for someone assume you have a basic understanding of most of the C++ language.

When mutexe was trying to explain "implemented function" to you, he assumed that you knew what a C++ class is. So he showed you a class "declaration" with one public method. He indicated that this class declaration would be put into a header file so it can be included in source code files (.cpp) where the class is being used.

He also included a "method definition" or "function definition" which would normally be included in the .cpp file which defines (not declares) the class.

So let us extend the terminology which your course may be using. An "unimplemented function" is probably function for which there is a declaration (also known as a prototype) but not a definition. An "implemented function" is a function which has both a declaration and a definition. The definition can normally also act as a declaration if it occurs in the file before any uses.

My suggestion is that you start to use the Beginners forum until you know all the features of the language.

[Note: I have very limited knowledge of C++ but extensive knowledge of C. However, I do not answer C++ questions for which I am unsure about the elements of the language which apply. But, of course, sometime I will think I know the applicable parts of the language, but am just proving that I am wrong. :-) ]
thank yew...

and yeah i`m a beginner ,but it`s not my major either ...

so to learn this it`s really hard.,, barely knew how to use c++ in the beginning...

i`ll try to see the forum you mentioned hoping i`d get to something ...and thank yew(^w^)

pheininger
oops, sorry dude, i did assume you knew about classes.
Apologies.
ow ow ,,it`s ok!!

you didn`t know ((sorry)) really sorry (><)

mutexe
OK. Below is a program that does nothing. It defines (or implements) and calls a function DoNothing(). The function DoNothing() as its name implies also does nothing.

Depending on how your development environment works, this may execute so fast that you do not even see an execution window.

1
2
3
4
5
6
7
8
9
10
void DoNothing()
{
}

int main()
{
    DoNothing();

    return 0;
}


Now my suggested exercises for you before your mid-term are the following:

Edit:0) Make sure the above compiles, links, and executes in your development environment without errors and warnings (expect possibly a warning that the program does nothing).
1) Change DoNothing to a function named ResistorCurrent which takes two parameters: resistance and voltageDrop. The function should return the calculated current. This version should call ResistorCurrent several times with different values for the resistance and voltageDrop.
2) Your next version should display the calculated resistor current for each example.
3) Your next version should ask the user for the resistance and voltage drop instead of using constants.
Last edited on
Can you give me easier equations to be used inside functions ~( i _ i )~

pheininger
Perhaps a different perspective might be helpful.

1
2
3
4
5
6
7
int DoSomething( int i )
{
	int j = 0;
	j = i * 3;

    return j;
}


This is a pretty generic function. We refer to this as the implementation of the function since this is the part which actually contains all the internal logic of the function. Why we need the distinction of the term 'implementation' I'll explain later.

now I'll show a generic program with uses our function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
	int a = 2;
	int b = 3;
	int c = 0;
	int d = 0;

	c = DoSomething( a );
	d = DoSomething( b );
} 

int DoSomething( int i )
{
	int j = 0;
	j = i * 3;

    return j;
}


Looks fairly simple right, any idiot could follow that program. Except your compiler is a lot dumber than you give it credit for. The compiler starts at the top of the file and works it's way down, and the instant it encounters something it doesn't know how to handle, it generates errors and stops. The problem here, is that when it runs into the line c = DoSomething( a ); in main, the compiler doesn't know what to do. It has no idea what 'DoSomething()' is, because it's never seen it before and doesn't know how to handle the function call.

This is why we have 'declarations' of functions in addition to the 'implementation' of functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int DoSomething( int i );

int main()
{
	int a = 2;
	int b = 3;
	int c = 0;
	int d = 0;

	c = DoSomething( a );
	d = DoSomething( b );
} 

int DoSomething( int i )
{
	int j = 0;
	j = i * 3;

    return j;
}


Here's the final bit... the first line is the function declaration (also called a prototype). this just tells the compiler what the function looks like (what type of parameters are passed, and what type the return value is). That's all the information the compiler really needs to generate a working call to the function when it hits the c = DoSomething( a ); line, so now that it knows what to do, it can successfully compile the whole program.

Does this clarify the distinction between declaration and implementation?
Last edited on
Esslercufi said: <removed because of his edit so my comments no longer apply>

Last edited on
Yeah, I screwed that up... but fixed it 10 min before you posted :)
To reiterate, a declaration or prototype simply tells the compiler the information it needs to properly generate a call to a function. the implementation or definition is the actual code which the function itself performs.
Last edited on
\(//∇//)\ yaaaaass..... That what I was searching for , thank yew thank yew... So implementation is a thing I do by my self ... Or a function I make and declare so it can work on behalf of main function!?

I thought it was a function that It has a certain image to make ...(⌒-⌒; )
Sort of.
An implementation is the actual code that makes up a function.
A declaration tells other parts of a program how to make use of that function.
Aaaahaaa~~
Topic archived. No new replies allowed.