Monday, September 30, 2024

Function in C Language

Function in C

Function:-

The fundamental component of a C program that facilitates modularity and promotes code reusability is the function. Functions are employed to execute specific tasks, and they play a crucial role in enabling code reuse: by defining the code once, it can be utilized multiple times.
  • A function carries out some specific well defined tasks.

  • A function will carry out its intended contains whenever it access or called.

  • Same function can be access from several different places within a program.

  • A function will process information passed in it from the calling portion.

syntax:-

return type function_name(arguement)
{
//-----
//-----
}
return (-);

syntax of a function for designing of format like (-).

    (------------------------)
draw();

draw(){ printf("\n ----------------------------");}


A- function with no arguement and no return.

sum(); / sum(){}

B- function with arguement and no return.

sum(a,b); / sum(int x, int y){z=x+y;}

C- function with arguement and return.

c=sum(a,b); / sum(int x, int y){ z=x+y; return(z);}

Need of function for repetitive task.

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf("\n --------------------");
printf("\n Employee Details");
printf("\n --------------------");
printf("\n Name Age Salary");
printf("\n Amit 20 15000");
printf("\n Sumit 22 20000");
printf("\n --------------------");
printf("\n Devloped by");
printf("\n Pankaj Kumar Dubey");
printf("\n --------------------");
getch();
}


Function with No argument No return.

This program print dashline where it is called.

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
dashline(); // function call shows dashline
printf("\n Employee Detail");
dashline(); //function call shows dashline
printf("\n Name Age Salary");
dashline(); //function call shows dashline
printf("\n Amit 20 15000");
printf("\n Sumit 22 22000");
dashline(); //function call shows dashline
printf("\n Developed by");
printf("\n Pankaj Kumar Dubey");
dashline(); //function call shows dashline
getch();
}

//function definition.

dashline()
{
printf("\n ----------------------------");
}

Function with argument No return.

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
dashline(*); // function call shows * line.
printf("\n Employee Detail");
dashline(#); // function call shows # line.
printf("\n Name Age Salary");
dashline($); // function call shows $ line.
printf("\n Amit 20 15000");
printf("\n Sumit 22 22000");
dashline($); // function call shows # line.
printf("\n Developed by");
printf("\n Pankaj Kumar Dubey");
dashline(*); // function call shows * line.
getch();
}

//function definition - whatever character send to at 'p' i.e. printed from where it is called like that '*', '#', '$'.

dashline(char p)
{
int i;
printf("\n");
for(i=0;i<=30;i++)
{
printf("%c",p);
}
}


Function with No argument No return.

//This program has a function name sum() that demands 2 input and calculate their addition where it is called.

// this function demands different-different values every time when it is called.

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
sum();         //function with no argument i.e. sum() called here.
getch();
}

//function definition.

sum()
{
int a,b,c;
printf("\n Enter the value of a ");
scanf("%d",&a);
printf("\n Enter the value of b ");
scanf("%d",&b);
c=a+b;
printf("\n The value of sum is %d",c);
}


Function with argument No return.


//This program has a function with argument i.e. sum(a,b) used to pass the values only for calculation.

//this function returns same value every time because the values are taken in main() function before called to sum(a,b).

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a,b;
printf("\n Enter the value of a ");
scanf("%d",&a);
printf("\n Enter the value of b ");
scanf("%d",&b);
sum(a,b); //function with argument i.e. sum(a,b) called.
getch();
}

//function definition.

sum(int x, int y)
{
int z;
z=x+y;
printf("\n The value of sum is %d",z);
}


//Function with argument with return.


//This program has a function sum(a,b) used to pass the values only for calculation and return is used here.

//this function returns same value every time because the values are taken in main() function before called to sum(a,b).

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf("\n Enter the value of a ");
scanf("%d",&a);
printf("\n Enter the value of b ");
scanf("%d",&b);
c=sum(a,b); // The value is returned in variable c.
printf("\n The value of sum is %d",c);
getch();
}

//function definition.

sum(int x, int y)
{
int z;
z=x+y;
return(z);
}

No comments:

Post a Comment

Autonomous Vehicles

Autonomous Vehicles Autonomous vehicles are cars or trucks that can drive themselves without needing a human to control them. They use advan...