How to use functions in C programming Language
Functions in C Programming Language
In programming languages, there is a concept of functions that we use to implement some functionality in our code. Functions help us reuse the code and enhance its performance, as they are written once and tested again and again. In C programming we can use functions to break the code into smaller pieces that are easily maintainable. A function is a piece of code that is enclosed in curly brackets, and a function only runs and executes when it is called within a main program in C programming language.
We can also pass data from our main program or function to any user-defined function. As we already discussed, the main advantage or benefit of functions is reusability. In functions, we can define the code once, and after that, we can use the function code many times, which enhances the reusability of the code. We can also refer to functions as procedures or sub-routines in some programming languages. Basically when we run a C program the execution of the program starts from a function that is declared as void main(), compilers in programming languages look for this main() function and the execution of the program from this main function.
In C programming we can say that different functions are combined to make a single program. These functions are basically coding blocks that we create as per the requirement of our program. These functions are reusable block of code, with the help of which we can perform specific tasks. One of the main benefits of using functions is that we can declare and define the functions or some code of block one time and then we can use them as many times as we want to. This not only enhances code reusability but also greatly increases the performance of our C program.
Now let us look at the basic syntax example of function in C programming language.
Basic syntax and working of functions in C programming language
Syntax of function
#include<stdio.h> void Hello(); void Hello() { printf("Hello World C Program."); }
void Hello();
{ printf("Hello World C Program."); }
After declaring and defining a function the next step is to call a specific function within a main program or main() function. We will cover this part of calling a function in the next C programming coding example.
Simple coding examples of functions in C programming language
First coding example in C programming language
#include<stdio.h> void Hello(); void Hello() { printf("Hello World C Program."); } void main() { Hello(); _getch(); }
This is a simple piece of code in which we have declared and defined a function named Hello(). This function simply prints a message "Hello World C Program."
void Hello(); void Hello() { printf("Hello World C Program."); }
Now there are basically three steps taken, which are considered good practice, the first step is to declare a function, and after that define a function in which the code is written inside the function body. The last step is to call a function inside the main program or main() function.
Like in the above code we already discussed about function declaration and definition previously. Now we are going to look at function calling or calling a function within main() function.
Inside the void main() function we call the function like this Hello();
Hello();this is a simple way to call a function within the main program by simply writing the function name with opening and closing round brackets and ending it with a semicolon (;) symbol.
And when we execute the program and call the function it simply prints the message "Hello World C Program"
Output of the program
Hello World C Program.
Second coding example in C programming language#include<stdio.h> void Add(); void Add() { int Number1, Number2, Result = 0; printf("Enter First Number = "); scanf_s("%d", &Number1); printf("Enter Second Number = "); scanf_s("%d", &Number2); Result = Number1 + Number2; printf("Sum of Entered Numbers = %d", Result); } void main() { Add(); _getch(); }
Explanation of the code
In the second C programming coding example, we are using function and with the help of which we are simply running a program in which we are going to take two numbers as input from the user like this,
printf("Enter First Number = "); scanf_s("%d", &Number1); printf("Enter Second Number = "); scanf_s("%d", &Number2);
And after that we will simply add the result of two numbers and show it on the console screen with a message that "sum of entered numbers is =" like this,
Result = Number1 + Number2; printf("Sum of Entered Numbers = %d", Result);
And we are implementing all this functionality in a function named as Add(). And then we are simply calling the Add() method in our main() function like this,
void main()
{
Add();
_getch();
}Function calling is the part where the execution of our function begins and in function calling we simply write the name of the function with opening and closing round brackets followed by the semi colon like this,
Add();
So this is how we added two numbers in C programming language using functions.
Output of the program
Enter First Number = 50
Enter Second Number = 60
Sum of Entered Numbers = 110
Congratulations! like always we have successfully imlemented functions in C programming language.
Comments
Post a Comment