SlideShare a Scribd company logo
1 of 41
INTRO DUCTION TO FUNCTIONS
TYPES OF FUNCTIONS
ELEM ENTS O F USER DEFINED FUNCTIO NS
TYPES O N THE BA SIS O F A RG UM ENTS A ND
RETURN VA LUES
METHODS OF CA LLING A FUNCTION
Functions
Introduction to
Function
Block of statements that perform the particular task.
Enables modular programming.
Main() is the driver function. Has pre defined
prototype.
Same function can be accessed from different places
within a program.
Once a function execution is completed , control
return to the place from where the function was
called.
Advantag
es
Modular Programming
Length of source program can be reduced
Easy to locate and isolate faulty function
Functions can be used by other program’s
Types of
Functions
Library (Built In) Functions:
 They are written in the header files.
 To use them appropriate header files should be included.
Header Files Functions Defined
stdio.h Printf(), scanf(), getchar(), putchar(),
gets(), puts(), fopen(), fclose()
conio.h Clrscr(), getch()
Ctype.h Toupper(), tolower(), isalpha()
Math.h Pow(), sqrt(), cos(), log()
Stdlib.h Rand(), exit()
String.h Strlen(), strcpy(), strupr()
User Defined Functions
 Written by the user at the time of programming.
Elements of User defined
functions
Function Prototype Function Call
Function arguments and parameters
Function Definitions
Function
prototype
 It specify the type of value that is to be return
from the function and that is to be passed to the
function.
 It is defined in the beginning before the function
call is made.
 Syntax:
 return-type name-of-function(list of arguments);
 Example
 Void sum(int, int);
