In programming, functions can be invoked in various ways based on whether they take arguments (inputs) and whether they return a value. Here’s a summary of the four types of function invocations:
No Arguments and No Return Value
The function does not take any input arguments and does not return a value.
Example:
#include
void printMessage() {
printf("Hello, World!\n");
}
int main() {
printMessage();
return 0;
}
In this example, printMessage does not take any arguments and does not return a value. It simply prints a message to the console.No Arguments and With Return Value
The function does not take any input arguments but returns a value.
Example:
#include
int getCurrentYear() {
return 2024;
}
int main() {
int year = getCurrentYear();
printf("Current Year: %d\n", year);
return 0;
}
Here, getCurrentYear does not take any arguments but returns the current year as an integer.With Arguments and No Return Value
The function takes input arguments but does not return any value.
Example:
#include
void greet(char name[]) {
printf("Hello, %s!\n", name);
}
int main() {
greet("Alice");
return 0;
}
In this example, greet takes a string argument (the name) and prints a greeting message, but it does not return a value.With Arguments and With Return Value
The function takes input arguments and returns a value.
Example:
#include
int addNumbers(int a, int b) {
return a + b;
}
int main() {
int result = addNumbers(5, 3);
printf("Sum: %d\n", result);
return 0;
}
In this example, addNumbers takes two integer arguments and returns their sum. The result is then printed in the main function.Each example demonstrates how functions can be used differently depending on whether they need arguments and whether they return a value.