Jump to content

Defining functions

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
kenex

kenex

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
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

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
kenex

kenex

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Ehh I meant C++3rdE, not K&R 2.

#6
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
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.

#7
kenex

kenex

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Thank u wing & lance.keep it up!