C Function Arguments and Return Value
A function can take values (arguments) and can give a value back (return value). Based on this there are four types of functions.
| Type | Arguments | Return value |
|---|---|---|
| 1 | No | No |
| 2 | Yes | No |
| 3 | No | Yes |
| 4 | Yes | Yes |
#include <stdio.h>
void hello(void) { // no argument, no return
printf("Hello\n");
}
void showSquare(int n) { // argument, no return
printf("Square = %d\n", n * n);
}
int readFive(void) { // no argument, return
return 5;
}
int multiply(int a, int b) { // argument and return
return a * b;
}
int main(void) {
hello();
showSquare(4);
printf("%d\n", readFive());
printf("%d\n", multiply(6, 7));
return 0;
}
Output:
Output of the above program is
Hello
Square = 16
5
42
Terms to know
- Parameters are the variables in the function definition (
int a, int b). - Arguments are the real values you pass when calling (
multiply(6, 7)). - A function can return only one value with
return. To get more values you can use pointers or a structure. - After
returnexecutes, the function ends at once.
Note:
C does not have default arguments or function overloading. Each function needs a different name.
Next: C Call by Value and Call by Reference.
Congratulations! Chapter Finished. Learn more about the similar topics:
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |