SlideShare ist ein Scribd-Unternehmen logo
1 von 25
Downloaden Sie, um offline zu lesen
USER DEFINED FUNCTIONS
          IN ‘C’

Prakash Khaire   Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
Introduction
Strength of ‘C’ language is C functions
●


They are easy to define and use
●


We are very much familiar with main(), printf() &
●


scanf() function



Prakash Khaire,
                  B V Patel Inst. of BMCBMCGopal Vidyanagar
                  B V Patel Inst. of & IT, & IT, Gopal Vidyanagar
Lecturer
In this chapter, you will see…
●   How a function is designed ?
●   How a function is integrated into a program ?
●   How two or more functions are put together /
●   How they communicate with one another ?



Prakash Khaire,
Prakash Khaire,
                  B V Patel Inst. of BMCBMCGopal Vidyanagar
                  B V Patel Inst. of & IT, & IT, Gopal Vidyanagar
Lecturer
Lecturer
Definition - function
●A set of statements working together with
common goal is known as function.
●Also known as subprograms which are used to
compute a value or perform a specific task.
●They can’t run independently and are always
called by the main() program or by some other
function.
Prakash Khaire,
Prakash Khaire,
                  B V Patel Inst. of BMCBMCGopal Vidyanagar
                  B V Patel Inst. of & IT, & IT, Gopal Vidyanagar
Lecturer
Lecturer
Categories of fucntions
●In ‘C’ language, functions are classified into the
following two categories
●Library functions
●User-Defined functions

●scanf(), printf(), getch(), strlen(), strcmp(),
strcat(), sqrt(), pow() are this are library
functions.
●main() is user-defined functions
Prakash Khaire,
Prakash Khaire,
                  B V Patel Inst. of BMCBMCGopal Vidyanagar
                  B V Patel Inst. of & IT, & IT, Gopal Vidyanagar
Lecturer
Lecturer
User Defined Functions
●User defined functions are self-contained blocks
of statements which are written by the user to
compute or perform a task.
●They can be called by the main program
repeatedly as per the requirement.


Prakash Khaire,
Prakash Khaire,
                  B V Patel Inst. of BMCBMCGopal Vidyanagar
                  B V Patel Inst. of & IT, & IT, Gopal Vidyanagar
Lecturer
Lecturer
Uses of functions
They are very much useful when a block of
●

statements has to be written/executed again and
again.
They are useful when program size are too large
●

and complex.
It works like top-down modular programming technique to
●

solve a problem.
They are also used to reduce the difficulties during
●

debugging a program.
Prakash Khaire,
Prakash Khaire,
                  B V Patel Inst. of BMCBMCGopal Vidyanagar
                  B V Patel Inst. of & IT, & IT, Gopal Vidyanagar
Lecturer
Lecturer
Uses of functions
●The length of a source program can be reduced
by using functions at appropriate places.
●It is easy to locate and isolate a faulty function
for further investigations.
●A function can be used by many other
programs. Thus C programmer can build their
own library.
Prakash Khaire,
Prakash Khaire,
                  B V Patel Inst. of BMCBMCGopal Vidyanagar
                  B V Patel Inst. of & IT, & IT, Gopal Vidyanagar
Lecturer
Lecturer
Top-down modular programming using
                   functions
                                   Main
                                   program


                  Function A       Function B        Function C




                               B                 B
                               1                 1

Prakash Khaire,
                         B V Patel Inst. of BMC & IT, Gopal Vidyanagar
Lecturer
A MULTI-FUNCTION PROGRAM
void main()
{
printline();
printf(“I love my parents !!!”);
printline();
}
void printline()
{
       int I;
       for(i=0;i<40;i++)
       printf(“-”);
printf(“n”);
}




Prakash Khaire,
Prakash Khaire,
                             B V Patel Inst. of BMCBMCGopal Vidyanagar
                             B V Patel Inst. of & IT, & IT, Gopal Vidyanagar
Lecturer
Lecturer
ELEMENTS OF USER DEFINED
FUNCTION
Some similarities between functions and
●

variables
●Both functions names and variables are considered
as identifiers and therefore they must follow the rules
for identifiers
●Like variables, functions have type associated with

them
●Like variables, function names and their types must

