In this section, we will deal on working with functions and its argument in C. This will just serve as an overview of the usefulness of the idea of functions in C. We will discuss more details of functions in future. As for now, get the gist of the usefulness of function. Enjoy!
Functions
A function in C is equivalent to a subroutine or function in FORTRAN, or a procedure or a function in Pascal. Functions are a convenient way to encapsulate some computation, which we can use without worrying about its implementation. C makes the use of functions easy, convenient and efficient.
Since C has no exponentation operator ** of FORTRAN, we will create the mechanics of functions by writing a function power(m,n) to raise an integer m to a positive integer power n. Here is the function power and the main program calling the power function,
Program: Temperature Conversion | Output |
---|---|
A function definition has the form:
Function definitions can appear in any order, and in one source file or several. For the moment, we will explore functions and main being in the same file.
Each call, power(2,i) and power(-3,i), passes two arguments to power, which each time returns an integer.
The first line of the function:
Declares the parameter types (e.g int) and names (e.g. base, n), and the type of the result that the function returns (e.g. int). The names used by power, (e.g. i,p, base, n) are all local to power function and are not visible to any other function, thus other functions can use the same names without conflict.
Declares the parameter types (e.g int) and names (e.g. base, n), and the type of the result that the function returns (e.g. int). The names used by power, (e.g. i,p, base, n) are all local to power function and are not visible to any other function, thus other functions can use the same names without conflict.
Terminologies:
- parameter: for a variable named in the parenthesized list in a function definition
- argument: for the value used in a call of the function
The value that the function computes is returned to main by the return statement. For example,
The value that the function computes is returned to main by the return statement. For example,
Any expression may follow return:
But note that a function need not return a value. Observe that we have a return value of zero inside the main function; typically this implies a normal termination.
The declaration,
Just before main says that power is a function that expects two int arguments and returns an int. This declaration is called a function prototype, which has to agree with the definition and uses of power. If the definition and prototype of a function doesn’t agree error will be prompted. However, parameter names need not agree. Take the following for example,
- Function prototype:
- Function definition:
Exercise 1-15: rewrite the temperature conversion program to use a function for conversion
Answer:
Arguments – Call by Value
In C all function arguments are passed by “value” which is different if you are used to procedures in FORTRAN.
Fortran is a “call by reference” type of language, which has access to the original argument. In C, local copies of the value (temporary variables rather than the originals) of the arguments are passed on the function.
C functions cannot directly alter a variable in the calling function; it can only alter the temporary copy. Call by value is actually an asset of C language, not a liability. This usually leads us to a more compact programs with fewer variables to work with.
Parameters of functions can be thought of as initialized local variables in the called routine. In our previous example:
Original | Call by value function |
---|---|
The paremeter n is a temporary variable (right program), and is counted down until it become zero. There is no need any more of the variable i.
It is possible to arrange a function to modify a variable in a calling routine. The caller must provide the address of the variable to be set (technically referred to as the pointer of the variable), and the called function must declare the parameter to be a pointer and access the variable directly through it.
In Using Arrays as Arguments
Arrays are weird objects in C. When the name of the array is used as an argument, the value passed on the function is the location or address of the beginning of the array – there is no copying of array elements. Thus, the function can access and alter the elements of the array.
Disclaimer: this article is a summary of section 1.7-1.8 from the book The C Programming Language (ANSI C): by Brian Kernighan and Dennis Ritchie, the content apart from rephrasing is identical, and the same line of codes are treated.
Thank you for reading. copyright 2018 by @sinbad989
You got a 7.33% upvote from @minnowvotes courtesy of @sinbad989!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit