SlideShare a Scribd company logo
9
Most read
10
Most read
11
Most read
• Introduction
• User Defined Functions
• Parts of the Function
1
Introduction
Function is a self contained program. In case the size of
the program is large, it becomes difficult to maintain it
and it is also hard to identify the flow of data. It is
preferred to divide the large program into small
modules called functions. Each function takes the data
that are provided by the main() function and carries out
operations as per the requirement, and results can be
written to the calling function.
2
Advantages of the functions are as follows:
• Reusability: A function once written can be invoked again and
again, thus helping us to reuse the code and removing data
redundancy.
• Modularity: Functions can help us in breaking a large, hard to
manage problem into smaller manageable sub-problems. It is easier
to understand the logic of sub-programs.
• Reduced Program Size: Functions can reduce the size of the
program by removing data redundancy.
• Easy Debugging: Using functions, debugging of a program becomes
very easy, as it is easier to locate and rectify the bug in the program
if functions are used.
• Easy Updating: If we need to update some code in the program,
then it is much more easier in case we have used functions.
3
#include<stdio.h>
// function prototype, also called function declaration
float square ( float x );
// main function, program starts from here
int main( ) {
float m, n ;
printf ( "nEnter some number for finding square n");
scanf ( "%f", &m ) ;
// function call
n = square ( m ) ;
printf ( "nSquare of the given number %f is %f",m,n ); }
float square ( float x ) {
float p ; Function Definition
p = x * x ;
return ( p ) ; } 4
5
Parts of the function
Parts of a function are as follows.
• Function prototype declaration
• Function call
• Definition of a function
• Actual and formal arguments
• Return statement
6
Function prototype declaration
A prototype statement helps the compiler to check the return and
argument types of the function.
A function prototype declaration consists of function’s return type,
name, and arguments list. It tells the compiler
Name of the function
Type of value returned
The type and number of arguments
The general Syntax: <Data type> <function name> (with/without
arguments list with/without data types)
Example : float sum (float, int);
float sum (float x, int y);
When the programmer defines the function, the definition of
function must be same like its prototype declaration.
7
Function Call
A function is a latent(hidden) body. It gets activated only when a call
to a function is invoked. A function must be called by its name
followed by argument, or without argument, list enclosed in
parenthesis and terminated by semicolon.
The general Syntax of function call is as follows:
function-name(with/without argument list);
Example : sum (x,y);
product (x,y);
Function Definition
The first line is called function definition and function body follows
it. The function definition and function prototype should match
with each other. The function body is enclosed within curly
braces. The function can be defined anywhere. If the function is
defined before its caller, then its prototype declaration is
optional. 8
Syntax of function call is as follows:
return_data_type function-name (argument/parameter list);
{
variable declarations
function statements
}
Actual and Formal Argument
The arguments declared in caller function and given in the function call are called
actual arguments. The arguments declared in the function definition are
known as formal arguments.
9
variables y and z are actual arguments and variables j and
k are formal arguments. The values of y and z are stored
in j and k, respectively. The values of actual arguments
are assigned to formal arguments. The function uses
formal arguments for computing.
The return Statement
The return statement is used to return value to the caller
function. The return statement returns only one value
at a time. When a return statement is encountered,
complier transfers the control of the program to caller
function.
The syntax of return statement is as follows:
return (variable name); or return variable name;
10
Factorial of a Number
#include <stdio.h>
long factorial(int);
int main() {
int number;
printf("Enter a number to calculate it's factorialn");
scanf("%d", &number);
printf("%d! = %ldn", number, factorial(number));
return 0; }
long factorial(int n) {
int i;
long fact = 1;
for (i = 1; i <= n; i++)
fact= fact * i;
return fact; } 11
Sum of digits in a given number
#include<stdio.h> int getSum(int num) {
int getSum(int); int sum =0,r;
int main() { if(num!=0) {
int num,sum; r=num%10;
printf("Enter a number: "); sum=sum+r;
scanf("%d",&num); getSum(num/10); }
sum = getSum(num); return sum; }
printf("Sum of digits of number: %d",sum);
return 0;
}
12
PASSING ARGUMENTS
The main objective of passing argument to function is
message passing. The message passing is also known as
communication between two functions, that is between
caller and called functions. There are three methods by
which we can pass values to the function. These
methods are as follows:
Call by value (pass by value)
Call by address (pass by address)
The arguments used to send values to function are known
as input arguments. The arguments used to return
result are known as output arguments.
13
While passing values to the function, the following
conditions should be fulfilled.
1. The data type and number of actual and formal
arguments should be same both in caller and callee
functions.
2. Extra arguments are discarded if they are declared.
3. If the formal arguments are more than the actual
arguments, then the extra arguments appear as
garbage.
4. Any mismatch in the data type will produce the
unexpected result.
14
Call by Value
In this type, values of actual arguments are passed to the
formal arguments and operation is done on the formal
arguments. Any change in the formal arguments does
not effect to the actual arguments because formal
arguments are photocopy of actual arguments. Hence,
when function is called by call by value method, it does
not affect the actual contents of actual arguments. The
advantage of this call by value is that actual parameters
are fully protected because their values are not
changed when control is returned to the calling
function.
15
Call by Value
#include <stdio.h>
void swap(int x, int y);
int main () {
int a = 100, b = 200;
printf("Before swap, value of a : %dn", a );
printf("Before swap, value of b : %dn", b );
swap(a, b);
printf("After swap, value of a : %dn", a );
printf("After swap, value of b : %dn", b );
return 0;
}
void swap(int x, int y) {
int temp;
temp = x;
x = y;
y = temp;
}
16
Call by Address
In this type, instead of passing values, addresses of actual
parameters are passed to the function by using
pointers. Function operates on addresses rather than
values. Here the formal arguments are pointers to the
actual arguments. Because of this, when the values of
formal arguments are changed, the values of actual
parameters also change. Hence changes made in the
argument are permanent.
17
#include <stdio.h>
void swap(int *x, int *y);
int main () {
int a = 100, b= 200;
printf("Before swap, value of a : %dn", a );
printf("Before swap, value of b : %dn", b );
/* calling a function to swap the values.* &a indicates pointer to a ie. address
of variable a and * &b indicates pointer to b ie. address of variable b. */
swap(&a, &b);
printf("After swap, value of a : %dn", a );
printf("After swap, value of b : %dn", b );
return 0; }
void swap(int *x, int *y) {
int temp;
temp = *x; /* save the value at address x */
*x = *y; /* put y into x */
*y = temp; /* put temp into y */
} 18
To check wheter a given integer is prime or not using functions
#include<stdio.h>
int check_prime(int);
main()
{
int n, result;
printf("Enter an integer to check whether it is prime or not.n");
scanf("%d",&n);
result = check_prime(n);
if ( result == 1 )
printf("%d is prime.n", n);
else
printf("%d is not prime.n", n);
return 0;
}
19
int check_prime(int a)
{
int c;
for ( c = 2 ; c <= a - 1 ; c++ )
{
if ( a%c == 0 )
return 0;
}
if ( c == a )
return 1;
}
20

