Functions in c
Functions in C - Interactive Learning
A function is a block of code that performs a specific task. Functions help in code reusability and better structure.
Syntax:
return_type function_name(parameters);
Example:
int add(int a, int b);
Syntax:
return_type function_name(parameters) {
// body of the function
return value;
}
Example:
int add(int a, int b) {
return a + b;
}
Syntax:
function_name(arguments);
Example:
#include <stdio.h>
int add(int, int);
int main() {
int result = add(5, 10);
printf("Sum = %d", result);
return 0;
}
These are pre-defined functions in header files.
Examples:
#include <stdio.h>
#include <math.h>
int main() {
printf("Square root: %f", sqrt(16));
printf("\nPower: %f", pow(2, 3));
return 0;
}
These are functions created by the programmer.
Example:
#include <stdio.h>
int multiply(int x, int y) {
return x * y;
}
int main() {
int result = multiply(4, 5);
printf("Multiplication = %d", result);
return 0;
}
Comments
Post a Comment