void
or datatype
keyword followed by the name of the parameter(s) enclosed in parentheses. For example:C
void myFunction(int param1, char param2);
myFunction
is a function that takes two arguments - an integer (param1
) and a character (param2
).C
myFunction(5, 'a'); // passing integers and characters to functions
Here's a simple example of a function that calculates the area of a rectangle:
#include <stdio.h>
void calculateArea(int length, int width) {
printf("The area of the rectangle is %d sq units\n", length * width);
}
int main() {
calculateArea(5, 4); // calling the function with arguments 5 and 4
return 0;
}
C supports functions that can take a variable number of arguments using the ...
token. These are known as variadic functions. Here's an example of a function that calculates the sum of any number of integers:
#include <stdarg.h> // header for variadic functions
int calculateSum(int numArgs, ...) {
int sum = 0;
va_list args;
va_start(args, numArgs); // initialize the variable argument list with 'numArgs' as the last declared argument
for (int i = 0; i < numArgs; ++i) {
sum += va_arg(args, int); // extract next integer from the variable argument list
}
va_end(args); // clean up and release memory associated with the variable argument list
return sum;
}
int main() {
int sum = calculateSum(3, 5, 7, 9); // calling the variadic function with three integers as arguments
printf("The sum of the numbers is %d\n", sum);
return 0;
}
What causes it: Forgetting to declare a function before calling it.
#include <stdio.h>
void myFunction(int param1, char param2); // Declare the function here!
int main() {
myFunction(5, 'a'); // Calling the function before declaring it causes a NameError
return 0;
}
Solution: Declare the function before calling it.
What causes it: Passing an incorrect data type as an argument for a specific parameter.
#include <stdio.h>
void myFunction(int param1, float param2); // Function expects integers and floats
int main() {
myFunction('a', 5.0); // Passing a character as the first argument causes a TypeError
return 0;
}
Solution: Ensure that you're passing the correct data types for each parameter when calling the function.
What causes it: Forgetting to include the function definition in your code, or having a syntax error in the function definition.
#include <stdio.h>
int main() {
calculateArea(5, 4); // Undefined reference error because 'calculateArea' is not defined anywhere in this code
return 0;
}
Solution: Include the function definition where it belongs (either within the same file or in a separate header and implementation files). Ensure that the function definition has no syntax errors.
What causes it: Forgetting to include the <stdarg.h>
header when working with variadic functions.
#include <stdio.h>
int calculateSum(int numArgs, ...) { // Compilation error because we didn't include <stdarg.h>
int sum = 0;
va_list args;
// rest of the code...
}
Solution: Include the <stdarg.h>
header before using variadic functions.
What causes it: Forgetting to clean up and release memory associated with the variable argument list using va_end()
.
#include <stdio.h>
#include <stdarg.h>
int calculateSum(int numArgs, ...) {
int sum = 0;
va_list args;
// initialize the variable argument list...
for (int i = 0; i < numArgs; ++i) {
sum += va_arg(args, int);
}
// forgetting to clean up and release memory associated with the variable argument list!
}
Solution: Always call va_end()
after using a variadic function to clean up and release memory associated with the variable argument list.
va_end()
to clean up and release memory associated with the variable argument list....
token.