In this C language tutorial section let learn the Basics of Function.
After reading this tutorial section you will be understand the Basics of functions in C programing Language.
Fisrt of all we shall have to understand that what is function in C programming Language?.
Function:function is a block of code in c programming language , that perform specific task repeatedly.
In simple word we can say that function in C programming Language is a named block or section of code that does specific task when we ask to do it.
Till this topic we have covered basic C language syntax with examples. Functions in C allows us to organize our program code into efficient parts,
with a minimum of repetition. In C programming creating function is very simple and straightforward and brings us many benefits.
Advantages of Function in C programming.
In c programming language function has many advantages. Let study them one by one in detail.
Modular Programming:
Modular programming is the one of the design technique that focuses on separating the functionality of a program into interchangeable ,independent modules.
It is one of the modern technique in C programming where it allow programers to break a complex and large problem into various small and manageable pieces.
Modular programming makes the program very easy to develope and debug which lead to fixing errors very easily.
By using functions in C programming the programmers can easily divide a program into smaller parts to do specific task.
Reusability: One of the main advantages of function in C is that it provides code reusability. It means when you have written a code you can call a code multiple times and in different parts of the program which saves the time and effort of the developer.
Testing:Testing is a very useful advantages of function in C used by developers as by diving the code into many smaller parts it is easy for the developers to test the code. With the help of this we can test the individual component which will be helpful in debugging and the errors can be identified easily.
Efficiency: Functions in C can also make code more efficient. By breaking down a program into smaller functions, it is easier to optimize the code for speed and memory usage. This can result in faster program execution and lower memory usage.
Abstraction: One of the useful advantages of function in c is that it can be used to abstract the complex implementation details that make the code easier to use and understand.
Functions in C programming.
From the Given Diagram In this article, let have a brief introduction to functions in C Programming Language.
in Our daily lives we use calculator to perform basic mathematical calculations.
Calculator given is a electronic device that perform specific mathematical( Addition,Substraction,Multiplication,Division etc.) calculations,requires manual interaction for each operations.
when ever we perform some calculations using calculator there is a program running in the background.
We can say calculator is the program that perform some mathematical calculations.
as soon as when the user press a button on the calculator the program runs behind the calculator.
The program is the set of instruction and functions(code) that are used to perform some specific task.
From the fig. we can observe that the Calculator has the program running behind in it.
Let understand how the calculator works for mathematical calculations.
1. Addition + : + is the button on the calculator which is used to perform addition of a numbers entered by the user.
when user press the + button the function code Add() in the program associated on the button + get executed or called.The Function Add() performs the addition of numbers entered by the user and gives the addition result.
2. Substraction - : - is also the button on the calculator which is used to perform substraction of a numbers entered by the user.
when user press the - button the function code subtract() in the program associated on the button - get executed or called.The Function subtract() performs the substraction of numbers entered by the user and gives the result.
Simmilarly other operations works for the calculator.
Note: when the user press any button on the calculator the function code(program)associated on the button get called or execute.
the function code get called repeteadly when the user press the button or call the function.
Here is the simple code to understad functions in C programming.
#include <stdio.h>
int Add(int x, int y ) /* function declaration and ddefinition */
{
return x + y;
}
int main()
{
int x,y;
printf("\n Enter any two Numbers\n");
scanf("%d%d",&x,&y);
int result = Add(x, y);
printf("\nThe result %d+%d=%d", x,y,result);
return 0;
}
Previous Topic:-->> Array Assignments in C || Next topic:-->>Need for User Defined Function.
Other Topics:
Variables and Identifiers Relational Operators if-else statements Switch case While Loop Infinite while Loops C FOR Loop Infinite for Loops Continue in Loops One Dimensional Array Two Dimensional Arrays Read and Display 2D Arrays Types of functions Passing Array To Functions Nesting of Function Array vs Structure Array of Structure Structures and Functions Structures Within Structures Use Of Pointers In C File Handling In C Loops FAQ Arrays FAQ count vowels in a file Function FAQ Conditional Statements Assignments For Loops Assignments Arrays Assignments Function Assignments Structure Assignments Pointers Assignments Files Assignments Storage classes Assignments Binary Files count words,lines in a file Copy files Update File Continue in Loops break in Loops Difference Between While and Do while difference while do..while & for malloc calloc Storage Classes Operators MCQ Conditional Statements MCQ Loops MCQ Arrays MCQ Function MCQ Structure MCQ Pointers MCQ Files MCQ Storage classes MCQ