be declared and defined before they are used in
program
Prakash Khaire,
Prakash Khaire,
Lecturer
                B V Patel Inst. of BMCBMCGopal Vidyanagar
                B V Patel Inst. of & IT, & IT, Gopal Vidyanagar
Lecturer
ELEMENTS OF USER-DEFINED
FUNCTION
●In order to make use of user-defined functions, we need to establish
three elements that are related to functions.
●Function definition
●Function Call

●Function declaration




Prakash Khaire,
                   B V Patel Inst. of BMC & IT, Gopal Vidyanagar
Lecturer
FUNCTION DEFINITION
●The function definition is an independent program
module that is specially written or implement the
requirements of the function.
●To use this block or function, we need to invoke it at
the required place in the program, known as the
functions
●The program that calls the function is referred to as
the calling program or calling functions
Prakash Khaire,
Prakash Khaire,
                  B V Patel Inst. of BMCBMCGopal Vidyanagar
                  B V Patel Inst. of & IT, & IT, Gopal Vidyanagar
Lecturer
Lecturer
main()
                           {
                              …..
                              function1();
                              ….
                              ….
                              function2();
                           }
                           function1()
                           {
                              …
                              …
                           }
                           function2()
                           {
                              …
                             function1();
                           }


Prakash Khaire,
                  B V Patel Inst. of BMC & IT, Gopal Vidyanagar
Lecturer
FLOW OF FUNCTION
     ● When the program is executed (that is, run) execution always begins at
   the first statement in the function main no matter where it is placed in the
   program.
     ● Other functions are executed only when they are called.
     ● Function prototypes appear before any function definition, so the
   compiler translates these first. The compiler can then correctly translate a
   function call.
     ● A function call statement results in the transfer of control to the first
   statement in the body of the called function.
     ● After the last statement of the called function is executed, the control is
   passed back to the point immediately following the function call.
     ● A value-returning function returns a value. Therefore, for value-returning
   functions, after executing the function when the control goes back to the
   caller, the value that the function returns replaces the function call
   statement.
Prakash Khaire,
Prakash Khaire,
                       B V Patel Inst. of BMCBMCGopal Vidyanagar
                       B V Patel Inst. of & IT, & IT, Gopal Vidyanagar
Lecturer
Lecturer
FUNCTION DEFINITION
A function definition includes the following
●


elements
●Function name
●Function type

●List of parameters

●Local variable declarations

●A return statement




Prakash Khaire,
Prakash Khaire,
                  B V Patel Inst. of BMCBMCGopal Vidyanagar
                  B V Patel Inst. of & IT, & IT, Gopal Vidyanagar
Lecturer
Lecturer
A general format of function
definition
                                                  Function header
datatype functionName(parameter list)
                                                    1. function type
{                                                   2. function name
    local variable declarations;                    3. formal parameter list
           executable statement1;
           executable statement2;   function body
           …
           …
          return statement                      returns the value - optional
 }

Prakash Khaire,
                    B V Patel Inst. of BMC & IT, Gopal Vidyanagar
Lecturer
A general format of function
definition
●If no return data type is specified than by default
‘C’ will assume that it is an integer type.
●If the function is not going to return any value then
we have to specify the return type as void.
●Function name follows the rules of identifier.



Prakash Khaire,
                  B V Patel Inst. of BMC & IT, Gopal Vidyanagar
Lecturer
A general format of function
definition
●Parameter list are list of variables that will
receive the data sent by the calling
program.
●int sum(int a, int b) { ………………… }
●float mul(float x, float y) { ………………. }




Prakash Khaire,
                  B V Patel Inst. of BMC & IT, Gopal Vidyanagar
Lecturer
Calling a function
●A function is called by the calling program using the
function name with the required number of arguments in
parenthesis.
●The function call comes in assignment statement or in an
output statement.
●printf(“%d”,sum(a,b));
●ans = sum(a,b);

A function is called using its name with required number of
●


arguments in paranthesis.
Prakash Khaire,
Prakash Khaire,
                  B V Patel Inst. of BMCBMCGopal Vidyanagar
                  B V Patel Inst. of & IT, & IT, Gopal Vidyanagar
