Function in c

FUNCTION IN CMS. LOTHE SAVITAA.
DEPT OF COMPUTER SCIENCE
VASANTRAO NAIK MAHAVIDYALAYA, AURANGABAD..
TO STUDY…
 Introduction, types of functions.
 Defining functions, Arguments,
 Function prototype, actual parameters and formal
parameters,
 Calling function, Returning function results,
 Call by value, Recursion.
2FUNCTION IN C PROGRAMMING
INTRODUCTION
 C functions are basic building blocks in a program.
 All C programs are written using functions to
improve re-usability, understandability and to keep
track on them.
3FUNCTION IN C PROGRAMMING
WHAT IS FUNCTION?
 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.
4
FUNCTION IN C PROGRAMMING
USES OF C FUNCTIONS:
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. 5
FUNCTION IN C PROGRAMMING
FUNCTION
6FUNCTION IN C PROGRAMMING
PROGRAM SHOWING FUNCTION IN C
#include<stdio.h>
float square ( float x ); // function prototype, also called function declaration
int main( )
{
float m, n ;
printf ( "n Enter some number for finding square n");
scanf ( "%f", &m ) ;
n = square ( m ) ; // function call
printf ( "nSquare of the given number %f is %f",m,n );
}
float square ( float x ) // function definition
{
float p ;
p = x * x ;
return ( p ) ;
}
7FUNCTION IN C PROGRAMMING
HOW TO CALL C FUNCTIONS IN A
PROGRAM?
Call by value
Call by reference
THERE ARE
TWO WAYS
THAT A C
FUNCTION CAN
BE CALLED
FROM A
PROGRAM.
THEY ARE,
8FUNCTION IN C PROGRAMMING
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.
9FUNCTION IN C PROGRAMMING
Actual parameter – This is the argument which
is used in function call.
Formal parameter – This is the argument
which is used in function definition
10FUNCTION IN C PROGRAMMING
PROGRAM FOR CALL BY VALUE
#include<stdio.h>
void swap(int a, int b); // function prototype, also called function declaration
int main()
{
int m = 22, n = 44;
printf(" values before swap m = %d nand n = %d", m, n); // calling swap function by value
swap(m, n);
}
void swap(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
printf(" nvalues after swap m = %dn and n = %d", a, b);
}
11FUNCTION IN C PROGRAMMING
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.
12FUNCTION IN C PROGRAMMING
EXAMPLE SHOWING CALL BY REFERENCE
#include<stdio.h>
void swap(int *a, int *b); // function prototype, also called function declaration
int main()
{ int m = 22, n = 44;
printf("values before swap m = %d n and n = %d",m,n); // calling swap function by
reference
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);
}
13
FUNCTION IN C PROGRAMMING
RECURSION
 Recursion is the process of repeating items in a self-similar way. In
programming languages, if a program allows you to call a function inside the
same function, then it is called a recursive call of the function.
 Common examples of where recursion is used
 Walking recursive data structures such as linked lists, binary trees, etc.
 Exploring possible scenarios in games such as chess
Recursion always consists of two main parts. A terminating case that indicates
when the recursion will finish and a call to itself that must make progress
towards the terminating case. 14
FUNCTION IN C PROGRAMMING
SYNTAX
int main()
{
callme( );
...
return 0;
}
void rec( )
{
statement 1;
...
rec( );
}
15FUNCTION IN C PROGRAMMING
WORKING OF RECURSION
// A C++ program to demonstrate working of recursion
#include<stdio.h>
void printFun(int test)
{
if (test < 1)
return;
else
{
cout << test << " ";
printFun(test-1); // statement 2
cout << test << " ";
return;
}
}
int main()
{
int test = 3;
printFun(test);
}
16
FUNCTION IN C PROGRAMMING
REFERENCES
 https://www.tutorialspoint.com/cprogramming/c_recursion.
htm
 “Yashavant P. Kanetkar”, Let Us C - 14th Edition BPB
Publications ©2016, ISBN:8183331637 9788183331630
 https://fresh2refresh.com/
