Friday 23 March 2018

Function

A function is a self contained block of statement that perform a coherent task of some kind.
Function provides modularity to the software.

Function definition :
[data type] function name (argument list)
argument declaration;
{
local variable declaration;
statements;
[return expression]
}

Example :
mul(a,b)
int a,b;
{
int y;
y = a + b;
return y;
}
When the value of y which is the addition of the values of a and b.The last two statement i.e.,
y = a + b; can be combined as
return (y)
return (a + b)

Why Functions are used ?
  1. Many program require that a specific function is repeated many times instead of writing the function code as many timers as it is required we can write it as a single function and access the same function again as many times as it is required.
  2. We can avoid writing redundant program code  of some instructions again and again.
  3. Programs with using functions are compact and easy to understand.
  4.Testing and correcting errors is easy because errors are localized and corrected.
  5.We can understand the flow of program and its code easily since the readability in enhanced while using the functions.
  6. A single function written in a program can also be used in other programs also.

#include <stdio.h>
/* function body */
int add (int x, int y){
 int z;
z = x + y;
return (z);
}
main ( )
{
int i,j,k;
i = 10;
j = 20;
k = add(i,j);  /* function call */
printf ("The value of k is %d\n",k);
}
The value of k is 30

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (114) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (89) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (743) Python Coding Challenge (195) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses