question on functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gdarian216
    New Member
    • Oct 2006
    • 57

    question on functions

    can you pass a string to a function? and would it have to be by reference?

    example if you were making a program and function prototypes and function definitions in seperate files and the function calls are in the main file.

    how would i code that?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by gdarian216
    can you pass a string to a function? and would it have to be by reference?
    You didn't mention the language; using C you can pass char arrays or pointers
    to chars (both are considerd strings) to any function you like. The pointers are
    passed by value and arrays in a value context are considerd pointers too so a
    pointer to the first element of the array is passed.

    If you're using C++ you can pass the same things as mentioned above and you
    can also pass C++ strings (a type defined in the standard lib) either by value
    or by reference; it's up to you.

    example if you were making a program and function prototypes and function definitions in seperate files and the function calls are in the main file.
    Yep, that's the way you should do it: put the declarartion in header files (.h)
    and stick your definitions in C or C++ files (.c or .cpp or c++ or ...)

    how would i code that?
    Give it a try and when you're stuck you're welcome to ask more specific questions.

    kind regards,

    Jos

    Comment

    Working...