Function Calls in C Language

There are generally two ways to call a function: one is by value, where the formal parameters do not affect the actual parameters, and the other is by reference, where the formal parameters can affect the actual parameters.

Passing by Value

  Passing by value means passing the value of the actual parameter into the function body; what is passed in is merely a copy of the actual parameter and will not change the actual parameter. This was discussed in the previous section, and the reason is precisely because of the C language’s copy-by-value characteristic, which can cause us a lot of trouble.

Function Calls in C Language

  After running, you will find that the values of a and b have not been swapped, which is precisely because we only used passing by value here; the passed copy does not change the actual parameters.

Passing by Reference

  Passing by reference means passing the address of the parameter, which is essentially passing a pointer as a parameter. As we mentioned before, the address is like the house number of a variable, so when we pass the address of a variable, the actual parameter is locked; changes to the formal parameter will also change the actual parameter.

Function Calls in C Language

  After running the above line of code, the values of a and b will be swapped. However, the change we made was merely to change the parameter type to a pointer. Therefore, if we want the function to change the value of the actual parameters, we must pass the pointer of the parameter, provided that we write a function with pointer parameters. In C++, we will learn more convenient ways to change actual parameters than passing by reference.

Nested Function Calls and Chained Access

Nested Calls

  Nested calls form the most basic syntax of the C language, simply put, it allows calling other functions within a function. For example, calling the printf function in the main function is something everyone is familiar with.

Chained Access

  Chained access involves calling a function within the parameters of another function. This calling method is also very simple; it involves calling a function that returns a value in the parameter list of another function. At runtime, the function in the parameter list will be called first, and then the outer function will determine how to run based on the return value. Both of these calling methods are quite simple and will be frequently used in the future, so we won’t elaborate too much here.

Function Declaration and Definition

Function Declaration

  When writing a function, we must define it before calling it (previously we wrote it before the main function). So how can we write the function definition after the call for aesthetic and readability purposes?

  This is where function declarations come into play. A function declaration is simply a declaration made before the call to let the compiler find the location of the function definition, even if the definition is written after the call.

  1. The declaration must inform the compiler what the function is called, what the parameters are, and what the return type is, but it does not matter whether the function actually exists.

  2. Function declarations generally appear before function calls, satisfying the requirement of calling before using.

  3. Function declarations are usually placed in header files.

Function Calls in C Language

  In this example, we wrote the function definition after the function call, and this was possible because we added the function declaration before the call. Function declarations are not mandatory, but we generally write them in header files to make our code more readable and manageable.

Header Files

  Since we are talking about function definitions, we must mention something closely related to function definitions: header files. As mentioned earlier, functions are generally defined in header files.

Function Calls in C Language

Function Calls in C Language

  You can see that I wrote my defined functions in two other files, and in the file where the main function resides, I can use the functions just by including the header file where the function declarations are located. This way of writing functions makes our code look much cleaner.

  It is worth mentioning that when including our own header files in the main function, it is better to use “” to enclose the filename, as this makes it easier for us to quickly find the function. In the header file, you can also see the statement #pragma once; its purpose is to ensure that multiple calls to the same file do not cause errors. It can be said to compensate for the drawbacks of using C language header files and is essential for header files.

  Some may find another writing format in header files in other textbooks.

Function Calls in C Language

  Actually, the effect of these several statements is similar to that of #pragma once, but this is an older writing style with many drawbacks. Therefore, in the future, everyone should abandon this format when writing header files.

Function Recursion

  Function recursion simply means calling itself within the function, achieving a loop calling effect. It is a very important programming design method. Sometimes using recursion to design a program can make things much easier and allow the program to run more smoothly, but sometimes iteration is still more natural.

  Recursion in program design uses a divide-and-conquer approach, considering the whole from the local perspective.

Function Calls in C Language

  The above example is a good use of recursion in program design. In some ways, recursion can make your code easier to write, but recursion also has some very troublesome inefficiencies. For example, when using recursion to calculate Fibonacci numbers, it will produce a lot of repeated calculations, greatly reducing efficiency.

  Recursion is a very important programming design concept, and to fully master this design idea, more practice is needed in the future.

Function Calls in C Language

Follow us!

Leave a Comment