SlideShare a Scribd company logo
1 of 35
Functions in C language
Mehak Bhatia
(Assistant Professor)
IIMT College of Science and Technology
Greater Noida
Functions
• A function is a group of statements that together
perform a task.
• Functions are sets of statements that take inputs,
perform some operations, and produce results
 Functions are used to perform certain actions, and
they are important for reusing code:
 Define the code once, and use it many times.
Mehak Bhatia, IIMT College of Science
and Technology
Functions
 Within a function, there are a number of
programming statements enclosed by {}, having
certain meanings and performing certain
operations.
 The operation of a function occurs only when it
is called.
 Rather than writing the same code for different
inputs repeatedly, we can call the function
instead of writing the same code over and over
again.
Mehak Bhatia, IIMT College of Science
and Technology
Creating a Function
Syntax:
void function_name()
{
// code to be executed
}
Example:
void myFunction()
{
printf("Hello World!");
}
Mehak Bhatia, IIMT College of Science
and Technology
Explanation of Example
 myFunction() is the name of the function.
 Inside the function (the body), we have to add
the code that defines what the function should
do.
 void means that the function does not have a
return value.
Mehak Bhatia, IIMT College of Science
and Technology
Why use Functions?
 The function can reduce the repetition of the
same statements in the program.
 The function makes code readable.
 There is no fixed number of calling functions it
can be called as many times as you want.
 The function reduces the size of the program.
 Once the function is declared you can just use it
without thinking about the internal working of
the function.
Mehak Bhatia, IIMT College of Science
and Technology
Syntax of Functions
Mehak Bhatia, IIMT College of Science
and Technology
Defining a Function
The general form of a function definition in
programming language is as follows :
return_type function_name( parameter list )
{
body of the function
}
A function definition in C programming consists of
a function header and a function body.
Mehak Bhatia, IIMT College of Science
and Technology
Here are all the parts of a function −
Mehak Bhatia, IIMT College of Science
and Technology
 Return Type − A function may return a value.
The return_type is the data type of the value
the function returns. Some functions perform
the desired operations without returning a value.
In this case, the return_type is the
keyword void.
 Function Name − This is the actual name of
the function. The function name and the
parameter list together constitute the function
signature.
 Parameters − A parameter is like a placeholder.
When a function is invoked, you pass a value to the
parameter.
 This value is referred to as actual parameter or
argument.
 The parameter list refers to the type, order, and
number of the parameters of a function.
 NOTE: Parameters are optional; that is, a function
may contain no parameters.
 Function Body − The function body contains a
collection of statements that define what the
function does.
Mehak Bhatia, IIMT College of Science
and Technology
Mehak Bhatia, IIMT College of Science
and Technology
Given below is the source code for a function called max().This
function takes two parameters num1 and num2 and returns the
maximum value between the two −
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
} Mehak Bhatia, IIMT College of Science
and Technology
Function Declarations
 A function declaration tells the compiler about a
function name and how to call the function. The
actual body of the function can be defined
separately.
 A function declaration has the following parts −
return_type function_name( parameter list );
Mehak Bhatia, IIMT College of Science
and Technology
For the above defined function max(), the function
declaration is as follows −
int max(int num1, int num2);
NOTE: Parameter names are not important in function
declaration only their type is required, so the following is
also a valid declaration −
int max(int, int);
Mehak Bhatia, IIMT College of Science
and Technology
 Function declaration is required when you define
a function in one source file and you call that
function in another file.
 In such case, you should declare the function at
