Data can be conveyed to functions via parameters, which function as variables within the function itself.
Parameters are delineated after the function name, residing within the parentheses. You can include multiple parameters, separating them with commas as needed.
returnType functionName(parameter1, parameter2, parameter3) { // code to be executed } |
The subsequent function accepts a character string named “name” as a parameter. Upon calling the function, we provide a name, which is then utilized within the function to output “Hello” along with the provided name for each individual.
void myFunction(char name[]) { printf(“Hello %s\n”, name); } int main() { // Hello Liam |
When a value is passed to a function, it is referred to as an argument. Thus, in the example mentioned earlier: “name” represents a parameter, whereas “Liam,” “Jenny,” and “Anja” serve as arguments. |
Within the function, you can include any number of parameters as needed.
void myFunction(char name[], int age) { printf(“Hello %s. You are %d years old.\n”, name, age); } int main() { // Hello Liam. You are 3 years old. |
Keep in mind that when dealing with multiple parameters, the function call should contain an equal number of arguments as there are parameters, and these arguments must be passed in the same sequence. |
Arrays can also be passed as arguments to a function.
void myFunction(int myNumbers[5]) { for (int i = 0; i < 5; i++) { printf(“%d\n”, myNumbers[i]); } } int main() { |
Example ExplainedThe function, myFunction, accepts an array as its parameter (int myNumbers[5]), and iterates through the array elements using a for loop. Upon calling the function within main(), we provide the myNumbers array, resulting in the output of its elements. Keep in mind that when calling the function, you only need to utilize the name of the array as an argument, like so: myFunction(myNumbers). However, in the function parameter, the complete declaration of the array is required (int myNumbers[5]). |
The void keyword, as demonstrated in the previous examples, signifies that the function is not intended to return a value. Should you desire the function to yield a value, you can specify a data type ( e.g., int or float ) instead of void and employ the return keyword within the function.
int myFunction(int x) { return 5 + x; } int main() { // Outputs 8 (5 + 3) |
This example illustrates the function returning the sum of two parameters.
int myFunction(int x, int y) { return x + y; } int main() { // Outputs 8 (5 + 3) |
Alternatively, you can assign the result to a variable.
int myFunction(int x, int y) { return x + y; } int main() { |
To illustrate a practical application of functions, let’s develop a program for converting a temperature value from Fahrenheit to Celsius.
// Function to convert Fahrenheit to Celsius float toCelsius(float fahrenheit) { return (5.0 / 9.0) * (fahrenheit – 32.0); } int main() { // Call the function with the fahrenheit value // Print the fahrenheit value // Print the result return 0; |