More Related Content

What's hot (20)

PPTX
predefined and user defined functions
Swapnil Yadav
 
PPTX
C functions
University of Potsdam
 
PPTX
User defined functions in C
Harendra Singh
 
PPTX
C function presentation
Touhidul Shawan
 
PPT
Functions
Online
 
PPTX
parameter passing in c#
khush_boo31
 
PDF
Function in C
Dr. Abhineet Anand
 
PPTX
Functions in c
sunila tharagaturi
 
PPTX
Types of function call
ArijitDhali
 
PPTX
Function in c program
umesh patil
 
PPTX
Functions in c language
tanmaymodi4
 
PPT
User defined functions in C programmig
Appili Vamsi Krishna
 
PDF
Pointers and call by value, reference, address in C
Syed Mustafa
 
PDF
Lecture20 user definedfunctions.ppt
eShikshak
 
PPTX
Function in C program
Nurul Zakiah Zamri Tan
 
PPTX
Functions in C
Kamal Acharya
 
PPT
Recursion in c
Saket Pathak
 
PPTX
Presentation on Function in C Programming
Shuvongkor Barman
 
PPTX
functions in C and types
mubashir farooq
 
PPTX
Control Structures in C
sana shaikh
 
predefined and user defined functions
Swapnil Yadav
 
User defined functions in C
Harendra Singh
 
C function presentation
Touhidul Shawan
 
Functions
Online
 
parameter passing in c#
khush_boo31
 
Function in C
Dr. Abhineet Anand
 
Functions in c
sunila tharagaturi
 
Types of function call
ArijitDhali
 
Function in c program
umesh patil
 
Functions in c language
tanmaymodi4
 
User defined functions in C programmig
Appili Vamsi Krishna
 
Pointers and call by value, reference, address in C
Syed Mustafa
 
Lecture20 user definedfunctions.ppt
eShikshak
 
Function in C program
Nurul Zakiah Zamri Tan
 
Functions in C
Kamal Acharya
 
Recursion in c
Saket Pathak
 
Presentation on Function in C Programming
Shuvongkor Barman
 
functions in C and types
mubashir farooq
 
Control Structures in C
sana shaikh
 

Similar to User Defined Functions in C (20)

PDF
Functions in C++.pdf
LadallaRajKumar
 
PDF
PSPC-UNIT-4.pdf
ArshiniGubbala3
 