the top of the file calling the function
Mehak Bhatia, IIMT College of Science
and Technology
Calling a Function
• While creating a C function, you give a definition of
what the function has to do. To use a function, you will
have to call that function to perform the defined task.
• When a program calls a function, the program control is
transferred to the called function.
• A called function performs a defined task and when its
return statement is executed or when its function-ending
closing brace is reached, it returns the program control
back to the main program.
Mehak Bhatia, IIMT College of Science
and Technology
To call a function, you simply need to pass the required parameters along
with the function name, and if the function returns a value, then you can
store the returned value. For example −
Mehak Bhatia, IIMT College of Science
and Technology
Example of function calling
#include <stdio.h>
int sum(int a, int b) // Function takes 2 parameters as inputs
{
return a + b; // Returns the sum
}
int main()
{
// Calling sum function and storing its value in add variable
int add = sum(10, 30);
printf("Sum is: %d", add);
return 0;
}
Mehak Bhatia, IIMT College of Science
and Technology
Output
Sum is: 40
Mehak Bhatia, IIMT College of Science
and Technology
How C function works?
•Declaring a function: Declaring a function is a
step where we declare a function. Here we define
the return types and parameters of the function.
•Calling the function: Calling the function is a
step where we call the function by passing the
arguments in the function.
.
 Executing the function: Executing the function
is a step where we can run all the statements
inside the function to get the final result.
 Returning a value: Returning a value is the
step where the calculated value after the
execution of the function is returned.
 Exiting the function: Exiting the function is the
final step where all the allocated memory to the
variables, functions, etc is destroyed before
giving full control to the main function.
Mehak Bhatia, IIMT College of Science
and Technology
Types of Functions
Mehak Bhatia, IIMT College of Science
and Technology
User Defined Function
Functions that the programmer creates are
known as User-Defined functions
or “tailor-made functions”. User-
defined functions can be improved and
modified according to the need of the
programmer. Whenever we write a
function that is case-specific and is not
defined in any header file, we need to
declare and define our own functions
according to the syntax.Mehak Bhatia, IIMT College of Science
and Technology
Advantages of User-Defined functions
 Changeable functions can be modified
as per need.
 The Code of these functions is reusable
in other programs.
 These functions are easy to understand,
debug and maintain.
Mehak Bhatia, IIMT College of Science
and Technology
#include <stdio.h>
int sum(int a, int b)
{
return a + b;
}
// Driver code
int main()
{
int a = 30, b = 40;
// function call
int res = sum(a, b);
printf("Sum is: %d", res);
return 0;
}
Mehak Bhatia, IIMT College of Science
and Technology
OutputSum is: 70
Mehak Bhatia, IIMT College of Science
and Technology
Java Method Overloading
If we have to perform only one operation, having
same name of the methods increases the
readability of the program.
There are two ways to overload the method in
java:
 By changing number of arguments
 By changing the data type
Mehak Bhatia, IIMT College of Science
and Technology
By changing number of arguments
class Adder{
static int add(int a,int b)
{return a+b;
}
static int add(int a,int b,int c)
{
return a+b+c;
} }
class TestOverloading1{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
Mehak Bhatia, IIMT College of Science
and Technology
Output
22
33
Mehak Bhatia, IIMT College of Science
and Technology
Library Function
A library function is also referred to as a “built-in
function”. A compiler package already exists that
contains these functions, each of which has a
specific meaning and is included in the package.
Built-in functions have the advantage of being
directly usable without being defined, whereas
user-defined functions must be declared and
defined before being used.
Mehak Bhatia, IIMT College of Science
and Technology
 For Example:
 pow(), sqrt(), strcmp(), strcpy() etc.
Mehak Bhatia, IIMT College of Science
and Technology
 Advantages of C library functions
 C Library functions are easy to use and
optimized for better performance.
 C library functions save a lot of time i.e,
function development time.
 C library functions are convenient as they
always work.
Mehak Bhatia, IIMT College of Science
and Technology
Advantage of methods:
 #include <math.h>
 #include <stdio.h>

 // Driver code
 int main()
 {
 double Number;
 Number = 49;

 // Computing the square root with
 // the help of predefined C
 // library function
 double squareRoot = sqrt(Number);

 printf("The Square root of %.2lf = %.2lf",
 Number, squareRoot);
 return 0;
 }
Mehak Bhatia, IIMT College of Science
and Technology
 OutputThe Square root of 49.00 =
7.00
Mehak Bhatia, IIMT College of Science
and Technology
THANK YOU
Mehak Bhatia, IIMT College of Science
and Technology

More Related Content

Similar to functions in c language_functions in c language.pptx

Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directivesVikash Dhal
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignmentAhmad Kamal
 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programmingnmahi96
 
Functions part1
Functions part1Functions part1
Functions part1yndaravind
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2yndaravind
 
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
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfTeshaleSiyum
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdfTeshaleSiyum
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfBoomBoomers
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxKhurramKhan173
 
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
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functionsAlisha Korpal
 

Similar to functions in c language_functions in c language.pptx (20)

Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignment
 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programming
 
Functions
Functions Functions
Functions
 
Functions part1
Functions part1Functions part1
Functions part1
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2
 
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
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
C FUNCTIONS
C FUNCTIONSC FUNCTIONS
C FUNCTIONS
 
Functions
FunctionsFunctions
Functions
 
Functions
FunctionsFunctions
Functions
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdf
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
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)
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
 
