Function in C Programming language

 Functions-
Functions in c language 

A function is group of statements that together perform a task. Every C program has at least one function, which is main(). Every function has a name and it is reusable. It can take parameters called as argument.

Properties-

  • Every function has a unique name. This name is used to call function from "main()" function. 
  • A  function can be called another function.
  • A function is independent and it can perform its task without intervention from or interfering with other parts of the program.
  • A function returns a value to the calling program. this is optional and depends upon the task your function is going to accomplish.

Structure of a Function

<return type>FunctionName(Argument1,Argument2,Argument3..............)
{                                                                                                                

statement1;                                                                for video click
statement2;
statement3;
}

Function prototype:

Function  prototype tells complier that we are going to use a function which will have the given name, return  type and parameters. 
Program1: Write a program to explain Function prototype
 code-
#include<Stdio.h>
double add(double x,double y); //prototype needed
 void main()
 {
  double num1,num2;
  printf("Enter two number for addition ");
scanf("%lf%lf",&num1,&num2);
double sum;
  sum=add(num1,num2);
  printf("sum=%lf\n",sum);
 }
 double add(double x,double y)
{
double result ;
result=x+y;
return result;
 } 
output-
Enter two number for addition 10.56
23.69
sum=34.250000
--------------------------------

Program-2: Write a program to find factorial of give number using function 
code-#include<stdio.h>
int fact(int n);
 void main ()
{
int  number,f;
printf("Enter the number ");
scanf("%d",&number);
f=fact(number);
printf("The factorial of the given number is %d",f);
}
int fact(int n)
{
int i, f=1;
for(i=1;i<=n;i++)
f=f*i;
return f;
}    
Output-
Enter the number 5
The factorial of the given number is 120
--------------------------------

Advantages of using functions-

  • The length of the source program can be reduced by using function at appropriate places.
  • It becomes uncomplicated to locate and separate a faulty function for further study.
  • A function can be used to keep away from rewriting the same block of codes which we are going use two or more location in a program.
  • This is especially useful if the code involved is long or complicated.



















Comments

Popular posts from this blog

CYBER SECURITY & CRACKING PASSWORDS

"The Dark Side of Social Media: How It Fuels Digital Addiction and Impacts Mental Health"

Online Platforms to Learn Coding