PPTX
Function C programming
Appili Vamsi Krishna
 
PDF
programlama fonksiyonlar c++ hjhjghjv jg
uleAmet
 
PPTX
CHAPTER THREE FUNCTION.pptx
GebruGetachew2
 
PPTX
C function
thirumalaikumar3
 
PPTX
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap
 
PPTX
Function in c
CGC Technical campus,Mohali
 
PPTX
Functionincprogram
Sampath Kumar
 
PPTX
Detailed concept of function in c programming
anjanasharma77573
 
PDF
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 
PDF
Chapter_1.__Functions_in_C++[1].pdf
TeshaleSiyum
 
PPTX
Functions (Computer programming and utilization)
Digvijaysinh Gohil
 
PDF
Functions
Pragnavi Erva
 
PPTX
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde3
 
PDF
Function
Kathmandu University
 
PPTX
User defined function in C.pptx
Rhishav Poudyal
 
PPTX
Functions
Golda Margret Sheeba J
 
PDF
cp Module4(1)
Amarjith C K
 
PPT
Ch4 functions
Hattori Sidek
 
Functions in C++.pdf
LadallaRajKumar
 
PSPC-UNIT-4.pdf
ArshiniGubbala3
 
Function C programming
Appili Vamsi Krishna
 
programlama fonksiyonlar c++ hjhjghjv jg
uleAmet
 
CHAPTER THREE FUNCTION.pptx
GebruGetachew2
 
C function
thirumalaikumar3
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap
 
Functionincprogram
Sampath Kumar
 
Detailed concept of function in c programming
anjanasharma77573
 
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 
Chapter_1.__Functions_in_C++[1].pdf
TeshaleSiyum
 
Functions (Computer programming and utilization)
Digvijaysinh Gohil
 
Functions
Pragnavi Erva
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde3
 
User defined function in C.pptx
Rhishav Poudyal
 
cp Module4(1)
Amarjith C K
 
Ch4 functions
Hattori Sidek
 
Ad

Recently uploaded (20)

DOCX
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
GitOps_Without_K8s_Training simple one without k8s
DanialHabibi2
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PPTX
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
PDF
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PPTX
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PPTX
Thermal runway and thermal stability.pptx
godow93766
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
GitOps_Without_K8s_Training simple one without k8s
DanialHabibi2
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Hashing Introduction , hash functions and techniques
sailajam21
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
MRRS Strength and Durability of Concrete
CivilMythili
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Thermal runway and thermal stability.pptx
godow93766
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Design Thinking basics for Engineers.pdf
CMR University
 
Ad