17FUNCTION IN C PROGRAMMING
THANK YOU
1 von 18

Recomendados

C functions von
C functionsC functions
C functionsUniversity of Potsdam
9.9K views32 Folien
Function in C program von
Function in C programFunction in C program
Function in C programNurul Zakiah Zamri Tan
70.4K views23 Folien
Function C programming von
Function C programmingFunction C programming
Function C programmingAppili Vamsi Krishna
11.6K views29 Folien
Function in c von
Function in cFunction in c
Function in cRaj Tandukar
6.8K views22 Folien
Function in c program von
Function in c programFunction in c program
Function in c programumesh patil
1.3K views23 Folien
Functions in c language von
Functions in c language Functions in c language
Functions in c language tanmaymodi4
2.6K views30 Folien

Más contenido relacionado

Was ist angesagt?

Functions in C von
Functions in CFunctions in C
Functions in CKamal Acharya
25.2K views22 Folien
Structure of C++ - R.D.Sivakumar von
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarSivakumar R D .
4.3K views27 Folien
Functions in c von
Functions in cFunctions in c
Functions in csunila tharagaturi
1.5K views42 Folien
Function von
FunctionFunction
Functionjayesh30sikchi
1.3K views22 Folien
Inline function von
Inline functionInline function
Inline functionTech_MX
14.2K views25 Folien
Functions in C von
Functions in CFunctions in C
Functions in CShobhit Upadhyay
4.1K views14 Folien

Was ist angesagt?(20)

Structure of C++ - R.D.Sivakumar von Sivakumar R D .
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
Sivakumar R D .4.3K views
Inline function von Tech_MX
Inline functionInline function
Inline function
Tech_MX14.2K views
Pointers,virtual functions and polymorphism cpp von rajshreemuthiah
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah2.4K views
Presentation on Function in C Programming von Shuvongkor Barman
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman14.4K views
RECURSION IN C von v_jk
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk14K views
Operators in c programming von savitamhaske
Operators in c programmingOperators in c programming
Operators in c programming
savitamhaske10.4K views
data types in C programming von Harshita Yadav
data types in C programmingdata types in C programming
data types in C programming
Harshita Yadav15.6K views

Similar a Function in c

Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx von
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxvekariyakashyap
13 views27 Folien
CH.4FUNCTIONS IN C_FYBSC(CS).pptx von
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxSangeetaBorde3
29 views31 Folien
C FUNCTIONS von
C FUNCTIONSC FUNCTIONS
C FUNCTIONSTeenaGeorge15
26 views18 Folien
Functionincprogram von
FunctionincprogramFunctionincprogram
FunctionincprogramSampath Kumar
44 views24 Folien
unit_2.pptx von
unit_2.pptxunit_2.pptx
unit_2.pptxVenkatesh Goud
8 views61 Folien
unit3 part2 pcds function notes.pdf von
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfJAVVAJI VENKATA RAO
7 views37 Folien

Similar a Function in c(20)

Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx von vekariyakashyap
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap13 views
CH.4FUNCTIONS IN C_FYBSC(CS).pptx von SangeetaBorde3
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde329 views
USER DEFINED FUNCTIONS IN C.pdf von BoomBoomers
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdf
BoomBoomers102 views
Pointers and call by value, reference, address in C von Syed Mustafa
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
Syed Mustafa4.9K views

Más de savitamhaske

Introduction html von
Introduction htmlIntroduction html
Introduction htmlsavitamhaske
329 views22 Folien
Specialized estimation tech von
Specialized estimation techSpecialized estimation tech
Specialized estimation techsavitamhaske
788 views21 Folien
8086 microprocessor von
8086 microprocessor8086 microprocessor
8086 microprocessorsavitamhaske
697 views19 Folien
Tables von
TablesTables
Tablessavitamhaske
521 views16 Folien
Introduction to toc and compiler von
Introduction to toc and compilerIntroduction to toc and compiler
Introduction to toc and compilersavitamhaske
869 views20 Folien
Decision control and iterative statements von
Decision control and iterative statementsDecision control and iterative statements
Decision control and iterative statementssavitamhaske
901 views15 Folien