Lecturer
Lecturer
Formal and Actual Arguments
int sum(int, int); //declaration                   //function definition
void main()                                        int sum(int x, int y)
{
      int a=5, b=6,ans;                            {                              formal arguments
                                                           int val;
    ans =sum(a , b);//calling function arguments
                                actual                 val = x +y;
                                                       return val;
    printf(“Answer : %d”,ans);                     }
}




The argument listed in the function calling        The arguments used in the function declaration are
statement are referred to as actual arguments      referred as formal arguments
                                                           Prakash Khaire,
B V Patel Inst. of BMC B VIT, Gopalof BMC & IT, Gopal Vidyanagar
Prakash Khaire, Lecturer & Patel Inst. Vidyanagar
                                                           Lecturer
Formal and Actual Arguments
●The argument listed in the                  ●The agrument used in the
function calling statement                   function declaration are
are referred to as actual                    referred as formal
arguments.                                   arguments.
●They are the actual values                  ●They are simply formal
passed to a function to                      variables that accepts or
compute a value or to                        receive the values supplied
perform a task.                              by the calling program.

                                                             Prakash Khaire,
B VPrakash Khaire, of BMC V PatelGopal BMC & IT, Gopal Vidyanagar
      Patel Inst.       B & IT, Inst. of Vidyanagar
    ●

Lecturer
                                                             Lecturer
Rules of call a function
●Function is called by the main() or any other function
●When the return type of the function is omitted, then by default the
return type will be int.
●A function can return a value any stage of its execution by using more
than one return statements.
●The return statement is omitted if it does not return a value directly to
the calling program.
●Arguments listed can be omitted, but not the paranthesis () following
the function.


Prakash Khaire,
Prakash Khaire,
                    B V Patel Inst. of BMCBMCGopal Vidyanagar
                    B V Patel Inst. of & IT, & IT, Gopal Vidyanagar
Lecturer
Lecturer
Function Declaration or prototype
To match the number and data type of the actual and formal
●


arguments in the calling and called function respectively.
To check this compiler will verify the function declaration or prototype.
●


●data_type function_name(data_type var1, data_type var2,…..,data_type
varn);
●Example

●   int sum(int, int);
The prototype declaration is always written above the main() function
●


definition.



Prakash Khaire,
Prakash Khaire,
                         B V Patel Inst. of BMCBMCGopal Vidyanagar
                         B V Patel Inst. of & IT, & IT, Gopal Vidyanagar
Lecturer
Lecturer
Return values and their types
●We can pass n numbers of values to the called function, but the called
function can only return one value per call.
●The return statement can take one of the following form
●return;

●return(expression);

●The plain return does not return any value
●return statement with expression returns the value of the expression
●There can be more than one return statement if there is use of
conditional statement.


Prakash Khaire,
Prakash Khaire,
                   B V Patel Inst. of BMCBMCGopal Vidyanagar
                   B V Patel Inst. of & IT, & IT, Gopal Vidyanagar
Lecturer
Lecturer

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Function in C
Function in CFunction in C
Function in C
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Functions in C - Programming
Functions in C - Programming Functions in C - Programming
Functions in C - Programming
 
Functions in c
Functions in cFunctions in c
Functions in c
 
C function presentation
C function presentationC function presentation
C function presentation
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Function
FunctionFunction
Function
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Function lecture
Function lectureFunction lecture
Function lecture
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
Function in C Language
Function in C Language Function in C Language
Function in C Language
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
 
functions in C and types
functions in C and typesfunctions in C and types
functions in C and types
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Function in c language(defination and declaration)
Function in c language(defination and declaration)Function in c language(defination and declaration)
Function in c language(defination and declaration)
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Function in c
Function in cFunction in c
Function in c
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 

Ähnlich wie Lecture20 user definedfunctions.ppt

Ähnlich wie Lecture20 user definedfunctions.ppt (20)

Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.pptLecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.ppt
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
 
Functions
FunctionsFunctions
Functions
 
Functions
FunctionsFunctions
Functions
 
Functions and return type
Functions and return typeFunctions and return type
Functions and return type
 
functions-.pdf
functions-.pdffunctions-.pdf
functions-.pdf
 
Unit 8
Unit 8Unit 8
Unit 8
 
Python functional programming
Python functional programmingPython functional programming
Python functional programming
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
Functional programming in python
Functional programming in pythonFunctional programming in python
Functional programming in python
 
Functional programming in python
Functional programming in pythonFunctional programming in python
Functional programming in python
 