User Defined Functions in C

  • 1. • Introduction • User Defined Functions • Parts of the Function 1
  • 2. Introduction Function is a self contained program. In case the size of the program is large, it becomes difficult to maintain it and it is also hard to identify the flow of data. It is preferred to divide the large program into small modules called functions. Each function takes the data that are provided by the main() function and carries out operations as per the requirement, and results can be written to the calling function. 2
  • 3. Advantages of the functions are as follows: • Reusability: A function once written can be invoked again and again, thus helping us to reuse the code and removing data redundancy. • Modularity: Functions can help us in breaking a large, hard to manage problem into smaller manageable sub-problems. It is easier to understand the logic of sub-programs. • Reduced Program Size: Functions can reduce the size of the program by removing data redundancy. • Easy Debugging: Using functions, debugging of a program becomes very easy, as it is easier to locate and rectify the bug in the program if functions are used. • Easy Updating: If we need to update some code in the program, then it is much more easier in case we have used functions. 3
  • 4. #include<stdio.h> // function prototype, also called function declaration float square ( float x ); // main function, program starts from here int main( ) { float m, n ; printf ( "nEnter some number for finding square n"); scanf ( "%f", &m ) ; // function call n = square ( m ) ; printf ( "nSquare of the given number %f is %f",m,n ); } float square ( float x ) { float p ; Function Definition p = x * x ; return ( p ) ; } 4
  • 5. 5
  • 6. Parts of the function Parts of a function are as follows. • Function prototype declaration • Function call • Definition of a function • Actual and formal arguments • Return statement 6
  • 7. Function prototype declaration A prototype statement helps the compiler to check the return and argument types of the function. A function prototype declaration consists of function’s return type, name, and arguments list. It tells the compiler Name of the function Type of value returned The type and number of arguments The general Syntax: <Data type> <function name> (with/without arguments list with/without data types) Example : float sum (float, int); float sum (float x, int y); When the programmer defines the function, the definition of function must be same like its prototype declaration. 7
  • 8. Function Call A function is a latent(hidden) body. It gets activated only when a call to a function is invoked. A function must be called by its name followed by argument, or without argument, list enclosed in parenthesis and terminated by semicolon. The general Syntax of function call is as follows: function-name(with/without argument list); Example : sum (x,y); product (x,y); Function Definition The first line is called function definition and function body follows it. The function definition and function prototype should match with each other. The function body is enclosed within curly braces. The function can be defined anywhere. If the function is defined before its caller, then its prototype declaration is optional. 8
  • 9. Syntax of function call is as follows: return_data_type function-name (argument/parameter list); { variable declarations function statements } Actual and Formal Argument The arguments declared in caller function and given in the function call are called actual arguments. The arguments declared in the function definition are known as formal arguments. 9
  • 10. variables y and z are actual arguments and variables j and k are formal arguments. The values of y and z are stored in j and k, respectively. The values of actual arguments are assigned to formal arguments. The function uses formal arguments for computing. The return Statement The return statement is used to return value to the caller function. The return statement returns only one value at a time. When a return statement is encountered, complier transfers the control of the program to caller function. The syntax of return statement is as follows: return (variable name); or return variable name; 10
  • 11. Factorial of a Number #include <stdio.h> long factorial(int); int main() { int number; printf("Enter a number to calculate it's factorialn"); scanf("%d", &number); printf("%d! = %ldn", number, factorial(number)); return 0; } long factorial(int n) { int i; long fact = 1; for (i = 1; i <= n; i++) fact= fact * i; return fact; } 11
  • 12. Sum of digits in a given number #include<stdio.h> int getSum(int num) { int getSum(int); int sum =0,r; int main() { if(num!=0) { int num,sum; r=num%10; printf("Enter a number: "); sum=sum+r; scanf("%d",&num); getSum(num/10); } sum = getSum(num); return sum; } printf("Sum of digits of number: %d",sum); return 0; } 12
  • 13. PASSING ARGUMENTS The main objective of passing argument to function is message passing. The message passing is also known as communication between two functions, that is between caller and called functions. There are three methods by which we can pass values to the function. These methods are as follows: Call by value (pass by value) Call by address (pass by address) The arguments used to send values to function are known as input arguments. The arguments used to return result are known as output arguments. 13
  • 14. While passing values to the function, the following conditions should be fulfilled. 1. The data type and number of actual and formal arguments should be same both in caller and callee functions. 2. Extra arguments are discarded if they are declared. 3. If the formal arguments are more than the actual arguments, then the extra arguments appear as garbage. 4. Any mismatch in the data type will produce the unexpected result. 14
  • 15. Call by Value In this type, values of actual arguments are passed to the formal arguments and operation is done on the formal arguments. Any change in the formal arguments does not effect to the actual arguments because formal arguments are photocopy of actual arguments. Hence, when function is called by call by value method, it does not affect the actual contents of actual arguments. The advantage of this call by value is that actual parameters are fully protected because their values are not changed when control is returned to the calling function. 15
  • 16. Call by Value #include <stdio.h> void swap(int x, int y); int main () { int a = 100, b = 200; printf("Before swap, value of a : %dn", a ); printf("Before swap, value of b : %dn", b ); swap(a, b); printf("After swap, value of a : %dn", a ); printf("After swap, value of b : %dn", b ); return 0; } void swap(int x, int y) { int temp; temp = x; x = y; y = temp; } 16
  • 17. Call by Address In this type, instead of passing values, addresses of actual parameters are passed to the function by using pointers. Function operates on addresses rather than values. Here the formal arguments are pointers to the actual arguments. Because of this, when the values of formal arguments are changed, the values of actual parameters also change. Hence changes made in the argument are permanent. 17
  • 18. #include <stdio.h> void swap(int *x, int *y); int main () { int a = 100, b= 200; printf("Before swap, value of a : %dn", a ); printf("Before swap, value of b : %dn", b ); /* calling a function to swap the values.* &a indicates pointer to a ie. address of variable a and * &b indicates pointer to b ie. address of variable b. */ swap(&a, &b); printf("After swap, value of a : %dn", a ); printf("After swap, value of b : %dn", b ); return 0; } void swap(int *x, int *y) { int temp; temp = *x; /* save the value at address x */ *x = *y; /* put y into x */ *y = temp; /* put temp into y */ } 18
  • 19. To check wheter a given integer is prime or not using functions #include<stdio.h> int check_prime(int); main() { int n, result; printf("Enter an integer to check whether it is prime or not.n"); scanf("%d",&n); result = check_prime(n); if ( result == 1 ) printf("%d is prime.n", n); else printf("%d is not prime.n", n); return 0; } 19
  • 20. int check_prime(int a) { int c; for ( c = 2 ; c <= a - 1 ; c++ ) { if ( a%c == 0 ) return 0; } if ( c == a ) return 1; } 20