Dr. D.Y.
Patil College Of Engineering and
innovation, Talegaon.
Subject : FPL
Project : USER-DEFINED FUNCTION
Under the guidance of :--
Prof. Priyanka madam
SUBMITTED BY :
Name:-- Yogeshwari Dilip Sonar
Div:-- D
Roll No:-- 25
Class:-- First year engineering
Branch:-- Computer Engineering
contents :--
User-defined functions in C
How to use user-defined functions in C
Function prototype
C function definition
C function call
Example :--
USER-DEFINED FUNCTIONS IN C
● A user-defined function is a type of function in C language that is
defined by the user himself to perform some specific task. It provides
code reusability and modularity to our program. User-defined functions
are different from built-in functions as their working is specified by the
user and no header file is required for their usage.
● In this article, we will learn about user-defined function, function
prototype, function definition, function call, and different ways in
which we can pass parameters to a function.
4
HOW TO USE USER-DEFINED FUNCTIONS IN C
To use a user-defined function, we first have to understand the different
parts of its syntax. The user-defined function in C can be divided into
three parts:
Function Prototype
Function Definition
Function Call
5
FUNCTION PROTOTYPE
A function prototype is also known as a function declaration which
specifies the function’s name, function parameters, and return type.
The function prototype does not contain the body of the function. It is
basically used to inform the compiler about the existence of the user-
defined function which can be used in the later part of the program.
Syntax-
return_type function_name (type1 arg1, type2 arg2, ... typeN argN);
We can also skip the name of the arguments in the function prototype.
So,
return_type function_name (type1 , type2 , ... typeN);
6
7
C FUNCTION DEFINITION
● Once the function has been called, the function definition contains the
actual statements that will be executed. All the statements of the
function definition are enclosed within { } braces.
● Syntax
● return_type function_name (type1 arg1, type2 arg2 .... typeN argN) { //
actual statements to be executed // return value if any}
● Note: If the function call is present after the function definition, we can
skip the function prototype part and directly define the function.
8
C FUNCTION CALL
In order to transfer control to a user-defined function, we need to
call it. Functions are called using their names followed by round
brackets. Their arguments are passed inside the brackets.
Syntax
function_name(arg1, arg2, ... argN);
9
EXAMPLE
#include <stdio.h>
// Function prototype
int sum(int, int);
// Function definition
int sum(int x, int y)
{
int sum;
sum = x + y;
return x + y;
}
10
int main()
{
int x = 10, y = 11;
// Function call
int result = sum(x, y);
printf("Sum of %d and %d = %d ", x, y, result);
return 0;
}
Output
Sum of 10 and 11 = 21
11
THANK YOU !!