Functions
FunctionsFunctions
Functions
 
Functions
FunctionsFunctions
Functions
 

Recently uploaded

CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 

Recently uploaded (20)

E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 

functions in c language_functions in c language.pptx

  • 1. Functions in C language Mehak Bhatia (Assistant Professor) IIMT College of Science and Technology Greater Noida
  • 2. Functions • A function is a group of statements that together perform a task. • Functions are sets of statements that take inputs, perform some operations, and produce results  Functions are used to perform certain actions, and they are important for reusing code:  Define the code once, and use it many times. Mehak Bhatia, IIMT College of Science and Technology
  • 3. Functions  Within a function, there are a number of programming statements enclosed by {}, having certain meanings and performing certain operations.  The operation of a function occurs only when it is called.  Rather than writing the same code for different inputs repeatedly, we can call the function instead of writing the same code over and over again. Mehak Bhatia, IIMT College of Science and Technology
  • 4. Creating a Function Syntax: void function_name() { // code to be executed } Example: void myFunction() { printf("Hello World!"); } Mehak Bhatia, IIMT College of Science and Technology
  • 5. Explanation of Example  myFunction() is the name of the function.  Inside the function (the body), we have to add the code that defines what the function should do.  void means that the function does not have a return value. Mehak Bhatia, IIMT College of Science and Technology
  • 6. Why use Functions?  The function can reduce the repetition of the same statements in the program.  The function makes code readable.  There is no fixed number of calling functions it can be called as many times as you want.  The function reduces the size of the program.  Once the function is declared you can just use it without thinking about the internal working of the function. Mehak Bhatia, IIMT College of Science and Technology
  • 7. Syntax of Functions Mehak Bhatia, IIMT College of Science and Technology
  • 8. Defining a Function The general form of a function definition in programming language is as follows : return_type function_name( parameter list ) { body of the function } A function definition in C programming consists of a function header and a function body. Mehak Bhatia, IIMT College of Science and Technology
  • 9. Here are all the parts of a function − Mehak Bhatia, IIMT College of Science and Technology  Return Type − A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.  Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature.
  • 10.  Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter.  This value is referred to as actual parameter or argument.  The parameter list refers to the type, order, and number of the parameters of a function.  NOTE: Parameters are optional; that is, a function may contain no parameters.  Function Body − The function body contains a collection of statements that define what the function does. Mehak Bhatia, IIMT College of Science and Technology
  • 11. Mehak Bhatia, IIMT College of Science and Technology
  • 12. Given below is the source code for a function called max().This function takes two parameters num1 and num2 and returns the maximum value between the two − int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } Mehak Bhatia, IIMT College of Science and Technology
  • 13. Function Declarations  A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately.  A function declaration has the following parts − return_type function_name( parameter list ); Mehak Bhatia, IIMT College of Science and Technology
  • 14. For the above defined function max(), the function declaration is as follows − int max(int num1, int num2); NOTE: Parameter names are not important in function declaration only their type is required, so the following is also a valid declaration − int max(int, int); Mehak Bhatia, IIMT College of Science and Technology
  • 15.  Function declaration is required when you define a function in one source file and you call that function in another file.  In such case, you should declare the function at the top of the file calling the function Mehak Bhatia, IIMT College of Science and Technology
  • 16. Calling a Function • While creating a C function, you give a definition of what the function has to do. To use a function, you will have to call that function to perform the defined task. • When a program calls a function, the program control is transferred to the called function. • A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program. Mehak Bhatia, IIMT College of Science and Technology
  • 17. To call a function, you simply need to pass the required parameters along with the function name, and if the function returns a value, then you can store the returned value. For example − Mehak Bhatia, IIMT College of Science and Technology
  • 18. Example of function calling #include <stdio.h> int sum(int a, int b) // Function takes 2 parameters as inputs { return a + b; // Returns the sum } int main() { // Calling sum function and storing its value in add variable int add = sum(10, 30); printf("Sum is: %d", add); return 0; } Mehak Bhatia, IIMT College of Science and Technology
  • 19. Output Sum is: 40 Mehak Bhatia, IIMT College of Science and Technology
  • 20. How C function works? •Declaring a function: Declaring a function is a step where we declare a function. Here we define the return types and parameters of the function. •Calling the function: Calling the function is a step where we call the function by passing the arguments in the function. .
  • 21.  Executing the function: Executing the function is a step where we can run all the statements inside the function to get the final result.  Returning a value: Returning a value is the step where the calculated value after the execution of the function is returned.  Exiting the function: Exiting the function is the final step where all the allocated memory to the variables, functions, etc is destroyed before giving full control to the main function. Mehak Bhatia, IIMT College of Science and Technology
  • 22. Types of Functions Mehak Bhatia, IIMT College of Science and Technology
  • 23. User Defined Function Functions that the programmer creates are known as User-Defined functions or “tailor-made functions”. User- defined functions can be improved and modified according to the need of the programmer. Whenever we write a function that is case-specific and is not defined in any header file, we need to declare and define our own functions according to the syntax.Mehak Bhatia, IIMT College of Science and Technology
  • 24. Advantages of User-Defined functions  Changeable functions can be modified as per need.  The Code of these functions is reusable in other programs.  These functions are easy to understand, debug and maintain. Mehak Bhatia, IIMT College of Science and Technology
  • 25. #include <stdio.h> int sum(int a, int b) { return a + b; } // Driver code int main() { int a = 30, b = 40; // function call int res = sum(a, b); printf("Sum is: %d", res); return 0; } Mehak Bhatia, IIMT College of Science and Technology
  • 26. OutputSum is: 70 Mehak Bhatia, IIMT College of Science and Technology
  • 27. Java Method Overloading If we have to perform only one operation, having same name of the methods increases the readability of the program. There are two ways to overload the method in java:  By changing number of arguments  By changing the data type Mehak Bhatia, IIMT College of Science and Technology
  • 28. By changing number of arguments class Adder{ static int add(int a,int b) {return a+b; } static int add(int a,int b,int c) { return a+b+c; } } class TestOverloading1{ public static void main(String[] args){ System.out.println(Adder.add(11,11)); System.out.println(Adder.add(11,11,11)); }} Mehak Bhatia, IIMT College of Science and Technology
  • 29. Output 22 33 Mehak Bhatia, IIMT College of Science and Technology
  • 30. Library Function A library function is also referred to as a “built-in function”. A compiler package already exists that contains these functions, each of which has a specific meaning and is included in the package. Built-in functions have the advantage of being directly usable without being defined, whereas user-defined functions must be declared and defined before being used. Mehak Bhatia, IIMT College of Science and Technology
  • 31.  For Example:  pow(), sqrt(), strcmp(), strcpy() etc. Mehak Bhatia, IIMT College of Science and Technology
  • 32.  Advantages of C library functions  C Library functions are easy to use and optimized for better performance.  C library functions save a lot of time i.e, function development time.  C library functions are convenient as they always work. Mehak Bhatia, IIMT College of Science and Technology
  • 33. Advantage of methods:  #include <math.h>  #include <stdio.h>   // Driver code  int main()  {  double Number;  Number = 49;   // Computing the square root with  // the help of predefined C  // library function  double squareRoot = sqrt(Number);   printf("The Square root of %.2lf = %.2lf",  Number, squareRoot);  return 0;  } Mehak Bhatia, IIMT College of Science and Technology
  • 34.  OutputThe Square root of 49.00 = 7.00 Mehak Bhatia, IIMT College of Science and Technology
  • 35. THANK YOU Mehak Bhatia, IIMT College of Science and Technology