A user-defined function is a function created by the programmer to perform a specific task. Functions in C help break down complex programs into smaller, manageable parts.
A function in C consists of three main parts:
The return type of a function specifies the type of value the function will return. Common return types include:
void – No value returned.int – Returns an integer.float, double – Returns floating-point values.char – Returns a character.Example:
int add(int a, int b); // Function Declaration
int add(int a, int b) { // Function Definition
return a + b;
}
int main() {
int sum = add(5, 10); // Function Call
return 0;
}
Example:
void greet(char name[]) { // Parameter
printf("Hello, %s!", name);
}
int main() {
greet("Alice"); // Argument
return 0;
}