Function
Call
A function can be called by specifying name and list
of arguments enclosed in parenthesis and separated
by comma.
If there is no arguments empty parenthesis are place
after function name.
If function return a value, function call is written as
assignment statement as:
A=sum(x,y);
Function arguments and
parameters
Arguments are also called actual parameters.
Arguments are written within parenthesis at the time
of function call.
Parameters are also called formal parameters.
These are written within parenthesis at the time of
function definition.
Function
Definition
It is the independent program module.
It is written to specify the particular task that is to be
performed by the function.
The first line of the function is called function
declarator and rest line inside { } is called function
body
Return
statement
It is the last statement of the function that return
certain values.
It return certain types of values to the place from
where the function was invoked.
Syntax:
return(variable-name or constant);
 #include <stdio.h>
 int addition(int num1, int num2)
 {
 int sum;
 /* Arguments are used here*/
 sum = num1+num2;

 /* Function return type is integer so we are returning
 * an integer value, the sum of the passed numbers.
 */
 return sum;
 }

 int main()
 {
 int var1, var2;
 printf("Enter number 1: ");
 scanf("%d",&var1);
 printf("Enter number 2: ");
 scanf("%d",&var2);

 /* Calling the function here, the function return type
 * is integer so we need an integer variable to hold the
 * returned value of this function.
 */
 int res = addition(var1, var2);
 printf ("Output: %d", res);

 return 0;
Categories of
function
Function with no arguments and no return
Function with arguments but no return
Function with no arguments and return
Function with arguments and return
Function with no argument and no
return
#include<stdio.h>
int sum(); // function prototype
int main()
{
sum(); //function call
return 0;
}
int sum() //function defination
{
int x,y;
printf("enter two numbers:");
scanf("%d%d", &x,&y);
printf("sum is%d :", x+y);
}
Function with argument and no
return
#include<stdio.h>
int sum(int a, int b); // function prototype
int main()
{
int x,y;
printf("enter two numbers:");
scanf("%d%d", &x,&y);
sum(x,y); //function call
return 0;
}
int sum(int a, int b) //function defination
{
printf("sum is%d :", a+b);
}
Function with no argument and
return
#include<stdio.h>
int sum(); // function prototype
int main()
{
int a;
a=sum();
printf("the sum of the numbers you entered is
%d",a);
return 0;
}
int sum() //function defination
{
int x,y;
printf("enter two numbers:");
scanf("%d%d", &x,&y);
return(x+y);
}
Function with argument and
return
#include<stdio.h>
int sum(int,int); // function prototype
int main()
{
int a,x,y;
printf("enter two numbers:");
scanf("%d%d", &x,&y);
a=sum(x,y);
printf("the sum of the numbers you entered is %d",a);
return 0;
}
int sum(int x, int y) //function defination
{
return(x+y);
}
Methods of calling function
Call by value
Call by reference
Call by value
 Copies the value of actual parameters into formal
parameters.
 During execution whatever changes are made in
the formal parameters are not reflected back in
the actual parameters.
Call by Reference
Reference(address) of the original variable is passed.
Function does not create its own copy, it refers to the
original values by reference.
Functions works with the original data and changes
are made in the original data.
#include<stdio.h>
int swap(int*,int*); // function prototype
int main()
{
int x,y;
printf("enter two numbers:");
scanf("%d%d", &x,&y);
printf("x=%d and y=%d before calling functionn",x,y);
swap(&x,&y);
printf("x=%d and y=%d after calling function",x,y);
return 0;
}
int swap(int* x, int* y) //function defination
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
Scope of variables
• Block scope
• Function scope
• Program scope
• File scope
Block scope
 A local scope or block is collective program statements put in and
declared within a function or block (a specific region enclosed with
curly braces) and variables lying inside such blocks are termed as
local variables.
 #include <stdio.h>
 int main ()
 {
 /* local variable definition and initialization */ int x,y,z;
 /* actual initialization */ x = 20;
 y = 30;
 z = x + y;
 printf ("value of x = %d, y = %d and z = %dn", x, y, z);
 return 0;
 }

Function Scope:
 A Function scope begins at the opening of the
function and ends with the closing of it. Function
scope is applicable to labels only. A label declared
is used as a target to go to the statement and both
goto and label statement must be in the same
function.
Program scope:
 After defining a local variable, the system or the compiler won't be
initializing any value to it. You have to initialize it by yourself. It is
considered good programming practice to initialize variables before
using. Whereas in contrast, global variables get initialized
automatically by the compiler as and when defined. Here's how
based on datatype; global variables are defined.
 Life time
 Place of declaration
 Name conflict

// C program to illustrate the global scope
#include <stdio.h>
// Global variable
int global = 5;
// global variable accessed from within a function
void display()
{
printf("%dn", global);
}
// main function
int main()
{
printf("Before change within main: ");
display();
// changing value of global variable from main function
printf("After change within main: ");
global = 10;
display();
}
File Scope:
 These variables are usually declared outside of all of
the functions and blocks, at the top of the program and
can be accessed from any portion of the program.
 When a global variable is accessible until the end of the
file, the variable is said to have file scope. To allow a
variable to have file scope, declare that variable with the
static keyword before specifying its data type.
 Static int x =10;

Variable storage classes
 automatic
 register
 static
 external
Storage class datatype vname:
Auto Storage Class
 auto comes by default with all local variables as its
storage class. The keyword auto is used to define
this storage class explicitly
 Syntax: auto datatype vname;
 int roll; // contains auto by default
 is the same as:
 auto int roll;
register Storage Class
 This storage class is implemented for classifying local
variables whose value needs to be saved in a CPU
register in place of RAM (Random Access Memory). This
is implemented when you want your variable the
maximum size equivalent to the size of the register. It
uses the keyword register.
 Syntax: register datatype vname;
 register int counter;
 Register variables are used when implementing looping
in counter variables to make program execution fast.
Register variables work faster than variables stored in
RAM (primary memory).
 &
static storage class
 This storage class uses static variables that are used
popularly for writing programs in C language. Static
variables preserve the value of a variable even when
the scope limit exceeds. Static storage class has its
scope local to the function in which it is defined.
 On the other hand, global static variables can be accessed
in any part of your program. The default value assigned is
'0' by the C compiler. The keyword used to define this
storage class is static.
 Example:
 static int var = 6
extern Storage class
 The extern storage class is used to feature a variable to be used from
within different blocks of the same program. Mainly, a value is set to
that variable which is in a different block or function and can be
overwritten or altered from within another block as well.
 #include <stdio.h>
 int val;
 extern void funcExtern();
 main()
 {
 val = 10;
 funcExtern();
 }
Recursion
 Recursion can be defined as the technique of
replicating or doing again an activity in a self-similar
way calling itself again and again, and the process
continues till specific condition reaches.
 Every recursive solution has two major cases
• Base case
• Recursive case
Recursion and its types
 Direct recursion
 Indirect recursion
 Tail recursion
 Linear and tree recursion
 Recursive functions can be classified on the
basis of :
a.) If the functions call itself directly or indirectly –
Direct / Indirect
b.) If an operation is pending at each recursive
call – Tail Recursive/ Not
c.) based on the structure of the function calling
pattern – Linear / Tree
 Direct Recursion:
 If a function explicitly calls itself it is called directly
recursive.
 When the method invokes itself it is direct.