Más de savitamhaske(7)

Specialized estimation tech von savitamhaske
Specialized estimation techSpecialized estimation tech
Specialized estimation tech
savitamhaske788 views
Introduction to toc and compiler von savitamhaske
Introduction to toc and compilerIntroduction to toc and compiler
Introduction to toc and compiler
savitamhaske869 views
Decision control and iterative statements von savitamhaske
Decision control and iterative statementsDecision control and iterative statements
Decision control and iterative statements
savitamhaske901 views
Programming in C- Introduction von savitamhaske
Programming in C- IntroductionProgramming in C- Introduction
Programming in C- Introduction
savitamhaske1.8K views

Último

11.28.23 Social Capital and Social Exclusion.pptx von
11.28.23 Social Capital and Social Exclusion.pptx11.28.23 Social Capital and Social Exclusion.pptx
11.28.23 Social Capital and Social Exclusion.pptxmary850239
281 views25 Folien
Narration ppt.pptx von
Narration  ppt.pptxNarration  ppt.pptx
Narration ppt.pptxTARIQ KHAN
119 views24 Folien
Narration lesson plan.docx von
Narration lesson plan.docxNarration lesson plan.docx
Narration lesson plan.docxTARIQ KHAN
104 views11 Folien
Material del tarjetero LEES Travesías.docx von
Material del tarjetero LEES Travesías.docxMaterial del tarjetero LEES Travesías.docx
Material del tarjetero LEES Travesías.docxNorberto Millán Muñoz
68 views9 Folien
Psychology KS4 von
Psychology KS4Psychology KS4
Psychology KS4WestHatch
68 views4 Folien
Plastic waste.pdf von
Plastic waste.pdfPlastic waste.pdf
Plastic waste.pdfalqaseedae
125 views5 Folien

Último(20)

11.28.23 Social Capital and Social Exclusion.pptx von mary850239
11.28.23 Social Capital and Social Exclusion.pptx11.28.23 Social Capital and Social Exclusion.pptx
11.28.23 Social Capital and Social Exclusion.pptx
mary850239281 views
Narration ppt.pptx von TARIQ KHAN
Narration  ppt.pptxNarration  ppt.pptx
Narration ppt.pptx
TARIQ KHAN119 views
Narration lesson plan.docx von TARIQ KHAN
Narration lesson plan.docxNarration lesson plan.docx
Narration lesson plan.docx
TARIQ KHAN104 views
Psychology KS4 von WestHatch
Psychology KS4Psychology KS4
Psychology KS4
WestHatch68 views
Plastic waste.pdf von alqaseedae
Plastic waste.pdfPlastic waste.pdf
Plastic waste.pdf
alqaseedae125 views
Are we onboard yet University of Sussex.pptx von Jisc
Are we onboard yet University of Sussex.pptxAre we onboard yet University of Sussex.pptx
Are we onboard yet University of Sussex.pptx
Jisc77 views
Lecture: Open Innovation von Michal Hron
Lecture: Open InnovationLecture: Open Innovation
Lecture: Open Innovation
Michal Hron96 views
UWP OA Week Presentation (1).pptx von Jisc
UWP OA Week Presentation (1).pptxUWP OA Week Presentation (1).pptx
UWP OA Week Presentation (1).pptx
Jisc74 views
Scope of Biochemistry.pptx von shoba shoba
Scope of Biochemistry.pptxScope of Biochemistry.pptx
Scope of Biochemistry.pptx
shoba shoba124 views
Community-led Open Access Publishing webinar.pptx von Jisc
Community-led Open Access Publishing webinar.pptxCommunity-led Open Access Publishing webinar.pptx
Community-led Open Access Publishing webinar.pptx
Jisc74 views
AI Tools for Business and Startups von Svetlin Nakov
AI Tools for Business and StartupsAI Tools for Business and Startups
AI Tools for Business and Startups
Svetlin Nakov101 views
Psychology KS5 von WestHatch
Psychology KS5Psychology KS5
Psychology KS5
WestHatch77 views

