0% found this document useful (0 votes)
6 views

Function 1

The document defines functions and describes their properties. Functions are subprograms that perform tasks and can be predefined or user-defined. User-defined functions require a prototype, call, and definition. Functions are classified based on their return type and parameters.

Uploaded by

Pavankumar Bhise
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Function 1

The document defines functions and describes their properties. Functions are subprograms that perform tasks and can be predefined or user-defined. User-defined functions require a prototype, call, and definition. Functions are classified based on their return type and parameters.

Uploaded by

Pavankumar Bhise
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Function

Contents :

#Definition

#Types :
I) Pre defined( in-built ) function
II) User-defined function

# User- defined function ( Function )


Basics:
i) Function prototype / declarartion / signature
ii) Function call ( invoke)
iii) Function definition

# Types ( Classsification )

#Property :
i) Calling method :
i) call by value
ii) call by reference
ii) Global & local variable
iii) Recursion
Definition :

It is a sub program which is a self- contained block used to perform a


specific task.

Or

It is set of stmt(s) that takes inputs, perform computation ( process ) &


results into an output value .

Types:

There are two types of functions:

i)Pre defined (In-built ) function

ii) User – defined function


i)Pre defined ( Built- in) function

i) It is also called as standard libaray functions .

ii) such as
<stdio.h> --> printf(), scanf() , putchar () , getchar() etc.

<math.h> --> pow( ) , sqrt( )

<string.h> --> strcpy( ) ,strcat( )

Iii) These functions are already defined in header files need to include only
in link section.

ii) user – defined function

i) As the name itself indicating it is user program i.e. user has to create .

ii) Every user defined function must consists of :

I) function prototype ( function declaration )


II) function call
III) function definition
I) Function prototype / declarartion / signature

i) one can create function by using following syntax

ii) syntax :

return _type function _ name ( parameter list );

Where,
return _ type : Here , we declare the data type of the value returned by
functions. It may be char, int ,float or double type.
If no return then it should be void

function_name: It is the name of function .

Parameter list : All the parameters to be passed into the function.


A function may or may not require parameters. Hence, the parameter list
is optional.

Iii) Example :

1) int add ( int , int );


function-name : add
return_type : integer
number of parameters : two
type of parameter 1 & 2 : integer

2) void fun ( int , char );


function-name : fun
return_type : No return : It is an empty return.
number of parameters : 2
type of parameter 1 : int
type of parameter2 : char
ii) function call :
i) When a main() { calling function } calls( invokes ) a function
( known as function call ) then Program ctrl is transferred to the called
function .

A called function performs a defined task & it returns back into


the main () ( calling function ).

ii) syntax
function-name( parameters );

iii) Example
add ( n1 , n2) ;

iii) function definition :

i) It consists of function header { return- type , function name,


parameters} and function's body{ block}.

ii) example

1) int add ( int m1, int m2)


{
printf(“\n %d\n”,m1+m2);
}

2) void fun ( int n, char c)


{
printf(“value of n=%d\n”, n);
printf(“value of c=%c\t”,c);

}
Example :

/* Title : program for addition of two numbers using function ( input :


main() , process : add() ,output : main() )*/

#include<stdio.h>

int add (int , int ); // function declaration

void main( )
{

int n1 , n2 , a;

printf("Enter the value for n1 & n2");


scanf("%d%d",&n1,&n2); // input stmt

a = add ( n1 , n2 ); // function call

printf(" addition = %d",a);// output stmt


}

int add( int m1 ,int m2 ) // function definition


{

return ( m1+m2);
}
Types ( Claasification )

i) A Function can be claasified into four types based upon return type &
parameter

I) A function with return type & with parameter

II) A function with return type & without parameter

III) A function without return type & with parameter

IV) A function without return type & without parameter


Example :
WAP to read a number & ckeck if it is prime number using function

I) A function with return type & with parameter

#include<stdio.h>

int prime ( int ); // function defintion

void main( )
{
int n;

printf(“\n Enter the i/p for n \n”);


scanf(“%d”, & n); // i/p stmt

prime( n) ; function call


}

int prime ( int m)


{
int a;

for( a=2 ; a<=m ; a++)


{
if( m% a == 0 )
break;
}
if( m == a)
printf(“\n given number is prime \n”);

return( 35);
}
O/P : I) first iteration

Enter the i/p for n


5
given number is prime
II) Second iteration

Enter the i/p for n


51

II) A function with return type & without parameter

#include<stdio.h>

int prime ( ); // function defintion

void main( )
{
prime( ) ; function call
}

int prime ( )
{
int a , m ;

printf(“\n Enter the i/p for m \n”);


scanf(“%d”, & m); // i/p stmt

for( a=2 ; a<=m ; a++)


{
if( m% a == 0 )
break;
}
if( m == a)
printf(“\n given number is prime \n”);

return( 35);
}
O/P :
Enter the i/p for m
11
given number is prime
III) A function without return type & with parameter

#include<stdio.h>

void prime ( int ); // function defintion

void main( )
{
printf(“\n Enter the i/p for n \n”);
scanf(“%d”, &n); // i/p stmt

prime( n ) ; function call


}

int prime ( int m )


{
int a ;

for( a=2 ; a<=m ; a++)


{
if( m% a == 0 )
break;
}
if( m == a)
printf(“\n given number is prime \n”);

return( 35);
}

O/P :
Enter the i/p for n
17
given number is prime
IV) A function without return type & without parameter

#include<stdio.h>

void prime ( ); // function defintion

void main( )
{
prime( ) ; function call
}

void prime ( )
{
int a , m ;

printf(“\n Enter the i/p for m \n”);


scanf(“%d”, &m); // i/p stmt

for( a=2 ; a<=m ; a++)


{
if( m% a == 0 )
break;
}
if( m == a)
printf(“\n given number is prime \n”);

O/P :
Enter the i/p for n
29
given number is prime

You might also like