int add(int x = 0, int y = 0); // Function declaration for adding two integers with default values
In this example, we’ve added default values to both parameters “x” and “y”. This means that if only one parameter is passed when calling the function, it will use the default value for the other parameter.
Here’s an example:
c
int main() {
int result = add(5); // Call the add function with one integer parameter and store the result in a variable
printf(“The sum of %d and %d is %dn”, 5, 0, result); // Print out the result
return 0;
}
Function Overloading
C programming does not support function overloading, which means that functions cannot have multiple parameter lists or return types. However, you can achieve similar functionality by creating multiple functions with different parameter lists or return types. Here’s an example:
c
int add(int x, int y); // Function for adding two integers
double multiply(float x, float y); // Function for multiplying two floating-point numbers
In this example, we’ve created two functions: “add” for adding two integers and “multiply” for multiplying two floating-point numbers. We then call both functions with the appropriate parameters and store the results in variables.
c
int main() {
int result1 = add(5, 3); // Call the add function with two integer parameters and store the result in a variable
double result2 = multiply(5.0, 3.0); // Call the multiply function with two floating-point parameters and store the result in a variable
printf(“The sum of %d and %d is %dn”, 5, 3, result1);
printf(“The product of %f and %f is %fn”, 5.0, 3.0, result2);
return 0;
}
Function Calls and Return Values
When calling a function in C programming, you must also check the return value to see if it is what you expect. If the function returns a value, you can use that value in your program as needed. Here’s an example:
c
int add(int x, int y); // Function for adding two integers
In this example, we’re calling our “add” function with two integer parameters (5 and 3) and storing the result in a variable called “result”. We then print out the result using the “printf” function. Since “add” returns an integer value, we can be confident that the returned value is what we expect.
c
int main() {
int result = add(5, 3); // Call the add function with two integer parameters and store the result in a variable
printf(“The sum of %d and %d is %dn”, 5, 3, result);
return 0;
}
Function Calls and Argument Types
When calling a function in C programming, you must ensure that the arguments passed match the parameter list of the function. If you pass the wrong types or numbers of parameters, the function will produce undefined behavior. Here’s an example:
c
int add(int x, int y); // Function for adding two integers
In this example, we’re calling our “add” function with two arguments: an integer (5) and a string (“3”). This is incorrect because the second argument should be an integer, not a string. When the program runs, it will produce undefined behavior because of this mistake.
c
int main() {
int result = add(5, "3"); // This is incorrect! We should not pass a string parameter to an integer function
printf(“The sum of %d and %s is %dn”, 5, “3”, result);
return 0;
}
Function Calls and Memory Management
When calling a function in C programming, you must also be aware of memory management issues. Functions can allocate or deallocate memory dynamically using functions like "malloc" and "free", so it’s important to manage memory carefully to avoid leaks or other problems. Here’s an example:
c
int add(int x, int y); // Function for adding two integers
In this example, we’re calling our “add” function with two integer arguments and storing the result in a variable called “result”. We don’t need to worry about memory management because “add” doesn’t allocate or deallocate memory dynamically. However, if “add” did allocate memory using “malloc”, we would need to ensure that we call “free” when we were done with the memory to avoid a leak.
c
int main() {
int result = add(5, 3); // Call the add function with two integer parameters and store the result in a variable