SlideShare a Scribd company logo
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

More Related Content

What's hot (20)

Function in C
Function in CFunction in C
Function in C
Dr. Abhineet Anand
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
Praveen M Jigajinni
 
User defined functions
User defined functionsUser defined functions
User defined functions
Rokonuzzaman Rony
 
Functions in C - Programming
Functions in C - Programming Functions in C - Programming
Functions in C - Programming
GaurangVishnoi
 
Functions in c
Functions in cFunctions in c
Functions in c
sunila tharagaturi
 
C function presentation
C function presentationC function presentation
C function presentation
Touhidul Shawan
 
Functions in c
Functions in cFunctions in c
Functions in c
KavithaMuralidharan2
 
Function
FunctionFunction
Function
jayesh30sikchi
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
Function lecture
Function lectureFunction lecture
Function lecture
DIT University, Dehradun
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
Saket Pathak
 
Function in C Language
Function in C Language Function in C Language
Function in C Language
programmings guru
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
Appili Vamsi Krishna
 
functions in C and types
functions in C and typesfunctions in C and types
functions in C and types
mubashir farooq
 
Function in C program
Function in C programFunction in C program
Function in C program
Nurul Zakiah Zamri Tan
 
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)
VC Infotech
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
Harendra Singh
 
Function in c
Function in cFunction in c
Function in c
savitamhaske
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
Harsh Pathak
 

Similar to Lecture20 user definedfunctions.ppt (20)

functions in c language_functions in c language.pptx
functions in c language_functions in c language.pptxfunctions in c language_functions in c language.pptx
functions in c language_functions in c language.pptx
MehakBhatia38
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
Vikash Dhal
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
KhurramKhan173
 
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.pptLecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.ppt
eShikshak
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
Bharath904863
 
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
sumitbardhan
 
Functions
FunctionsFunctions
Functions
Mitali Chugh
 
Functions
FunctionsFunctions
Functions
Mitali Chugh
 
Functions and return type
Functions and return typeFunctions and return type
Functions and return type
baabtra.com - No. 1 supplier of quality freshers
 
functions-.pdf
functions-.pdffunctions-.pdf
functions-.pdf
MikialeTesfamariam
 
Unit 8
Unit 8Unit 8
Unit 8
rohassanie
 
Python functional programming
Python functional programmingPython functional programming
Python functional programming
Geison Goes
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
Ashwini Raut
 
Functions and structure in programming c
Functions and structure in programming cFunctions and structure in programming c
Functions and structure in programming c
dalalbhargavi19
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
miki304759
 
structured Programming Unit-7-Functions.pptx
structured Programming Unit-7-Functions.pptxstructured Programming Unit-7-Functions.pptx
structured Programming Unit-7-Functions.pptx
SuryaBasnet1
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
Venkatesh Goud
 
Functional programming in python
Functional programming in pythonFunctional programming in python
Functional programming in python
Edward D. Weinberger
 
Functional programming in python
Functional programming in pythonFunctional programming in python
Functional programming in python
Edward D. Weinberger
 
Modular Programming in C
Modular Programming in CModular Programming in C
Modular Programming in C
bhawna kol
 
functions in c language_functions in c language.pptx
functions in c language_functions in c language.pptxfunctions in c language_functions in c language.pptx
functions in c language_functions in c language.pptx
MehakBhatia38
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
Vikash Dhal
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
KhurramKhan173
 
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.pptLecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.ppt
eShikshak
 
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
sumitbardhan
 
Python functional programming
Python functional programmingPython functional programming
Python functional programming
Geison Goes
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
Ashwini Raut
 
Functions and structure in programming c
Functions and structure in programming cFunctions and structure in programming c
Functions and structure in programming c
dalalbhargavi19
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
miki304759
 
structured Programming Unit-7-Functions.pptx
structured Programming Unit-7-Functions.pptxstructured Programming Unit-7-Functions.pptx
structured Programming Unit-7-Functions.pptx
SuryaBasnet1
 
Modular Programming in C
Modular Programming in CModular Programming in C
Modular Programming in C
bhawna kol
 
Ad

More from eShikshak (20)

Modelling and evaluation
Modelling and evaluationModelling and evaluation
Modelling and evaluation
eShikshak
 
Operators in python
Operators in pythonOperators in python
Operators in python
eShikshak
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
eShikshak
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
eShikshak
 
Introduction to e commerce
Introduction to e commerceIntroduction to e commerce
Introduction to e commerce
eShikshak
 
Chapeter 2 introduction to cloud computing
Chapeter 2   introduction to cloud computingChapeter 2   introduction to cloud computing
Chapeter 2 introduction to cloud computing
eShikshak
 
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
eShikshak
 
Unit 1.3 types of cloud
Unit 1.3 types of cloudUnit 1.3 types of cloud
Unit 1.3 types of cloud
eShikshak
 
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
eShikshak
 
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
eShikshak
 
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 executions
eShikshak
 
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__else
eShikshak
 
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
eShikshak
 
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 variables
eShikshak
 
Lecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operatorsLecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operators
eShikshak
 
Lecture18 structurein c.ppt
Lecture18 structurein c.pptLecture18 structurein c.ppt
Lecture18 structurein c.ppt
eShikshak
 
Modelling and evaluation
Modelling and evaluationModelling and evaluation
Modelling and evaluation
eShikshak
 
Operators in python
Operators in pythonOperators in python
Operators in python
eShikshak
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
eShikshak
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
eShikshak
 
Introduction to e commerce
Introduction to e commerceIntroduction to e commerce
Introduction to e commerce
eShikshak
 
Chapeter 2 introduction to cloud computing
Chapeter 2   introduction to cloud computingChapeter 2   introduction to cloud computing
Chapeter 2 introduction to cloud computing
eShikshak
 
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
eShikshak
 
Unit 1.3 types of cloud
Unit 1.3 types of cloudUnit 1.3 types of cloud
Unit 1.3 types of cloud
eShikshak
 
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
eShikshak
 
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
eShikshak
 
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 executions
eShikshak
 
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__else
eShikshak
 
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
eShikshak
 
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 variables
eShikshak
 
Lecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operatorsLecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operators
eShikshak
 
Lecture18 structurein c.ppt
Lecture18 structurein c.pptLecture18 structurein c.ppt
Lecture18 structurein c.ppt
eShikshak
 
Ad

Recently uploaded (20)

“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
Edge AI and Vision Alliance
 
Dancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptxDancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptx
Elliott Richmond
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptxISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
AyilurRamnath1
 
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
DevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical PodcastDevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical Podcast
Chris Wahl
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Soulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate reviewSoulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate review
Soulmaite
 
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyesEnd-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
ThousandEyes
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf
Minuscule Technologies
 
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
Edge AI and Vision Alliance
 
Dancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptxDancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptx
Elliott Richmond
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptxISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
AyilurRamnath1
 
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
DevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical PodcastDevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical Podcast
Chris Wahl
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Soulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate reviewSoulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate review
Soulmaite
 
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyesEnd-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
ThousandEyes
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf
Minuscule Technologies
 

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