int testfunc(int num)
{ if (num == 0)
return 0;
else
return (testfunc(num - 1));}
 Here, the function ‘testfunc’ calls itself for all positive
values of num.
Indirect Recursion
 This occurs when the function invokes other method
which again causes the original function to be called
again.
 If a method ‘X’ , calls method ‘Y’, which calls method
‘Z’ which again leads to ‘X’ being invoked is called
indirect recursive or mutually recursive as well.
int testfunc1(int num)
{ if (num == 0)
return 0;
else
return (testfunc2(num - 1));
}
int testfunc2(int num2)
{ return testfunc1(num2 - 1);}
Tail / Bottom Recursion
 A function is said to be tail-recursive, if no
operations are pending when the recursive
function returns to its caller.
 Such functions, immediately return the return
value from the calling function.
 It is an efficient method as compared to others, as
the stack space required is less and even
compute overhead will get reduced.
 Recollect the previously discussed example,
factorial of a number. We had written it in non tail
recursive way, as after call operation is still
pending.
Linear and Tree Recursion
 Depending on the structure the recursive function
calls take, or grows it can be either linear or non
linear.
It is linearly recursive when, the pending operations
do not involve another recursive call to the function.
Our Factorial recursive function is linearly recursive
as it only involves multiplying the returned values
and no further calls to function.
Tree recursion is when, pending operations involve
another recursive call to function.
For example – Fibonacci series, the pending
operations have recursive call to the fib() recursive
function to compute the results.

More Related Content

What's hot (19)

Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
Function in c
Function in cFunction in c
Function in c
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
 
Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppt
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
C functions
C functionsC functions
C functions
 
Function
FunctionFunction
Function
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
C functions list
C functions listC functions list
C functions list
 
Function in C
Function in CFunction in C
Function in C
 
Storage classess of C progamming
Storage classess of C progamming Storage classess of C progamming
Storage classess of C progamming
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Unit 8
Unit 8Unit 8
Unit 8
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
C function
C functionC function
C function
 
Function in c
Function in cFunction in c
Function in c
 
User defined functions
User defined functionsUser defined functions
User defined functions
 

Similar to Functions in c

CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxSangeetaBorde3
 
What is storage class
What is storage classWhat is storage class
What is storage classIsha Aggarwal
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 FunctionDeepak Singh
 
Storage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageStorage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageJenish Bhavsar
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1YOGESH SINGH
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)Mansi Tyagi
 
function of C.pptx
function of C.pptxfunction of C.pptx
function of C.pptxshivas379526
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and FunctionsJake Bond
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
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
 
D-38 vedant ICCPL.pptx
D-38 vedant ICCPL.pptxD-38 vedant ICCPL.pptx
D-38 vedant ICCPL.pptxVedantSahane
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfBoomBoomers
 

Similar to Functions in c (20)

CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
What is storage class
What is storage classWhat is storage class
What is storage class
 
Unit iii
Unit iiiUnit iii
Unit iii
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
 
4. function
4. function4. function
4. function
 
Storage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageStorage classes arrays & functions in C Language
Storage classes arrays & functions in C Language
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 
5.program structure
5.program structure5.program structure
5.program structure
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 
function of C.pptx
function of C.pptxfunction of C.pptx
function of C.pptx
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
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
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 
D-38 vedant ICCPL.pptx
D-38 vedant ICCPL.pptxD-38 vedant ICCPL.pptx
D-38 vedant ICCPL.pptx
 
Functions
Functions Functions
Functions
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdf
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 

Recently uploaded

What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 

Recently uploaded (20)

What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 

