Sei sulla pagina 1di 16

PRESENTATION ON

FUNCTION IN C

Submitted to: Submitted by:


Vipul Sir Abhishek Gautam
BCA, 3rd sem., sec-A
Roll nu. -> 748
CONTENTS
1. INTRODUCTION and ADVANTAGES
2. TYPES OF FUNCTION
3. ELEMENTS OF USER DEFINED FUNCTIONS
4. PARAMETER and RETURN VALUE
5. CATEGORIES OF FUNCTION
Introduction Advantages
It is a block of statements that is designed 1. It provides better readability and easy to
perform a specific task. update.
It is a subpart of program. 2. It is easier to debug.
It enables modular programming. 3. It is allowing to be called many times i.e.
Main() is the function from where execution code reusability.
starts. 4. It is much easier to write the larger
Same function can be accessed from program.
different places and called many times
within a program.
Types of Function
LIBRARY FUNCTION USER-DEFINED FUNCTION

1) These definitions are already written in 1) These definitions are written by


c library. programmer.
2) Programmer cannot make any changes 2) Programmer can update or make any
in these functions. changes in these functions.
3) To use there functions, we must 3) These don’t have header files. But these
include header files such as stdio.h, functions must be declared.
conio.h, math.h etc.
4) For instance, printf(), scanf(),getch(),
putchar() etc.
Elements of User defined functions
• Function prototype declaration.
• Function call.
• Function definition.
Function prototype declaration
• It contains description of function that is used later in the program i.e.
return type, function name and its parameter.
• It specify the type of value that is to be returned from the function and that
is to be passed to the function and function name.
• It is written before the function call.

• Syntax:
Return_type function_name(parameter);
EXAMPLE: void sum(int, int);
Function call
• This tells compiler when to execute function definition.
• When a function call is executed, the control jumps to the function definition
where actual code gets executed and returns to same function call once
completed.

• This is performed inside the main function or any other function.


• A function can also call itself. This is called recursion.

• Syntax:
Function_name(parameter);
EXAMPLE: sum(x , y);
Function definition
• This provides the actual code of the function and also known as body of the
function.
• The actual task of the function is implemented in function definition.
• The code is to be written in these braces {}.
• Syntax:
Return_type function_name(parameter)
{
statement 1;
statement 2;
………….
}
Parameter
Parameter are those data values that are passes from calling function to called
function.
When function gets executed in program, control is transferred from calling
function to called function. It may carry one or more value. These values are
called parameter.
These are of two types: ACTUAL and FORMAL.
Actual parameter are specified in calling function and
Formal parameter are specified in called function.
When function gets executed, actual parameter values are copied into formal
parameter. (starts from first argument).
Both must be same in number, data type and order.
Return statement
And while coming back to called function, it may carry a single value called return
value.
Syntax:
return value or variable;
It serves two purposes:
i. On executing return statement, it immediately transfer control back to calling function.
ii. It returns value to calling function.
iii. A function return only one value at a time.
• #include<stdio.h> //header files.
EXAMPLE • #include<conio.h>
• void sum(int , int); //prototype declaration.
• void main()
• {
Return type  void • int a,b;
• clrscr();
• printf("\nEnter the two integers:\n");
• scanf("%d%d",&a,&b);
• sum(a,b); //function call or calling function.
ACTUAL PARAMETER • getch();
• }
• void sum(int x, int y)
• {
FORMAL PARAMETER
• int sum; //function body or definition.
• sum=x + y;
• printf("\nSUM = %d",sum);
• }


Function Categories
1) Function with no parameter and no return type.
2) Function with parameter and no return type.
3) Function with no parameter and a return type.
4) Function with parameter and a return type.
Function with no parameter and no return type

Program to find area of circle

OUTPUT:
Function with parameter and no return type

Program to find area of circle

OUTPUT:
Enter radius:
3

AREA OF CIRCLE = 28.26


Function with no parameter and a return type

OUTPUT:
Enter radius:
3

AREA OF CIRCLE = 28.26


Function with parameter and a return type.
OUTPUT:

Input two integers:


54
76

Sum = 130

Potrebbero piacerti anche