Function in c

  • 1. FUNCTION IN CMS. LOTHE SAVITAA. DEPT OF COMPUTER SCIENCE VASANTRAO NAIK MAHAVIDYALAYA, AURANGABAD..
  • 2. TO STUDY…  Introduction, types of functions.  Defining functions, Arguments,  Function prototype, actual parameters and formal parameters,  Calling function, Returning function results,  Call by value, Recursion. 2FUNCTION IN C PROGRAMMING
  • 3. INTRODUCTION  C functions are basic building blocks in a program.  All C programs are written using functions to improve re-usability, understandability and to keep track on them. 3FUNCTION IN C PROGRAMMING
  • 4. WHAT IS FUNCTION?  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. 4 FUNCTION IN C PROGRAMMING
  • 5. USES OF C FUNCTIONS: 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. 5 FUNCTION IN C PROGRAMMING
  • 7. PROGRAM SHOWING FUNCTION IN C #include<stdio.h> float square ( float x ); // function prototype, also called function declaration int main( ) { float m, n ; printf ( "n Enter some number for finding square n"); scanf ( "%f", &m ) ; n = square ( m ) ; // function call printf ( "nSquare of the given number %f is %f",m,n ); } float square ( float x ) // function definition { float p ; p = x * x ; return ( p ) ; } 7FUNCTION IN C PROGRAMMING
  • 8. HOW TO CALL C FUNCTIONS IN A PROGRAM? Call by value Call by reference THERE ARE TWO WAYS THAT A C FUNCTION CAN BE CALLED FROM A PROGRAM. THEY ARE, 8FUNCTION IN C PROGRAMMING
  • 9. 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. 9FUNCTION IN C PROGRAMMING
  • 10. Actual parameter – This is the argument which is used in function call. Formal parameter – This is the argument which is used in function definition 10FUNCTION IN C PROGRAMMING
  • 11. PROGRAM FOR CALL BY VALUE #include<stdio.h> void swap(int a, int b); // function prototype, also called function declaration int main() { int m = 22, n = 44; printf(" values before swap m = %d nand n = %d", m, n); // calling swap function by value swap(m, n); } void swap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; printf(" nvalues after swap m = %dn and n = %d", a, b); } 11FUNCTION IN C PROGRAMMING
  • 12. 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. 12FUNCTION IN C PROGRAMMING
  • 13. EXAMPLE SHOWING CALL BY REFERENCE #include<stdio.h> void swap(int *a, int *b); // function prototype, also called function declaration int main() { int m = 22, n = 44; printf("values before swap m = %d n and n = %d",m,n); // calling swap function by reference 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); } 13 FUNCTION IN C PROGRAMMING
  • 14. RECURSION  Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.  Common examples of where recursion is used  Walking recursive data structures such as linked lists, binary trees, etc.  Exploring possible scenarios in games such as chess Recursion always consists of two main parts. A terminating case that indicates when the recursion will finish and a call to itself that must make progress towards the terminating case. 14 FUNCTION IN C PROGRAMMING
  • 15. SYNTAX int main() { callme( ); ... return 0; } void rec( ) { statement 1; ... rec( ); } 15FUNCTION IN C PROGRAMMING
  • 16. WORKING OF RECURSION // A C++ program to demonstrate working of recursion #include<stdio.h> void printFun(int test) { if (test < 1) return; else { cout << test << " "; printFun(test-1); // statement 2 cout << test << " "; return; } } int main() { int test = 3; printFun(test); } 16 FUNCTION IN C PROGRAMMING
  • 17. REFERENCES  https://www.tutorialspoint.com/cprogramming/c_recursion. htm  “Yashavant P. Kanetkar”, Let Us C - 14th Edition BPB Publications ©2016, ISBN:8183331637 9788183331630  https://fresh2refresh.com/ 17FUNCTION IN C PROGRAMMING