C pointers and functions

In this lesson, we discuss the important “C pointers and functions” topic. We use pointers and functions mostly when need to call functions. Therefore, we discuss two methods of C function calling in the following.

Function calling in C language

After defining a function, we need to use it. In fact, we call the function to do something for us. The question is how to call the function.

Calling means sending an argument (value) to a function and receiving the output. Calling is defined in two ways:

  1. Calling the function by value
  2. Calling the function by reference

Function call by value

In calling the function by value, a copy of the value of the function’s arguments is written on its parameters, so changing the argument does not affect the parameter’s value. Pay attention to the following example:

#include <stdio.h>

float tempconv(float centigrade);
 
float c, f;
 
void main(void){
	c = 38.7;
	f = tempconv(c);  
}
 
float tempconv(float centigrade){
	float i;  
	i = 1.8 * centigrade + 32;  
	return i;  
}
  1. In this example, the tempconv function converts the temperature in Celsius to Fahrenheit.
  2. Can you explain the function definition?
  3. Variables f and c are global variables and can be used in all programs and functions. Since they are not given an initial value, they will be zero by default.
  4. The variable i is a local variable that is only defined within the scope of the tempconv function and is destroyed after exiting the function.
  5. If we want the variable i not to be destroyed when leaving the function, we must use the word static before defining the variable.
  6. In the C language, public variables have an initial value of zero, while local variables have an undefined initial value.
  7. Line 9 is where the tempconv function is called.
  8. The value of the variable given as input to the function (variable C is the argument of the function) is copied to the input of the function (variable centigrade is the parameter of the function).
  9. After making this copy, any changes made during the program to the variable C do not affect the value of the centigrade parameter and vice versa. Because only a copy of the C variable is delivered to the function and not its address.

Function call by reference

There is another option in C language where the function body has direct access to the arguments. This is a useful feature called calling by reference. In this method, the address of the argument is copied to the parameter. In the body of the function, the sent address is used to access the argument; in this way, the changes made in the parameter variable also affect the argument. Pay attention to the following example:

#include <stdio.h>

void tempconv(float centigrade, float *farenheit, float *kelvin);

float c, f, k;

void main(void){
	c = 38.7;  
	tempconv(c, &f, &k);  
}

void tempconv(float centigrade, float *farenheit, float *kelvin){
	*farenheit = 1.8 * centigrade + 32;  
	*kelvin = centigrade + 273.15;  
}
  1. In this program, the temperature is received in Celsius and delivered in Fahrenheit and Kelvin.
  2. The tempconv function is responsible for doing this conversion.
  3. This function has no output and instead has three parameters.
  4. The parameters of this function are the Centigrade decimal variable and the Fahrenheit and Kelvin pointers that point to the decimal type.
  5. The arguments passed to this function at call time are the value of the variable C (call by value) and the address of the variables f and k (call by reference).
  6. Can you explain why pointers are used as function parameters?
  7. Because the call by reference has been used, the value of f and k variables, which are the outputs of this function, can be changed from within the function. Something that was not possible in the call with the value.

In the call-by-value method, the function can have one or more inputs and only one output. But in many applications, the function must have several outputs.

Was this helpful?
[0]
Scroll to Top
Scroll to Top