Modular Programming in C
Modular Programming in CModular Programming in C
Modular Programming in C
 
U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).ppt
 
Function
Function Function
Function
 
C++ unit-1-part-10
C++ unit-1-part-10C++ unit-1-part-10
C++ unit-1-part-10
 

Mehr von eShikshak

Modelling and evaluation
Modelling and evaluationModelling and evaluation
Modelling and evaluationeShikshak
 
Operators in python
Operators in pythonOperators in python
Operators in pythoneShikshak
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in pythoneShikshak
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythoneShikshak
 
Introduction to e commerce
Introduction to e commerceIntroduction to e commerce
Introduction to e commerceeShikshak
 
Chapeter 2 introduction to cloud computing
Chapeter 2   introduction to cloud computingChapeter 2   introduction to cloud computing
Chapeter 2 introduction to cloud computingeShikshak
 
Unit 1.4 working of cloud computing
Unit 1.4 working of cloud computingUnit 1.4 working of cloud computing
Unit 1.4 working of cloud computingeShikshak
 
Unit 1.3 types of cloud
Unit 1.3 types of cloudUnit 1.3 types of cloud
Unit 1.3 types of cloudeShikshak
 
Unit 1.2 move to cloud computing
Unit 1.2   move to cloud computingUnit 1.2   move to cloud computing
Unit 1.2 move to cloud computingeShikshak
 
Unit 1.1 introduction to cloud computing
Unit 1.1   introduction to cloud computingUnit 1.1   introduction to cloud computing
Unit 1.1 introduction to cloud computingeShikshak
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'eShikshak
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'eShikshak
 
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7   iteration and repetitive executionsMesics lecture 7   iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executionseShikshak
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’eShikshak
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__elseeShikshak
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssionseShikshak
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’eShikshak
 
Mesics lecture 3 c – constants and variables
Mesics lecture 3   c – constants and variablesMesics lecture 3   c – constants and variables
Mesics lecture 3 c – constants and variableseShikshak
 
Lecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operatorsLecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operatorseShikshak
 
Lecture18 structurein c.ppt
Lecture18 structurein c.pptLecture18 structurein c.ppt
Lecture18 structurein c.ppteShikshak
 

Mehr von eShikshak (20)

Modelling and evaluation
Modelling and evaluationModelling and evaluation
Modelling and evaluation
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Introduction to e commerce
Introduction to e commerceIntroduction to e commerce
Introduction to e commerce
 
Chapeter 2 introduction to cloud computing
Chapeter 2   introduction to cloud computingChapeter 2   introduction to cloud computing
Chapeter 2 introduction to cloud computing
 
Unit 1.4 working of cloud computing
Unit 1.4 working of cloud computingUnit 1.4 working of cloud computing
Unit 1.4 working of cloud computing
 
Unit 1.3 types of cloud
Unit 1.3 types of cloudUnit 1.3 types of cloud
Unit 1.3 types of cloud
 
Unit 1.2 move to cloud computing
Unit 1.2   move to cloud computingUnit 1.2   move to cloud computing
Unit 1.2 move to cloud computing
 
Unit 1.1 introduction to cloud computing
Unit 1.1   introduction to cloud computingUnit 1.1   introduction to cloud computing
Unit 1.1 introduction to cloud computing
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7   iteration and repetitive executionsMesics lecture 7   iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executions
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssions
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Mesics lecture 3 c – constants and variables
Mesics lecture 3   c – constants and variablesMesics lecture 3   c – constants and variables
Mesics lecture 3 c – constants and variables
 
Lecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operatorsLecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operators
 
Lecture18 structurein c.ppt
Lecture18 structurein c.pptLecture18 structurein c.ppt
Lecture18 structurein c.ppt
 

Kürzlich hochgeladen

Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxUdaiappa Ramachandran
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDELiveplex
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Commit University
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Brian Pichman
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8DianaGray10
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsSeth Reyes
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding TeamAdam Moalla
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Will Schroeder
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Websitedgelyza
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesMd Hossain Ali
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?IES VE
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-pyJamie (Taka) Wang
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024SkyPlanner
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAshyamraj55
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintMahmoud Rabie
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 

Kürzlich hochgeladen (20)

Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptx
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and Hazards
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Website
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?
 
