C Functions
Functions are blocks of code that can be reused within a program. They are stored under a name similar to a variable and can be called using this name.
Syntax
return_type name(parameters) {
// Code goes here
}
Functions are declared with the following pieces:
- A
return_typethat denotes the specific type of data output. - The
nameof the functions. - The
parametersare used for processing data in the function body, and the code for it to run in curly brackets.
A function named greet() that prints a greeting to the console can be declared like this:
void greet(void) {puts("Howdy!");}
The greet() function uses a void type to indicate that no output is returned after execution.
Calling a Function
After a function is declared, it can be called in the program. The syntax for this is the variables name followed by parenthesis.
#include <stdio.h>// Declaring the functionvoid greet(void) {puts("Howdy!");}// Calling the function inside main()int main() {greet();}
The output would be:
Howdy!
Arguments
The values passed to a function are known as arguments. They represent the actual input values that can be used within the function.
#include <stdio.h>void max(int x, int y) {if (x > y)printf("%d is the bigger number\n", x);elseprintf("%d is the bigger number\n", y);}int main() {int a = 20;int b = 30;max(a, b);return 0;}
Here’s another example:
#include <stdio.h>void greet(char* name) {printf("Howdy %s!\n")}int main() {greet("John Doe"); // Output: Howdy John Doe!return 0;}
Returning Values
A function is also capable of returning a value back to were it was called using the return keyword. This is useful for computing values within a function. Notice that the word proceeding the function name indicates the type of value returned.
#include <stdio.h>int add(int a, int b) {return a + b;}int main(void) {int num = add(1, 1);printf("%d\n", num);}
Here, the add() function has a return value of int.
All contributors
Christine_Yang
BrandonDusch- Anonymous contributor
christian.dinh- Anonymous contributor
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours