Skip to main content
Engineering LibreTexts

3.3: Functions

  • Page ID
    35805
  • \( \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}}\)

    Functions use the same naming rules as variables. All functions use the same template that looks something like this:

    Code \(\PageIndex{1}\) (C): Basic function template.

    return_value function_name( function argument list )
    {
        statement(s)
    }
    

    You might think of the function in the mathematical sense. That is, you give it some value(s) and it gives you back a value. For example, your calculator has a sine function. You send it an angle and it gives you back a value. In C, functions may have several arguments, not just one. They might not even have an argument. Also, C functions may return a value, but they don’t have to. The “guts” of the function are defined within the opening and closing brace pair {}. So, a function which takes two integers, x and y, as arguments, and returns a floating point value will look something like this:

    float my_function( int x, int y )
    {
        //...appropriate statements here...
    }
    

    If the function doesn’t take or return values, the word void is used. If a function neither requires values nor returns a value, it would look like:

    void other_function( void )
    {
        //...appropriate statements here...
    }
    

    This may appear to be extra fussy work at first, but the listing of data types makes a lot of sense because C has something called type checking. This means that if you try to send a function the wrong kind of variable, or even the wrong number of variables, the compiler will warn you that you’ve made a mistake! Thus if you try to send my_function() above two floats or three integers, the compiler will complain and save you a big headache during testing.

    All programs must have a place to start, and in C, program execution begins with a function called main. This does not have to be the first function written or listed, but all programs must have a function called main. Here’s our first program, found in Code \(\PageIndex{2}\), following:

    Code \(\PageIndex{2}\) (C): A simple program.

    /* Our first program */
    
    void main( void )
    {
        float x = 2.0;
        float y = 3.0;
        float z;
        
        z = x*y/(x+y);
    }
    

    There is only one function here, main(). It takes no variables and returns nothing. What’s the other stuff? First, the /* */ pair denotes a comment1. Anything inside of the comment pair is ignored by the compiler. A C comment can stretch for many lines. Once inside the function, three variables are declared with two of them given initial values. Next, the variables x and y are multiplied together, divided by their sum, and assigned to z. As C is free-flow, an equivalent (but ugly) version is:

    Code \(\PageIndex{3}\) (C): Alternate format (to be avoided).

    /* Our first program */ void main(void){
    float x=2.0;float y=3.0;float z;z=x*y/(x+y);}
    

    This is the complete opposite of Python which has very rigid spacing and formatting rules.

    Now, suppose that this add, multiply, divide operation is something that you need to do a lot. We could split this off into a separate function. Our program now looks like Code \(\PageIndex{4}\):

    Code \(\PageIndex{4}\) (C): Program with separate function.

    /* Our second program */
    
    float add_mult_div( float a, float b )
    {
        float answer;
        
        answer = a*b/(a+b);
        return( answer );
    }
    
    void main( void )
    {
        float x = 2.0;
        float y = 3.0;
        float z;
    
        z = add_mult_div( x, y );
    }
    

    The new math function takes two floats as arguments and returns a float to the caller. The compiler sees the new function before it is used in main(), thus, it already “knows” that it should be sent two floats and that the return value must be assigned to a float. It is very important to note that the new math function uses different variable names (a and b) from the caller (x and y). The variables in the new math function are really just place-holders. The values from the original call (x and y) are copied to these new variables (a and b) and used within the new function. As they are copies, they can be altered without changing the original values of x and y. In this case, x and y are said to be local to the main() function while a and b are local to the add_mult_div() function. In other words, a isn’t visible from main() so you can’t accidentally alter it! Similarly, x isn’t visible from add_mult_div(), so you can’t accidentally alter it either. This is a positive boon when dealing with large programs using many variable names. While it’s not usually preferred, there are times when you want a variable to be known “everywhere”. These are called global items. You can make variables global by simply declaring them at the beginning of the program outside of the functions (i.e., right after that initial comment in our example).


    1. C also allows // to denote a single line comment without the “backend pairing”.

    This page titled 3.3: Functions is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by James M. Fiore via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.