Functions in c

  • 1. INTRO DUCTION TO FUNCTIONS TYPES OF FUNCTIONS ELEM ENTS O F USER DEFINED FUNCTIO NS TYPES O N THE BA SIS O F A RG UM ENTS A ND RETURN VA LUES METHODS OF CA LLING A FUNCTION Functions
  • 2. Introduction to Function Block of statements that perform the particular task. Enables modular programming. Main() is the driver function. Has pre defined prototype. Same function can be accessed from different places within a program. Once a function execution is completed , control return to the place from where the function was called.
  • 3. Advantag es Modular Programming Length of source program can be reduced Easy to locate and isolate faulty function Functions can be used by other program’s
  • 4. Types of Functions Library (Built In) Functions:  They are written in the header files.  To use them appropriate header files should be included. Header Files Functions Defined stdio.h Printf(), scanf(), getchar(), putchar(), gets(), puts(), fopen(), fclose() conio.h Clrscr(), getch() Ctype.h Toupper(), tolower(), isalpha() Math.h Pow(), sqrt(), cos(), log() Stdlib.h Rand(), exit() String.h Strlen(), strcpy(), strupr()
  • 5. User Defined Functions  Written by the user at the time of programming.
  • 6. Elements of User defined functions Function Prototype Function Call Function arguments and parameters Function Definitions
  • 7. Function prototype  It specify the type of value that is to be return from the function and that is to be passed to the function.  It is defined in the beginning before the function call is made.  Syntax:  return-type name-of-function(list of arguments);  Example  Void sum(int, int);
  • 8. Function Call A function can be called by specifying name and list of arguments enclosed in parenthesis and separated by comma. If there is no arguments empty parenthesis are place after function name. If function return a value, function call is written as assignment statement as: A=sum(x,y);
  • 9. Function arguments and parameters Arguments are also called actual parameters. Arguments are written within parenthesis at the time of function call. Parameters are also called formal parameters. These are written within parenthesis at the time of function definition.
  • 10. Function Definition It is the independent program module. It is written to specify the particular task that is to be performed by the function. The first line of the function is called function declarator and rest line inside { } is called function body
  • 11.
  • 12. Return statement It is the last statement of the function that return certain values. It return certain types of values to the place from where the function was invoked. Syntax: return(variable-name or constant);
  • 13.  #include <stdio.h>  int addition(int num1, int num2)  {  int sum;  /* Arguments are used here*/  sum = num1+num2;   /* Function return type is integer so we are returning  * an integer value, the sum of the passed numbers.  */  return sum;  }   int main()  {  int var1, var2;  printf("Enter number 1: ");  scanf("%d",&var1);  printf("Enter number 2: ");  scanf("%d",&var2);   /* Calling the function here, the function return type  * is integer so we need an integer variable to hold the  * returned value of this function.  */  int res = addition(var1, var2);  printf ("Output: %d", res);   return 0;
  • 14. Categories of function Function with no arguments and no return Function with arguments but no return Function with no arguments and return Function with arguments and return
  • 15. Function with no argument and no return #include<stdio.h> int sum(); // function prototype int main() { sum(); //function call return 0; } int sum() //function defination { int x,y; printf("enter two numbers:"); scanf("%d%d", &x,&y); printf("sum is%d :", x+y); }
  • 16. Function with argument and no return #include<stdio.h> int sum(int a, int b); // function prototype int main() { int x,y; printf("enter two numbers:"); scanf("%d%d", &x,&y); sum(x,y); //function call return 0; } int sum(int a, int b) //function defination { printf("sum is%d :", a+b); }
  • 17. Function with no argument and return #include<stdio.h> int sum(); // function prototype int main() { int a; a=sum(); printf("the sum of the numbers you entered is %d",a); return 0; } int sum() //function defination { int x,y; printf("enter two numbers:"); scanf("%d%d", &x,&y); return(x+y); }
  • 18. Function with argument and return #include<stdio.h> int sum(int,int); // function prototype int main() { int a,x,y; printf("enter two numbers:"); scanf("%d%d", &x,&y); a=sum(x,y); printf("the sum of the numbers you entered is %d",a); return 0; } int sum(int x, int y) //function defination { return(x+y); }
  • 19. Methods of calling function Call by value Call by reference
  • 20. Call by value  Copies the value of actual parameters into formal parameters.  During execution whatever changes are made in the formal parameters are not reflected back in the actual parameters.
  • 21.
  • 22. Call by Reference Reference(address) of the original variable is passed. Function does not create its own copy, it refers to the original values by reference. Functions works with the original data and changes are made in the original data.
  • 23. #include<stdio.h> int swap(int*,int*); // function prototype int main() { int x,y; printf("enter two numbers:"); scanf("%d%d", &x,&y); printf("x=%d and y=%d before calling functionn",x,y); swap(&x,&y); printf("x=%d and y=%d after calling function",x,y); return 0; } int swap(int* x, int* y) //function defination { int temp; temp=*x; *x=*y; *y=temp; }
  • 24. Scope of variables • Block scope • Function scope • Program scope • File scope
  • 25. Block scope  A local scope or block is collective program statements put in and declared within a function or block (a specific region enclosed with curly braces) and variables lying inside such blocks are termed as local variables.  #include <stdio.h>  int main ()  {  /* local variable definition and initialization */ int x,y,z;  /* actual initialization */ x = 20;  y = 30;  z = x + y;  printf ("value of x = %d, y = %d and z = %dn", x, y, z);  return 0;  } 
  • 26. Function Scope:  A Function scope begins at the opening of the function and ends with the closing of it. Function scope is applicable to labels only. A label declared is used as a target to go to the statement and both goto and label statement must be in the same function.
  • 27. Program scope:  After defining a local variable, the system or the compiler won't be initializing any value to it. You have to initialize it by yourself. It is considered good programming practice to initialize variables before using. Whereas in contrast, global variables get initialized automatically by the compiler as and when defined. Here's how based on datatype; global variables are defined.  Life time  Place of declaration  Name conflict 
  • 28. // C program to illustrate the global scope #include <stdio.h> // Global variable int global = 5; // global variable accessed from within a function void display() { printf("%dn", global); } // main function int main() { printf("Before change within main: "); display(); // changing value of global variable from main function printf("After change within main: "); global = 10; display(); }
  • 29. File Scope:  These variables are usually declared outside of all of the functions and blocks, at the top of the program and can be accessed from any portion of the program.  When a global variable is accessible until the end of the file, the variable is said to have file scope. To allow a variable to have file scope, declare that variable with the static keyword before specifying its data type.  Static int x =10; 
  • 30. Variable storage classes  automatic  register  static  external Storage class datatype vname:
  • 31. Auto Storage Class  auto comes by default with all local variables as its storage class. The keyword auto is used to define this storage class explicitly  Syntax: auto datatype vname;  int roll; // contains auto by default  is the same as:  auto int roll;
  • 32. register Storage Class  This storage class is implemented for classifying local variables whose value needs to be saved in a CPU register in place of RAM (Random Access Memory). This is implemented when you want your variable the maximum size equivalent to the size of the register. It uses the keyword register.  Syntax: register datatype vname;  register int counter;  Register variables are used when implementing looping in counter variables to make program execution fast. Register variables work faster than variables stored in RAM (primary memory).  &
  • 33. static storage class  This storage class uses static variables that are used popularly for writing programs in C language. Static variables preserve the value of a variable even when the scope limit exceeds. Static storage class has its scope local to the function in which it is defined.  On the other hand, global static variables can be accessed in any part of your program. The default value assigned is '0' by the C compiler. The keyword used to define this storage class is static.  Example:  static int var = 6
  • 34. extern Storage class  The extern storage class is used to feature a variable to be used from within different blocks of the same program. Mainly, a value is set to that variable which is in a different block or function and can be overwritten or altered from within another block as well.  #include <stdio.h>  int val;  extern void funcExtern();  main()  {  val = 10;  funcExtern();  }
  • 35. Recursion  Recursion can be defined as the technique of replicating or doing again an activity in a self-similar way calling itself again and again, and the process continues till specific condition reaches.  Every recursive solution has two major cases • Base case • Recursive case
  • 36. Recursion and its types  Direct recursion  Indirect recursion  Tail recursion  Linear and tree recursion
  • 37.  Recursive functions can be classified on the basis of : a.) If the functions call itself directly or indirectly – Direct / Indirect b.) If an operation is pending at each recursive call – Tail Recursive/ Not c.) based on the structure of the function calling pattern – Linear / Tree
  • 38.  Direct Recursion:  If a function explicitly calls itself it is called directly recursive.  When the method invokes itself it is direct. int testfunc(int num) { if (num == 0) return 0; else return (testfunc(num - 1));}  Here, the function ‘testfunc’ calls itself for all positive values of num.
  • 39. Indirect Recursion  This occurs when the function invokes other method which again causes the original function to be called again.  If a method ‘X’ , calls method ‘Y’, which calls method ‘Z’ which again leads to ‘X’ being invoked is called indirect recursive or mutually recursive as well. int testfunc1(int num) { if (num == 0) return 0; else return (testfunc2(num - 1)); } int testfunc2(int num2) { return testfunc1(num2 - 1);}
  • 40. Tail / Bottom Recursion  A function is said to be tail-recursive, if no operations are pending when the recursive function returns to its caller.  Such functions, immediately return the return value from the calling function.  It is an efficient method as compared to others, as the stack space required is less and even compute overhead will get reduced.  Recollect the previously discussed example, factorial of a number. We had written it in non tail recursive way, as after call operation is still pending.
  • 41. Linear and Tree Recursion  Depending on the structure the recursive function calls take, or grows it can be either linear or non linear. It is linearly recursive when, the pending operations do not involve another recursive call to the function. Our Factorial recursive function is linearly recursive as it only involves multiplying the returned values and no further calls to function. Tree recursion is when, pending operations involve another recursive call to function. For example – Fibonacci series, the pending operations have recursive call to the fib() recursive function to compute the results.