A Function is a self-contained subprogram that can perform some specific task. Programmers called it either function or subprogram. In C language we have a lot of functions. But In c has one default function which is the called main function. The main function either executes another function or statements.
Why Functions?
- Modularity – When a programmer works on an application then the size of code increases that time function can break down a large programme into smaller parts that can be more maintainable and reusable.
- Reusability – This means in the large programme we can see common mistakes a lot of times we can write duplicate code and that reduces the reusability of the program.
Let us understand with an example. Suppose you want to look total score of a student in five subjects. You have an Array of marks for five subjects. Then probably you will use this snippet to find the total scores of students.
int student_rollno_1[5] = { 95, 80, 70, 67, 59 };
int total_score_stu_rn1 = 0;
for (int i = 0; i < 5; i++) {
int score = student_rollno_1[i];
total_score_stu_rn1 = total_score_stu_rn1 + score;
}
printf("Total Score of the roll number 1 is: %d\n", total_score_stu_rn1);
But what else when you want to know another student’s total marks? Then you will repeat the same code.
int student_rollno_1[5] = { 95, 80, 70, 67, 59 };
int total_score_stu_rn1 = 0;
for (int i = 0; i < 5; i++) {
int score = student_rollno_1[i];
total_score_stu_rn1 = total_score_stu_rn1 + score;
}
printf("Total Score of the roll number 1 is: %d\n", total_score_stu_rn1);
// For the second student
int student_rollno_2[5] = { 92, 89, 97, 70, 90 };
int total_score_stu_rn2 = 0;
for (int i = 0; i < 5; i++) {
int score = student_rollno_2[i];
total_score_stu_rn2 = total_score_stu_rn2 + score;
}
printf("Total Score of the roll number 2 is: %d\n", total_score_stu_rn2);
And what else when we need to do for the entire class then it will be difficult for us to maintain? I hope you got the point. So to overcome this problem and to reuse our logic we will use functions.
int totalNumbers(int scorecard[]) {
int total_score = 0;
for (int i = 0; i < 5; i++) {
int score = scorecard[i];
total_score = total_score + score;
}
return total_score;
}
int student_rollno_1[5] = { 95, 80, 70, 67, 59 };
int student_rollno_2[5] = { 92, 89, 97, 70, 90 };
printf("Total Score of the roll number 1 is: %d\n", totalNumbers(student_rollno_1));
printf("Total Score of the roll number 2 is: %d\n", totalNumbers(student_rollno_2));
If you can understand this code then it’s ok but if you don’t understand then no worry because we will step by step understand the whole process.
Syntax –
returnType functionName (parameters) {
// code snippet
// return value
}
Note – the return value is optional you can write a function without a return value but at that time you need to add the return type void. Like –
void msg() {
printf("Welcome to c world.");
}
msg(); // Welcome to c world.
Let’s take an easy example Suppose that we want to add two numbers and then we will simply write.
int a = 11, b = 23;
int sum = a + b;
printf("Sum is: %d", sum);
Now turn this logic into a functional way.
int sum(int a, int b) {
int result = a + b;
return result;
}
printf("Total of (11, 23) is: %d\n", sum(11, 23));
Here you can observe that we are just passing the input to function and function just using those numbers and returning the result.
Type of functions
- User Define functions – User Define functions are those functions that are created by the programmer to perform specific tasks like sum, totalNumbers functions.
- Library Functions – Whereas C language also provides a lot of functions like main, printf, and scanf functions which are called Library functions.