How can one call new functions in c++? what are the neccesary steps needed in defining a function and invoking it before it can be used for programming?e.g if you want to define and call a function in the header file <iostream>?i would also like to know the return type used.again,how do one know that function name is a valid c++ identifier?thanks
Defining functions
Started by kenex, Dec 10 2008 02:43 AM
6 replies to this topic
#1
Posted 10 December 2008 - 02:43 AM
|
|
|
#2
Posted 10 December 2008 - 08:27 AM
I believe you are talking about defining new functions.
The name of a function can be any valid variable name (not a reserved word, doesn't start with a number, etc). When you define the function you specify the return type and parameters it takes. Generally, you will simply create the new function in the same file as your other code, being sure to declare it before another function tries to call it. If you get almost any book on C++, it can teach you how to do this.
The name of a function can be any valid variable name (not a reserved word, doesn't start with a number, etc). When you define the function you specify the return type and parameters it takes. Generally, you will simply create the new function in the same file as your other code, being sure to declare it before another function tries to call it. If you get almost any book on C++, it can teach you how to do this.
#3
Posted 11 December 2008 - 01:20 AM
pls how do i know the return type of the function statement e.g if it is void or not?i would also like to know where and where not to put the function prototype or declaration before making funtioncall?
#4
Posted 11 December 2008 - 08:39 AM
If you are creating the function, it's what you made it. If you are using a library function, refer to the documentation or header file.
You can put function prototypes in an included header file or at the beginning of your file.
You can put function prototypes in an included header file or at the beginning of your file.
#5
Posted 11 December 2008 - 08:57 AM
Ehh I meant C++3rdE, not K&R 2.
#6
Posted 11 December 2008 - 08:30 PM
What would you need a function in the first place. You need it to do some common job, and chance is, you're going to need the functionality again and again.
Lets say, you have this job to do. You have a fullname, and you want to parse it to First, Middle Initial and Last. Now this qualifies to be created as a function. First give it a name, how about
parse_name, or ParseName. (regarding keywords confliction. The set of keywords are but limited, you should get formilar with then gradually. The good things is, many code editors do syntax highlighting for you, so you can tell immediately if it's a keyword or not).
what should you return? If really depends. Depend on how you intend to use it. You can not return all 3 parts of the name(actually you can, but let's pretend it's not good to do it). You can choose to return void. or you can return a bool to signal whether the parsing is successful.
How about the parameters. You need the full name as an input. You also need some space to store the result of the parsing. A possile prototype would be
void parse_name(char * full_name, char* first, char * mi, char * last);
Let's say it's agreed the caller will provide enough storage for first, mi, last.
So when somebody need the functionality, he/she can use it like this:
char first[30];
char mi[2];
char last[30];
parse_name("Sant Clause", first, mi, last);
to make your job available to others, you would put the prototype in a header file. Either distribute you *.c or *.cpp file which you implemented the logic, or compile it to a libary file and distribute with the headfile.
Lets say, you have this job to do. You have a fullname, and you want to parse it to First, Middle Initial and Last. Now this qualifies to be created as a function. First give it a name, how about
parse_name, or ParseName. (regarding keywords confliction. The set of keywords are but limited, you should get formilar with then gradually. The good things is, many code editors do syntax highlighting for you, so you can tell immediately if it's a keyword or not).
what should you return? If really depends. Depend on how you intend to use it. You can not return all 3 parts of the name(actually you can, but let's pretend it's not good to do it). You can choose to return void. or you can return a bool to signal whether the parsing is successful.
How about the parameters. You need the full name as an input. You also need some space to store the result of the parsing. A possile prototype would be
void parse_name(char * full_name, char* first, char * mi, char * last);
Let's say it's agreed the caller will provide enough storage for first, mi, last.
So when somebody need the functionality, he/she can use it like this:
char first[30];
char mi[2];
char last[30];
parse_name("Sant Clause", first, mi, last);
to make your job available to others, you would put the prototype in a header file. Either distribute you *.c or *.cpp file which you implemented the logic, or compile it to a libary file and distribute with the headfile.
#7
Posted 13 December 2008 - 06:03 AM
Thank u wing & lance.keep it up!


Sign In
Create Account


Back to top









