Introduction
Let’s talk about functions in the C programming language. The function is a super useful concept and it’s there in every programming language. Generally till now what we have learned we can write a program and a job is executed which gives the output.
If the same thing is done many times then it will be a hectic job to write the same thing again and again so we have an option which is known as Function.
So the idea of functions is you write it once and wherever you need the functionality, use it by calling that function.
A program to give you a flavor of functions in C:
#include<stdio.h>
void fun()
{
printf("fun() called\n");
}
int main()
{
printf("Before calling fun()\n");
fun();
printf("After calling fun()\n");
return 0;
}
Explanation of the functions in C
As we know every C program begins its execution from the main function so the control comes to the main function and prints Before calling fun()
as the work of printf to print whatever is written. In the next line, fun() calls the function fun() and the control enters the void fun().
Then prints fun() called.
After that, the next line of printf is printed which is After calling fun().
Then the program ends.
Output
Before calling fun()
fun() called
After calling fun()
Applications of Functions(or methods)
- Avoid code redundancy and easy maintenance
- Make code modular
takeInput()
processData()
processOutput()
- Abstraction
For example, in library functions, we do not have to worry about internal working.
- Avoid variable name collisions
Declaration and definition of functions in C
There are two ways to use functions:
- Define first and then use
- Declare first then use then define
- The above program shows the first way of using function. I am writing this again below for your clarity.
#include<stdio.h>
void fun()
{
printf("fun() called\n");
}
int main()
{
printf("Before calling fun()\n");
fun();
printf("After calling fun()\n");
return 0;
}
- For the second way please refer the below program which states Declare -> Use -> Define
#include<stdio.h>
int getMax(int x, int y); //declare
int main()
{
int a=10, b=20;
printf("%d", getMax(a,b));
return 0;
}
/*define*/
int getMax(int x, int y)
{
if(x>y)
{
return x;
}
else
{
return y;
}
Compiler error in functions in C
If we remove the declare part(declaration) then there will be a compiler error because at this point the compiler does not know if there is a getMax function or not.
If your declaration does not match with the usage in the line printf(“%d”, getMax(a,b)); then the compiler is going to throw an error. Even if the definition is correct, the compiler is going to throw an error.
If you do not write the definition of the function then also the compiler is going to throw an error. Here there is a linking concept where when the compiler looks for the getMax function and not able to find then the compiler error which is known as the linker error is going to show.
Reference
http://www.freebookcentre.net/Language/Free-C-Books-Download.html
Conclusion of Search Programs
All the programs are super simple. Try to understand, you will get it easily. Read the explanation for more clarification and stay tuned for the upcoming blogs.
Also, read
https://curledmark.in/the-largest-number-program-in-an-array-in-c-programming/
https://curledmark.in/armstrong-number-and-arrays-in-c-programming/
https://curledmark.in/prime-reversepower-programs-in-c-programming/
The Book Of Five Rings Summary
How to Talk to Anyone by Leil Lowndes
Jonathan Livingston Seagull Book Summary
Your Brain on PORN Book Summary by Gary Wilson
THE IMMORTALS OF MELUHA By Amish Tripathi full summary
Eleven Minutes by Paulo Coelho Summary
Hyperfocus by Chris Bailey Summary
Black Beauty by Anna Sewell Summary
You Can Heal Your Life by Louise Hay Summary
ATTITUDE IS EVERYTHING BY JEFF KELLER SUMMARY
THE BIOGRAPHY OF SWAMI VIVEKANANDA
THE SHAWSHANK REDEMPTION EXPLAINED
Turtles All the Way Down by John Green Story Explained
More Stories
File Handling implementation in C language
What is do-while loop in C Programming ? Loops
What are loops in C programming?For,While,Do-While