Skip to main content
Engineering LibreTexts

10.1: Functions - Declarations and Definitions

  • Page ID
    29089
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    Functions in C++

    A function is a set of statements that take zero or more arguments, does some specific computation and returns some value. All C++ programs have at least one function - that is the main() function.

    The idea is to put code that is repeated multiple times into a function so that instead of writing the same code again and again for different inputs, we can call the function.

    Why do we need functions?

    • Functions help us in reducing code redundancy. If the same functionality is necessary at multiple places in the project, then rather than writing the same code, again and again, we create a function and call it whenever needed. This also helps with code maintenance as we only have to change the code in one place and that change is good for every time the function gets called.
    • Consider a big project with thousands of lines of code. Using functions helps to reduce the number of lines of code, makes the code easier to read and make code maintenance easier.
    • Consider library functions, we can call them without worrying about how they work, we can simply call the function, pass any necessary arguments and accept the returned results.

    There are two tasks that are required when coding functions: provide a function prototype and provide the function definition.

    Prototypes

    The function prototype only contains the declaration of the function. For instance take the following example of a prototype for a function named Sum().

    int Sum( int, int );
    

    First of all, function prototypes include the function signature, which includes, the name of the function, return type and argument number and type In this case the name of the function is "Sum". The function signature determines the number of parameters, in this instance two, and their types, both are int values. In the above example, the return type is "int", meaning the function returns an integer value back to the calling function. Notice that in the function prototype you are NOT required to provide variable names, just the types of the arguments is required. The purpose of the prototype is to tell the compiler that there is a function named, in our example "Sum", and it accepts, in this example 2 integer arguments, and returns a value, in our example a "void" value, meaning we don't pay attention to the return value.

    Any number of arguments (also called parameters) can be passed, and you can mix any of the valid types in the list.

    int myFunction(float, char, int);
    

    Function Definition

    Since the prototype does not contain any actual code, we must also prvide the function definition, which is where the code is that gets executed when the function is called. Functions should do something specific, and then return to the calling location in the code. Your functions should not add a bunch of other processing that is not part of the specific processing. This takes practice to know when to return to the calling code and continue the processing there.

    The following is a simple example and includes the prototype and the definition, as well as a function call from main():

    #include <iostream>
    using namespace std;
    
    // Prototype is here, BEFORE it is used
    // This is the function declaration
    void output(char);
    
    int main()
    {
        char newCh = 'A';
    
        output(newCh);
    
        return 0;
    }
    
    // Function definition - includes the code for
    // the function
    void output(char inCh)
    {
        cout << "The value is " << inCh << endl;
        return;
    }

    Some of this code looks familiar. On line 6 we have the function prototype - simply tells the compile that there is a function called output, takes a single character argument, and return no value -  it is type void. There is no code for this prototype yet.

    In main() we declare a character variable named newCh and assign it the value of 'A'. Then we actually make the function call, passing the proper type of argument - a character. 

    At the bottom of our code we have the function definition, where the code for this function is provided. Now, on the first line of the function we have a variable type AND a variable name - inCh. A WORD OF WARNING - do not use the same variable name here that you used when you called the function - for instance do NOT call both of your variables newCh or inCh. While this is actually allowed in C++ it will generate confusion and in this class you will lose points for coding in that manner. Also - notice the return statement does not actually return a value - because this is a void function. If you put a value in the return statement you will get a compile error.

    When the function returns, control of the program returns the the line of code in main() where the function was called, and execution continues from that point.

    Definitions

    User Defined Library
    A file containing specific task functions created by individuals to be used in many programs.

    Adapted from:
    "Functions in C/C++" by Unknown, Geeks for Geeks CC BY-SA 4.0
    "Function prototype" by Numerous ContributorsWikipedia is licensed under CC BY-SA 4.0 


    This page titled 10.1: Functions - Declarations and Definitions is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.

    • Was this article helpful?