201610817 - edge part1
201610817 - edge part1201610817 - edge part1
201610817 - edge part1
 
20230104 - machine vision
20230104 - machine vision20230104 - machine vision
20230104 - machine vision
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-py
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership Blueprint
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 

Lecture20 user definedfunctions.ppt

  • 1. USER DEFINED FUNCTIONS IN ‘C’ Prakash Khaire Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar
  • 2. Introduction Strength of ‘C’ language is C functions ● They are easy to define and use ● We are very much familiar with main(), printf() & ● scanf() function Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal Vidyanagar Lecturer
  • 3. In this chapter, you will see… ● How a function is designed ? ● How a function is integrated into a program ? ● How two or more functions are put together / ● How they communicate with one another ? Prakash Khaire, Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal Vidyanagar Lecturer Lecturer
  • 4. Definition - function ●A set of statements working together with common goal is known as function. ●Also known as subprograms which are used to compute a value or perform a specific task. ●They can’t run independently and are always called by the main() program or by some other function. Prakash Khaire, Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal Vidyanagar Lecturer Lecturer
  • 5. Categories of fucntions ●In ‘C’ language, functions are classified into the following two categories ●Library functions ●User-Defined functions ●scanf(), printf(), getch(), strlen(), strcmp(), strcat(), sqrt(), pow() are this are library functions. ●main() is user-defined functions Prakash Khaire, Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal Vidyanagar Lecturer Lecturer
  • 6. User Defined Functions ●User defined functions are self-contained blocks of statements which are written by the user to compute or perform a task. ●They can be called by the main program repeatedly as per the requirement. Prakash Khaire, Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal Vidyanagar Lecturer Lecturer
  • 7. Uses of functions They are very much useful when a block of ● statements has to be written/executed again and again. They are useful when program size are too large ● and complex. It works like top-down modular programming technique to ● solve a problem. They are also used to reduce the difficulties during ● debugging a program. Prakash Khaire, Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal Vidyanagar Lecturer Lecturer
  • 8. Uses of functions ●The length of a source program can be reduced by using functions at appropriate places. ●It is easy to locate and isolate a faulty function for further investigations. ●A function can be used by many other programs. Thus C programmer can build their own library. Prakash Khaire, Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal Vidyanagar Lecturer Lecturer
  • 9. Top-down modular programming using functions Main program Function A Function B Function C B B 1 1 Prakash Khaire, B V Patel Inst. of BMC & IT, Gopal Vidyanagar Lecturer
  • 10. A MULTI-FUNCTION PROGRAM void main() { printline(); printf(“I love my parents !!!”); printline(); } void printline() { int I; for(i=0;i<40;i++) printf(“-”); printf(“n”); } Prakash Khaire, Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal Vidyanagar Lecturer Lecturer
  • 11. ELEMENTS OF USER DEFINED FUNCTION Some similarities between functions and ● variables ●Both functions names and variables are considered as identifiers and therefore they must follow the rules for identifiers ●Like variables, functions have type associated with them ●Like variables, function names and their types must be declared and defined before they are used in program Prakash Khaire, Prakash Khaire, Lecturer B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal Vidyanagar Lecturer
  • 12. ELEMENTS OF USER-DEFINED FUNCTION ●In order to make use of user-defined functions, we need to establish three elements that are related to functions. ●Function definition ●Function Call ●Function declaration Prakash Khaire, B V Patel Inst. of BMC & IT, Gopal Vidyanagar Lecturer
  • 13. FUNCTION DEFINITION ●The function definition is an independent program module that is specially written or implement the requirements of the function. ●To use this block or function, we need to invoke it at the required place in the program, known as the functions ●The program that calls the function is referred to as the calling program or calling functions Prakash Khaire, Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal Vidyanagar Lecturer Lecturer
  • 14. main() { ….. function1(); …. …. function2(); } function1() { … … } function2() { … function1(); } Prakash Khaire, B V Patel Inst. of BMC & IT, Gopal Vidyanagar Lecturer
  • 15. FLOW OF FUNCTION ● When the program is executed (that is, run) execution always begins at the first statement in the function main no matter where it is placed in the program. ● Other functions are executed only when they are called. ● Function prototypes appear before any function definition, so the compiler translates these first. The compiler can then correctly translate a function call. ● A function call statement results in the transfer of control to the first statement in the body of the called function. ● After the last statement of the called function is executed, the control is passed back to the point immediately following the function call. ● A value-returning function returns a value. Therefore, for value-returning functions, after executing the function when the control goes back to the caller, the value that the function returns replaces the function call statement. Prakash Khaire, Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal Vidyanagar Lecturer Lecturer
  • 16. FUNCTION DEFINITION A function definition includes the following ● elements ●Function name ●Function type ●List of parameters ●Local variable declarations ●A return statement Prakash Khaire, Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal Vidyanagar Lecturer Lecturer
  • 17. A general format of function definition Function header datatype functionName(parameter list) 1. function type { 2. function name local variable declarations; 3. formal parameter list executable statement1; executable statement2; function body … … return statement returns the value - optional } Prakash Khaire, B V Patel Inst. of BMC & IT, Gopal Vidyanagar Lecturer
  • 18. A general format of function definition ●If no return data type is specified than by default ‘C’ will assume that it is an integer type. ●If the function is not going to return any value then we have to specify the return type as void. ●Function name follows the rules of identifier. Prakash Khaire, B V Patel Inst. of BMC & IT, Gopal Vidyanagar Lecturer
  • 19. A general format of function definition ●Parameter list are list of variables that will receive the data sent by the calling program. ●int sum(int a, int b) { ………………… } ●float mul(float x, float y) { ………………. } Prakash Khaire, B V Patel Inst. of BMC & IT, Gopal Vidyanagar Lecturer
  • 20. Calling a function ●A function is called by the calling program using the function name with the required number of arguments in parenthesis. ●The function call comes in assignment statement or in an output statement. ●printf(“%d”,sum(a,b)); ●ans = sum(a,b); A function is called using its name with required number of ● arguments in paranthesis. Prakash Khaire, Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal Vidyanagar Lecturer Lecturer
  • 21. Formal and Actual Arguments int sum(int, int); //declaration //function definition void main() int sum(int x, int y) { int a=5, b=6,ans; { formal arguments int val; ans =sum(a , b);//calling function arguments actual val = x +y; return val; printf(“Answer : %d”,ans); } } The argument listed in the function calling The arguments used in the function declaration are statement are referred to as actual arguments referred as formal arguments Prakash Khaire, B V Patel Inst. of BMC B VIT, Gopalof BMC & IT, Gopal Vidyanagar Prakash Khaire, Lecturer & Patel Inst. Vidyanagar Lecturer
  • 22. Formal and Actual Arguments ●The argument listed in the ●The agrument used in the function calling statement function declaration are are referred to as actual referred as formal arguments. arguments. ●They are the actual values ●They are simply formal passed to a function to variables that accepts or compute a value or to receive the values supplied perform a task. by the calling program. Prakash Khaire, B VPrakash Khaire, of BMC V PatelGopal BMC & IT, Gopal Vidyanagar Patel Inst. B & IT, Inst. of Vidyanagar ● Lecturer Lecturer
  • 23. Rules of call a function ●Function is called by the main() or any other function ●When the return type of the function is omitted, then by default the return type will be int. ●A function can return a value any stage of its execution by using more than one return statements. ●The return statement is omitted if it does not return a value directly to the calling program. ●Arguments listed can be omitted, but not the paranthesis () following the function. Prakash Khaire, Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal Vidyanagar Lecturer Lecturer
  • 24. Function Declaration or prototype To match the number and data type of the actual and formal ● arguments in the calling and called function respectively. To check this compiler will verify the function declaration or prototype. ● ●data_type function_name(data_type var1, data_type var2,…..,data_type varn); ●Example ● int sum(int, int); The prototype declaration is always written above the main() function ● definition. Prakash Khaire, Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal Vidyanagar Lecturer Lecturer
  • 25. Return values and their types ●We can pass n numbers of values to the called function, but the called function can only return one value per call. ●The return statement can take one of the following form ●return; ●return(expression); ●The plain return does not return any value ●return statement with expression returns the value of the expression ●There can be more than one return statement if there is use of conditional statement. Prakash Khaire, Prakash Khaire, B V Patel Inst. of BMCBMCGopal Vidyanagar B V Patel Inst. of & IT, & IT, Gopal Vidyanagar Lecturer Lecturer