frusterating
i am having a bit of trouble understanding how user defined functions work and what they are good for in c++. could anybody explain them to me or send me to a good web site that explains them? thanks.
If I am understanding correctly, user defined functions are simply functions that you, as a programmer, write (ie: not contained in any library or header file) That being said, you can''t live without them, it''s impossible. If you couldn''t write your own functions, you''d be forced to rely on whatever functions came with the compiler, and because there are an infinite number of problems, there wouldn''t be a function for every problem. Take a simple program for an example:
Okay, this might be a dumb example, but the above is an example of a user defined function. You start with a function derclaration, declaring the name, type, and any arguments the function takes. The function can then be used in your program by making a function call. In the above example, I make a call to my function "int sum()", sending 3 and 4 as arguments. When I call this function, the program goes down to the function declaration to see what it should do. In this case, it simply returns the sum of the two arguments.
Anyway, I hope I''m understanding the question right, and that this helps out, at least a little :D
Peon
int Sum(int a, int b); //function declarationint main(){cout<<Sum(3,4) //function callreturn(0}int Sum(int a, int b) //function definition{return (a+b)}
Okay, this might be a dumb example, but the above is an example of a user defined function. You start with a function derclaration, declaring the name, type, and any arguments the function takes. The function can then be used in your program by making a function call. In the above example, I make a call to my function "int sum()", sending 3 and 4 as arguments. When I call this function, the program goes down to the function declaration to see what it should do. In this case, it simply returns the sum of the two arguments.
Anyway, I hope I''m understanding the question right, and that this helps out, at least a little :D
Peon
Peon
Code reuse and modularity
Some programs are pretty big. I mean, printed out some programs would be the size of Moby Dick(the book, not the whale). Now, programming is basically all about making tasks easier to do for some user. The way programming languages try making programming easier is by encouraging code reuse and modulerization.
In a standardized language like C, functions that are used a lot are already made and come with the compiler. Like printing to the screen, or math functions. Believe it or not, someone actually wrote the code for those functions: in a sense, they are user defined. In the case of C, the user was the American National Standards Institute(ANSI) Okay, ANSI isn''t a person, but a group of people.(we''ll get that later)
"Okay, but why would I need to write a function if we have all these really cool functions already?"
In a way, that''s what software devolopers should be asking. A common used saying is "don''t reinvent the wheel," which basically means, "don''t write a function if others exist that do what you want." So you should make sure the function you need doesn''t exist already.
But what if your job was to write a function for other people to use? If you worked for microsoft, for example, and Bill comes to you and says, "write me a function that takes the paramaters A and B, does X, and returns Y," where A, B, and Y are variables and X is the operations done on A and B. He doesn''t want to know how you do it, just that it will work the way it''s supposed to.
Imagine that you are Bill, and that you''re writing a program and you get to a point where you need the least common multiples of two numbers. So, you would write
But wait, you don''t actually HAVE the function to do that. So you tell your employee to write the function and you just add it in later. Modularity is about not knowing how the code works, but that it does.
Another way modularity is important is because what if you write a program that uses the least common multiple code and use it a hundred times over several thousand lines of code. Pretend that you run the program and realize the LCM code is wrong. Because you didn''t use functions you''ll have to go over all the code changing the offending errors. If you used a function, you would only have to change the code in one place.
But what about code reuse? Well, that one''s easy. Let''s say you write your program that uses the LCM function and you sell it and get rich. You start work on another program and realize you need to find the least common multiple again, so you write another LCM function that does the same thing as the last one. Wait, that doesn''t seem right! You got the code from the last program, just copy and past! And so you do.
And finally, your code will probably be smaller. Much smaller. This is because the function resides only in one spot in the program and when you call it, all you do is Jump to the right spot.
And those are some reasons to use functions.
Some programs are pretty big. I mean, printed out some programs would be the size of Moby Dick(the book, not the whale). Now, programming is basically all about making tasks easier to do for some user. The way programming languages try making programming easier is by encouraging code reuse and modulerization.
In a standardized language like C, functions that are used a lot are already made and come with the compiler. Like printing to the screen, or math functions. Believe it or not, someone actually wrote the code for those functions: in a sense, they are user defined. In the case of C, the user was the American National Standards Institute(ANSI) Okay, ANSI isn''t a person, but a group of people.(we''ll get that later)
"Okay, but why would I need to write a function if we have all these really cool functions already?"
In a way, that''s what software devolopers should be asking. A common used saying is "don''t reinvent the wheel," which basically means, "don''t write a function if others exist that do what you want." So you should make sure the function you need doesn''t exist already.
But what if your job was to write a function for other people to use? If you worked for microsoft, for example, and Bill comes to you and says, "write me a function that takes the paramaters A and B, does X, and returns Y," where A, B, and Y are variables and X is the operations done on A and B. He doesn''t want to know how you do it, just that it will work the way it''s supposed to.
Imagine that you are Bill, and that you''re writing a program and you get to a point where you need the least common multiples of two numbers. So, you would write
int result = LCM(256, 32);
But wait, you don''t actually HAVE the function to do that. So you tell your employee to write the function and you just add it in later. Modularity is about not knowing how the code works, but that it does.
Another way modularity is important is because what if you write a program that uses the least common multiple code and use it a hundred times over several thousand lines of code. Pretend that you run the program and realize the LCM code is wrong. Because you didn''t use functions you''ll have to go over all the code changing the offending errors. If you used a function, you would only have to change the code in one place.
But what about code reuse? Well, that one''s easy. Let''s say you write your program that uses the LCM function and you sell it and get rich. You start work on another program and realize you need to find the least common multiple again, so you write another LCM function that does the same thing as the last one. Wait, that doesn''t seem right! You got the code from the last program, just copy and past! And so you do.
And finally, your code will probably be smaller. Much smaller. This is because the function resides only in one spot in the program and when you call it, all you do is Jump to the right spot.
And those are some reasons to use functions.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement