SlideShare a Scribd company logo
3
Most read
7
Most read
8
Most read
1
Function in C Language
2
1.What is C Function?
In short: A function is a group of statements that together
perform a task.
C functions are basic building blocks in a program.
A large C program is divided into basic building blocks
called C function. C function contains set of instructions
enclosed by “{ }” which performs specific operation in a C
program. Actually, Collection of these functions creates a C
program
3
Classification Of
Function
Library
function
User define
function
- main()
-printf()
-scanf()
-sqrt()
-getchar()
A large program in c can be divided to many subprogram
The subprogram posses a self contain components and have well define
purpose.
The subprogram is called as a function
Basically a job of function is to do something
C program contain at least one function which is main().
2. Types of C functions
4
Standard Library Functions:
Library functions in C language are inbuilt functions which are grouped together and placed in a
common place called library. Each library function in C performs specific operation.
The standard library functions are built-in functions in C programming to handle tasks such as
mathematical computations, I/O processing, string handling etc.
These functions are defined in the header file. When you include the header file, these functions
are available for use. For example:
➤ The printf() is a standard library function to send formatted output to the screen (display
output on the screen). This function is defined in "stdio.h" header file.
➤ There are other numerous library functions defined under "stdio.h", such as scanf(), printf(),
getchar() etc. Once you include "stdio.h" in your program, all these functions are available for
use.
User-defined Functions:
As mentioned earlier, C allow programmers to define functions. Such functions created by
the user are called user-defined functions.
Depending upon the complexity and requirement of the program, you can create as many
user-defined functions as you want.
5
 C functions are used to avoid rewriting same logic/code again
and again in a program.
 There is no limit in calling C functions to make use of same
functionality wherever required.
 We can call functions any number of times in a program and
from any place in a program.
 A large C program can easily be tracked when it is divided into
functions.
 The core concept of C functions are, re-usability, dividing a big
task into small pieces to achieve the functionality and to
improve understandability of very large C programs.
3. Uses of C functions:
6
4. Advantages of
function
It is much easier to write a structured program where a large
program can be divided into a smaller, simpler task.
Allowing the code to be called many times
Easier to read and update
It is easier to debug a structured program where there error is
easy to find and fix
7
C functions aspects syntax
function definition
Return_type function_name
(arguments list)
{ Body of function; }
function call function_name (arguments list);
function declaration
return_type function_name
(argument list);
There are 3 aspects in each C function. They are,
 Function declaration or prototype – this informs compiler about
the function name, function parameters and return value’s data
type.
 Function call - this calls the actual function
 Function definition – this contains all the statements to be
executed.
5. C function declaration, function call and function definition:
8
 As you know, functions should be declared and defined before calling in a C program.
 In the below program, function “square” is called from main function.
 The value of “m” is passed as argument to the function “square”. This value is multiplied by itself in this function
