Anda di halaman 1dari 6

TOPIC :- FUNCTION

MADE BY:- Shubham Meshram

C functions can be classified into two categories –

library functions (eg. main) and user defined functions (eg. printf,scanf)

Elements of user defined functions

In order to make user defined functions need three elements that are
related to functions

i) functions Definition
ii) functions Call
iii) functions declaration

Functions Definition is an independent program module that is


specially written to implement the requirement of functions .
the program that calls the function is referred to as the calling
program or calling function.
The calling program should declare any function that is to be
used later in program. This is known as the function declaration
or function prototype.
Like variables, all functions in a c program must be declared, before
they are invoked. A function declaration consist of
 Function type(return type)
 Function Name
 Parameter list
 Terminating semicolon.
Syntax:-

Function/return-type function-name (parameter list);

CATEGORY OF FUCTIONS
Functions, depending on whether arguments are present or not and whether a
value is returned or not may belong to one of the following categories

Category 1: function with no argument and no return type

Category 2: function with argument and no return type

Category 3: function with no argument and return type

Category 4: function with argument and return type

Category 5: function that return multiple values.

CATEGORY 1:-

 function with no argument and no return type


/*C program to addition of a number entered by user using category1*/
#include <stdio.h>
#include<conio.h>
Void sum (void); //function prototype
Void main () //starting of main function
{
Clrscr();
Sum(); // no argument is passed
Getch();
}
Void sum() //There is no return value to calling function
main().hence, return type of sum() is void.
{
Int ans,num1,num2;
Printf(“enter two numbers:”);
Scanf(“%d%d”);
Ans=num1+num2;
Printf(“addition of two no is %d”,ans);

OUTPUT:-
Enter two numbers:2 3
Addition two no is 5
NOTE:-Function sum() is used for asking user a input, for
addition. No argument is passed and returned from sum().

CATEGORY 2:-

 function with argument and no return type


/*write a user defined function for calculating a factorial of number +using
category2*/
#include <stdio.h>
#include<conio.h>
Void fact(int num); //function prototype
Void main () //starting of main function
{ / / argument is passed
Int num;
Clrscr();
Printf(“enter any number”);
Scanf(“%d”,&num);
Fact(num);
Getch();
}
Void fact(int num) //There is no return value to calling function
main().hence, return type of fact() is void.

{
Int I,flag=1;
For(i=1;i<=num;i++)
{
Flag=flag*i;
}
Printf(“factorial is %d”,flag);
}
OUTPUT:-
Enter anynumbers:2
Factorial is 2

NOTE:- Function fact() is used for asking user a input, for


calculating factorial. An argument is passed (i.e.int num) with no
returned value is returned to its calling function.
CATEGORY 3:-

 function with no argument and return type

#include <stdio.h>
#include<conio.h>

Void fact(int num); //function prototype


Void main () //starting of main function

{ / / no argument is passed

Int ans;

Clrscr();

Ans=factorial();

Printf(“factorial is %d”,ans);

Getch();

Int factorial() //There is return value to calling function


main().hence, return type of factorial() is int.

Int I,fact=1,num;

Printf(“enter any number”);

Scanf(“%d”,&num);

For(i=1;i<=num;i++)
{
Fact=fact*i;
}
Return (fact);
}
OUTPUT:-
Enter any numbers:2
Factorial is 2

CATEGORY 4:-

 function with argument and return type

/*write a user defined function for calculating a factorial of number +using


category4*/
#include <stdio.h>
#include<conio.h>
int factorial(int num); //function prototype
Void main () //starting of main function
{ / / argument is passed
Int num,ans;
Clrscr();
Printf(“enter any number”);
Scanf(“%d”,&num);

Ans=factorial();

Printf(“factorial is %d”,ans);

Getch();
}

Int factorial(int num) //There is return value to calling function


main().hence, return type of factorial() is int with an argument of
integer type.
{

Int i,fact=1,;

For(i=1;i<=num;i++)

{
Fact=fact*i;
}
Return (fact);

OUTPUT:-
Enter any numbers:2
Factorial is 2

Anda mungkin juga menyukai