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

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 functionsare 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 CFUNCTIONS: 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
  • 6.
  • 7.
    PROGRAM SHOWING FUNCTIONIN 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 CALLC 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 Incall 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 CALLBY 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 Incall 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 CALLBY 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 isthe 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( ); ... return0; } 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  “YashavantP. Kanetkar”, Let Us C - 14th Edition BPB Publications ©2016, ISBN:8183331637 9788183331630  https://fresh2refresh.com/ 17FUNCTION IN C PROGRAMMING
  • 18.