and multiplied value “p” is returned to main function from function “square”.
6. Simple Example Program for C Function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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 ) // function definition
{
float p ;
p = x * x ;
Enter some number for finding square 2
Square of the given number 2.000000 is
4.000000
Output:
Enter some number for finding square 4
Square of the given number 2.000000 is
16.000000
https://www.programiz.com/online-compiler/
4Cc8QE1mAphiO
9
7. How To Call C Functions In A Program?
There are two ways that a C function can be called from a program.
They are,
1.Call by value
2.Call by reference
1. Call By Value:
•In call by value method, the value of the variable is passed to the function as
parameter.
•The value of the actual parameter can not be modified by formal parameter.
•Different Memory is allocated for both actual and formal parameters. Because,
value of actual parameter is copied to formal parameter.
10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
// function prototype, also called function declaration
void swap(int a, int b);
int main()
{
int m = 22, n = 44;
// calling swap function by value
printf(" values before swap m = %d nand n = %d", m,
n);
swap(m, n);
}
void swap(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
values before swap m = 22
and n = 44
values after swap m = 44
and n = 22
 In this program, the
values of the
variables “m” and “n”
are passed to the
function “swap”.
 These values are
copied to formal
parameters “a” and
“b” in swap function
and used.
Example Program For C Function (Using Call By Value):
11
2. Call by reference:
•In call by reference method, the
address of the variable is passed to
the function as parameter.
•The value of the actual parameter
can be modified by formal
parameter.
•Same memory is used for both
actual and formal parameters since
only address is used by both
parameters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<stdio.h>
// function prototype, also called function declaration
void swap(int *a, int *b);
int main()
{
int m = 22, n = 44;
// calling swap function by reference
printf("values before swap m = %d n and n = %d",m,n);
swap(&m, &n);
}
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
printf("n values after swap a = %d nand b = %d", *a, *b);
}
Example Program For C Function
(Using Call By Reference):
•In this program, the address of the
variables “m” and “n” are passed to
the function “swap”.
•These values are not copied to
formal parameters “a” and “b” in
swap function.
•Because, they are just holding the
address of those variables.
•This address is used to access and
change the values of the variables.
values before swap m = 22
and n = 44
values after swap a = 44
and b = 22
Output:
12
Thank You

More Related Content

PPTX
arrays of structures
DOCX
C programming tutorial
PPTX
Fundamental of C Programming (Data Types)
PPTX
Union in C programming
PDF
Introduction of matrices
PPT
Searching in c language
PPTX
Dynamic memory allocation
PPTX
A Presentation About Array Manipulation(Insertion & Deletion in an array)
arrays of structures
C programming tutorial
Fundamental of C Programming (Data Types)
Union in C programming
Introduction of matrices
Searching in c language
Dynamic memory allocation
A Presentation About Array Manipulation(Insertion & Deletion in an array)

What's hot (20)

PDF
Character Array and String
PPTX
Presentation on array
PPTX
Data Types In C
PPTX
lec 2- array declaration and initialization.pptx
PPT
Arrays
PPTX
Array in c programming
PPTX
PPTX
Array and string
PDF
Functions and modules in python
PPTX
OOPs & C++ UNIT 3
PPT
Arrays in c
PPTX
Java Array String
PPT
Introduction to Matrices
PPTX
Pointers in C
PPT
structure and union
PPT
Array in c
PDF
Basic of the C language
PPTX
Variables in C and C++ Language
PPTX
Multiple integral(tripple integral)
Character Array and String
Presentation on array
Data Types In C
lec 2- array declaration and initialization.pptx
Arrays
Array in c programming
Array and string
Functions and modules in python
OOPs & C++ UNIT 3
Arrays in c
Java Array String
Introduction to Matrices
Pointers in C
structure and union
Array in c
Basic of the C language
Variables in C and C++ Language
Multiple integral(tripple integral)
Ad

Similar to C functions by ranjan call by value and reference.pptx (20)

PPTX
Presentation on Function in C Programming
PPSX
Function in c
PPTX
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
PDF
cp Module4(1)
PPTX
Function C programming
PPTX
Functionincprogram
PPTX
Lecture 1_Functions in C.pptx
PDF
[ITP - Lecture 12] Functions in C/C++
PPTX
Unit-III.pptx
PPTX
Lecture_5_-_Functions_in_C_Detailed.pptx
PPTX
unit_2.pptx
PPTX
Functions and Header files ver very useful
PDF
USER DEFINED FUNCTIONS IN C.pdf
PPTX
structured Programming Unit-7-Functions.pptx
PDF
Unit 3 (1)
PPTX
unit_2 (1).pptx
DOC
c.p function
PPTX
Programming in C FUNCTION Basic concepts.pptx
PPTX
User Defined Functionscccccccccccccccccccccccccc.pptx
PDF
unit3 part2 pcds function notes.pdf
Presentation on Function in C Programming
Function in c
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
cp Module4(1)
Function C programming
Functionincprogram
Lecture 1_Functions in C.pptx
[ITP - Lecture 12] Functions in C/C++
Unit-III.pptx
Lecture_5_-_Functions_in_C_Detailed.pptx
unit_2.pptx
Functions and Header files ver very useful
USER DEFINED FUNCTIONS IN C.pdf
structured Programming Unit-7-Functions.pptx
Unit 3 (1)
unit_2 (1).pptx
c.p function
Programming in C FUNCTION Basic concepts.pptx
User Defined Functionscccccccccccccccccccccccccc.pptx
unit3 part2 pcds function notes.pdf
Ad

More from ranjan317165 (18)

PPT
universal human values L 14 Trust v4.ppt
PPT
Universal human values self and body chapter
PPT
L 13 universal human values Harmony in the Family v4.ppt
PPT
L 20 Mutual Fulfilment in Nature uhv lectures v5.ppt
PPTX
Module 4 Project management by ranjan v.pptx
PPTX
Software Requiremnet analysis module 2.pptx
PPTX
Introduction-to-Programming-Languages.pptx
PPT
Information system securit lecture 1y .ppt
PPTX
C functions with exercise to solve easily.pptx
PPT
L 27 Holistic Technologies v5 universal human values.ppt
PPT
06_PumpingLemma compiler design of chapter 4.ppt
PPT
CS540-2-lecture2 Lexical analyser of .ppt
PPT
atc 3rd module compiler and automata.ppt
PDF
role of lexical parser compiler design1-181124035217.pdf
PPT
15CS46 - Data communication or computer networks 1_Module-3.ppt
PPTX
compiler introduction vtu syllabus 1st chapter.pptx
PPT
Ppt on Design engineering which is chapter 9
PPTX
FiniteAutomata_anim.pptx
universal human values L 14 Trust v4.ppt
Universal human values self and body chapter
L 13 universal human values Harmony in the Family v4.ppt
L 20 Mutual Fulfilment in Nature uhv lectures v5.ppt
Module 4 Project management by ranjan v.pptx
Software Requiremnet analysis module 2.pptx
Introduction-to-Programming-Languages.pptx
Information system securit lecture 1y .ppt
C functions with exercise to solve easily.pptx
L 27 Holistic Technologies v5 universal human values.ppt
06_PumpingLemma compiler design of chapter 4.ppt
CS540-2-lecture2 Lexical analyser of .ppt
atc 3rd module compiler and automata.ppt
role of lexical parser compiler design1-181124035217.pdf
15CS46 - Data communication or computer networks 1_Module-3.ppt
compiler introduction vtu syllabus 1st chapter.pptx
Ppt on Design engineering which is chapter 9
FiniteAutomata_anim.pptx

Recently uploaded (20)

PDF
737-MAX_SRG.pdf student reference guides
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Artificial Intelligence
PPT
Total quality management ppt for engineering students
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
PPT on Performance Review to get promotions
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Current and future trends in Computer Vision.pptx
PPT
Project quality management in manufacturing
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Geodesy 1.pptx...............................................
PPTX
Sustainable Sites - Green Building Construction
PPTX
UNIT 4 Total Quality Management .pptx
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PDF
Well-logging-methods_new................
737-MAX_SRG.pdf student reference guides
additive manufacturing of ss316l using mig welding
Artificial Intelligence
Total quality management ppt for engineering students
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PPT on Performance Review to get promotions
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
CYBER-CRIMES AND SECURITY A guide to understanding
Categorization of Factors Affecting Classification Algorithms Selection
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Current and future trends in Computer Vision.pptx
Project quality management in manufacturing
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Geodesy 1.pptx...............................................
Sustainable Sites - Green Building Construction
UNIT 4 Total Quality Management .pptx
III.4.1.2_The_Space_Environment.p pdffdf
Well-logging-methods_new................

C functions by ranjan call by value and reference.pptx

  • 1. 1 Function in C Language
  • 2. 2 1.What is C Function? In short: A function is a group of statements that together perform a task. C functions are basic building blocks in a program. A large C program is divided into basic building blocks called C function. C function contains set of instructions enclosed by “{ }” which performs specific operation in a C program. Actually, Collection of these functions creates a C program
  • 3. 3 Classification Of Function Library function User define function - main() -printf() -scanf() -sqrt() -getchar() A large program in c can be divided to many subprogram The subprogram posses a self contain components and have well define purpose. The subprogram is called as a function Basically a job of function is to do something C program contain at least one function which is main(). 2. Types of C functions
  • 4. 4 Standard Library Functions: Library functions in C language are inbuilt functions which are grouped together and placed in a common place called library. Each library function in C performs specific operation. The standard library functions are built-in functions in C programming to handle tasks such as mathematical computations, I/O processing, string handling etc. These functions are defined in the header file. When you include the header file, these functions are available for use. For example: ➤ The printf() is a standard library function to send formatted output to the screen (display output on the screen). This function is defined in "stdio.h" header file. ➤ There are other numerous library functions defined under "stdio.h", such as scanf(), printf(), getchar() etc. Once you include "stdio.h" in your program, all these functions are available for use. User-defined Functions: As mentioned earlier, C allow programmers to define functions. Such functions created by the user are called user-defined functions. Depending upon the complexity and requirement of the program, you can create as many user-defined functions as you want.
  • 5. 5  C functions are used to avoid rewriting same logic/code again and again in a program.  There is no limit in calling C functions to make use of same functionality wherever required.  We can call functions any number of times in a program and from any place in a program.  A large C program can easily be tracked when it is divided into functions.  The core concept of C functions are, re-usability, dividing a big task into small pieces to achieve the functionality and to improve understandability of very large C programs. 3. Uses of C functions:
  • 6. 6 4. Advantages of function It is much easier to write a structured program where a large program can be divided into a smaller, simpler task. Allowing the code to be called many times Easier to read and update It is easier to debug a structured program where there error is easy to find and fix
  • 7. 7 C functions aspects syntax function definition Return_type function_name (arguments list) { Body of function; } function call function_name (arguments list); function declaration return_type function_name (argument list); There are 3 aspects in each C function. They are,  Function declaration or prototype – this informs compiler about the function name, function parameters and return value’s data type.  Function call - this calls the actual function  Function definition – this contains all the statements to be executed. 5. C function declaration, function call and function definition:
  • 8. 8  As you know, functions should be declared and defined before calling in a C program.  In the below program, function “square” is called from main function.  The value of “m” is passed as argument to the function “square”. This value is multiplied by itself in this function and multiplied value “p” is returned to main function from function “square”. 6. Simple Example Program for C Function: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #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 ) // function definition { float p ; p = x * x ; Enter some number for finding square 2 Square of the given number 2.000000 is 4.000000 Output: Enter some number for finding square 4 Square of the given number 2.000000 is 16.000000 https://www.programiz.com/online-compiler/ 4Cc8QE1mAphiO
  • 9. 9 7. How To Call C Functions In A Program? There are two ways that a C function can be called from a program. They are, 1.Call by value 2.Call by reference 1. Call By Value: •In call by value method, the value of the variable is passed to the function as parameter. •The value of the actual parameter can not be modified by formal parameter. •Different Memory is allocated for both actual and formal parameters. Because, value of actual parameter is copied to formal parameter.
  • 10. 10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include<stdio.h> // function prototype, also called function declaration void swap(int a, int b); int main() { int m = 22, n = 44; // calling swap function by value printf(" values before swap m = %d nand n = %d", m, n); swap(m, n); } void swap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; values before swap m = 22 and n = 44 values after swap m = 44 and n = 22  In this program, the values of the variables “m” and “n” are passed to the function “swap”.  These values are copied to formal parameters “a” and “b” in swap function and used. Example Program For C Function (Using Call By Value):
  • 11. 11 2. Call by reference: •In call by reference method, the address of the variable is passed to the function as parameter. •The value of the actual parameter can be modified by formal parameter. •Same memory is used for both actual and formal parameters since only address is used by both parameters 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include<stdio.h> // function prototype, also called function declaration void swap(int *a, int *b); int main() { int m = 22, n = 44; // calling swap function by reference printf("values before swap m = %d n and n = %d",m,n); swap(&m, &n); } void swap(int *a, int *b) { int tmp; tmp = *a; *a = *b; *b = tmp; printf("n values after swap a = %d nand b = %d", *a, *b); } Example Program For C Function (Using Call By Reference): •In this program, the address of the variables “m” and “n” are passed to the function “swap”. •These values are not copied to formal parameters “a” and “b” in swap function. •Because, they are just holding the address of those variables. •This address is used to access and change the values of the variables. values before swap m = 22 and n = 44 values after swap a = 44 and b = 22 Output: