SlideShare a Scribd company logo
1 of 31
Download to read offline
Function
Abhineet Anand
Center For Information Technology
College of Engineering Studies
UPES Dehradun, India

November 18, 2013

Abhineet Anand

Function
Function

Abhineet Anand

Function
Function
Definition
A Function is a self-contained block of statement that perform a coherent
task of some kind.

Abhineet Anand

Function
Function
Definition
A Function is a self-contained block of statement that perform a coherent
task of some kind.
1

Every C Program can be thought of as a collection of these functions.

2

Function is a set of instruction to carryout a particular task.

3

Function after its execution returns a single value.

4

Generally, the function are classified into standard function and
user-defined functions.

5

The Standard function are also called library function or built-in
function.

6

All standard function, such as sqrt(), abs(), log(), sin() etc. are
provided in the library of function.

7

But, most of the application need other functions than those available
in the software, those are known as user-defined functions.
Abhineet Anand

Function
Need of Function

Abhineet Anand

Function
Need of Function
Several advantages of modularizing a program into function includes:
Reduction in code redundancy
Enabling code reuse
Better readability
Information Hiding
Improved debugging and testing
Improved maintainability

Abhineet Anand

Function
Need of Function
Several advantages of modularizing a program into function includes:
Reduction in code redundancy
Enabling code reuse
Better readability
Information Hiding
Improved debugging and testing
Improved maintainability
Function interact with each other to accomplish a particular task. They
are classified according to the following criteria:
Based upon who develop the function
Based upon the number of arguments a function accepts.

Abhineet Anand

Function
Classification of Functions
User-defined function
Defined by user at the time of writing the program.
There are three aspect of working with user-defined functions:
Function Declaration, also known as function prototype
Function Definition
Function use, also known as function call or function invocation

Abhineet Anand

Function
Classification of Functions
User-defined function
Defined by user at the time of writing the program.
There are three aspect of working with user-defined functions:
Function Declaration, also known as function prototype
Function Definition
Function use, also known as function call or function invocation

Declaration
Introduces the function name, function return type, and function
parameters to the program.
The function body (statements)is not part of the declaration
A function must be declared before it is used

Abhineet Anand

Function
Classification of Functions
User-defined function
Defined by user at the time of writing the program.
There are three aspect of working with user-defined functions:
Function Declaration, also known as function prototype
Function Definition
Function use, also known as function call or function invocation

Declaration
Introduces the function name, function return type, and function
parameters to the program.
The function body (statements)is not part of the declaration
A function must be declared before it is used
return_type function_name(parameter_list);
parameter_list: type param1,type param2,type param3, etc
double sqrt(double);
int func1();
Abhineet Anand

Function
Function Definition

Function Definition
Function Definition, also known as function implementation, means
composing a function. Every Function defination consists of two Parts:
Header of the function,
Body of the function.

Abhineet Anand

Function
Function Definition

Function Definition
Function Definition, also known as function implementation, means
composing a function. Every Function defination consists of two Parts:
Header of the function,
Body of the function.
return_type function_name(parameter_list){
// Function body }

Abhineet Anand

Function
Function Definition
Function Definition
The general form of header of a function is:
[return_type] function_name([parameter_list])
The header of function is not terminated with a semicolon.
The body of a function consist of a set of statements enclosed
within braces.
The return statement is used to return the result of the computations
done in the called function and/or to return the program control back
to the calling function.
A function can be defined in any part of the program text or within a
library.
void print_func(){cout << hello world << endl;}

Abhineet Anand

Function
Function Invocation/call/Use

Function Invocation/call/Use
Depending upon their inputs (i.e. parameters) and outputs, functions are
classified as:
Function with no input-output.
Function with inputs and no output.
Function with input and one output.
Function with input and output.

Abhineet Anand

Function
Function with no Input-Output
Function with no Input-Output
A function with no input-output does not accept any input and does not
return any result.
//Function with no input-output
#include<stdio.h>
printsum();
//Function deceleration
main()
//main function, the master function
{
printsum();
//Function Calling
}
printsum()
//definition of function printsum
{
printf("Sum of 2 and 3 is %d",2+3);
}
Abhineet Anand

Function
Function with Input and No Output
Function with Input and No Output
A function can be made flexible by adding input to it.
//Function with input and No output
#include<stdio.h>
void printsum(int, int);
//Function deceleration
main()
//main function, the master function
{
int a,b;
printf("Enter values of a & bt");
scanf("%d %d", &a,&b);
printsum(a,b);
//Function Calling
}
printsum(int x, int y)
//definition of function printsum
{
printf("Sum of %d and %d is %d",x,y,x+y);
}
Abhineet Anand

Function
Function with Input and No Output

Function with Input and No Output
The expression that appear within parenthesis of function call are
known as actual arguments.
The variable declared in the parameter list in the function header are
known as formal parameters.
The below-mentioned steps are followed when a function with input is
invoked:
1
2

3

The Actual arguments expression are evaluated.
The program control is transfered to the called function and the result
of the evaluation of the actual argument expression are assigned to the
formal parameters on one-to-one basis.
The execution of the calling function is suspended and the called
function starts execution.

Abhineet Anand

Function
More Generalization of the previous example
//Function with three input and No output
#include<stdio.h>
void printsum(int, int, char);
//Function deceleration
void main()
//main function, the master function
{
int a,b; char base;
printf("Enter values of a & bt");
scanf("%d %d", &a,&b);
printf("Enter base of output(O, D or H)t");
flushall();
scanf("%c", &base);
printsum(a, b, base);
//Function Calling
}

Abhineet Anand

Function
More Generalization of the previous example

void printsum(int
{
if(base=’O’)
printf("Sum of %d
if(base=’D’)
printf("Sum of %d
if(base=’H’)
printf("Sum of %d
}

x, int y, char base)

//definition of functi

and %d in octal is %o",x,y,x+y);
and %d in decimal is %d",x,y,x+y);
and %d in hexadecimal is %X",x,y,x+y);

Abhineet Anand

Function
Function with Input and One Output
Function with Input and One Output
The Result of the computation may be required in the calling function
for further processing. The best software engineering practices
suggest the following:
1

2

The developed functions should be kept as general as possible so that
they can be used in different situations.
A function should receive inputs in the form of arguments and return
the result of computation instead of directly printing it. A function
should behave like a ’black box’ that receive inputs, and outputs the
desired value.

Abhineet Anand

Function
Function with Input and One Output
Function with Input and One Output
The Result of the computation may be required in the calling function
for further processing. The best software engineering practices
suggest the following:
1

2

The developed functions should be kept as general as possible so that
they can be used in different situations.
A function should receive inputs in the form of arguments and return
the result of computation instead of directly printing it. A function
should behave like a ’black box’ that receive inputs, and outputs the
desired value.

return statement
The return statement is used to return the result of the computations
performed in the called function and/or transfer the program control
back to the calling function.
There are two forms of the return statement:
1
2

return,
return expression.
Abhineet Anand

Function
Function with Input and One Output

Abhineet Anand

Function
Function with Input and Output

Function with Input and Output
More than one value can be indirectly returned to the calling function
by making the use of Pointer.
Pointers can also be used to pass arguments to a function.
Depending upon whether the values or addresses(i.e. pointer) are
passed as arguments to a function, the argument passing methods in
C language are classified as:
1
2

Pass by value,
Pass by address

Abhineet Anand

Function
Function with Input and Output

Passing Arguments by Value
The method of passing arguments by value is also known as call by
value.
The value of actual argument are copied to the formal parameters of
the function.
If the argument are passed by value, the change made in the values of
formal parameters inside the called function are not reflected back to
the calling function.

Abhineet Anand

Function
Function with Input and Output

Abhineet Anand

Function
Function with Input and Output

Passing Arguments by Address/Reference
The method of passing arguments by address or reference is also
known as call by Address or Call by reference.
The address of the actual arguments are passed to the formal
parameters of the function.
If, the arguments are passed by reference, the change made in the
values pointed to by the formal parameter in the called function are
reflected back to the calling function.

Abhineet Anand

Function
Function with Input and Output

Abhineet Anand

Function
Function with Input and Output

Abhineet Anand

Function
Passing Arrays to function
Passing Arrays to function
Like simple variable, arrays can also be passed to functions.
There are two ways to pass array to function:
Passing individual elements of an array one by one.
Passing an Entire array at a time.

Abhineet Anand

Function
Passing Arrays to function
Passing Arrays to function
Like simple variable, arrays can also be passed to functions.
There are two ways to pass array to function:
Passing individual elements of an array one by one.
Passing an Entire array at a time.

Passing individual element
Passing individual element of an array one by one is similar to passing
basic variables.
The individual elements of an array can be passed either by value or
by reference.

Abhineet Anand

Function
Passing Arrays to function
Passing Arrays to function
Like simple variable, arrays can also be passed to functions.
There are two ways to pass array to function:
Passing individual elements of an array one by one.
Passing an Entire array at a time.

Passing individual element
Passing individual element of an array one by one is similar to passing
basic variables.
The individual elements of an array can be passed either by value or
by reference.
Passing entire array at a time
Passing entire array at a time is preferred way of passing arrays to
functions. The entire array is always passed by reference.
Abhineet Anand

Function

More Related Content

What's hot

What's hot (20)

Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Inline function
Inline functionInline function
Inline function
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
File in C language
File in C languageFile in C language
File in C language
 
Pointers
PointersPointers
Pointers
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Function in c
Function in cFunction in c
Function in c
 
File in c
File in cFile in c
File in c
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 

Similar to Function in C

unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfJAVVAJI VENKATA RAO
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignmentAhmad Kamal
 
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
 
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
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 FunctionDeepak Singh
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfBoomBoomers
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxGebruGetachew2
 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programmingnmahi96
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxvekariyakashyap
 
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
Modular Programming in C
Modular Programming in CModular Programming in C
Modular Programming in Cbhawna kol
 

Similar to Function in C (20)

unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignment
 
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
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.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)
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
C FUNCTIONS
C FUNCTIONSC FUNCTIONS
C FUNCTIONS
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdf
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programming
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
 
Modular Programming in C
Modular Programming in CModular Programming in C
Modular Programming in C
 

More from Dr. Abhineet Anand (12)

Software Engineering Introduction
Software Engineering IntroductionSoftware Engineering Introduction
Software Engineering Introduction
 
String
StringString
String
 
Key concept
Key conceptKey concept
Key concept
 
Arrays
ArraysArrays
Arrays
 
C language preliminaries
C language preliminariesC language preliminaries
C language preliminaries
 
Ndfa
NdfaNdfa
Ndfa
 
Finite automata
Finite automataFinite automata
Finite automata
 
Introduction
IntroductionIntroduction
Introduction
 
Micro program
Micro programMicro program
Micro program
 
Memory organization
Memory organizationMemory organization
Memory organization
 
Instruction, interrupts & io processing
Instruction, interrupts & io processingInstruction, interrupts & io processing
Instruction, interrupts & io processing
 
Instruction code
Instruction codeInstruction code
Instruction code
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 

Recently uploaded (20)

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 

Function in C

  • 1. Function Abhineet Anand Center For Information Technology College of Engineering Studies UPES Dehradun, India November 18, 2013 Abhineet Anand Function
  • 3. Function Definition A Function is a self-contained block of statement that perform a coherent task of some kind. Abhineet Anand Function
  • 4. Function Definition A Function is a self-contained block of statement that perform a coherent task of some kind. 1 Every C Program can be thought of as a collection of these functions. 2 Function is a set of instruction to carryout a particular task. 3 Function after its execution returns a single value. 4 Generally, the function are classified into standard function and user-defined functions. 5 The Standard function are also called library function or built-in function. 6 All standard function, such as sqrt(), abs(), log(), sin() etc. are provided in the library of function. 7 But, most of the application need other functions than those available in the software, those are known as user-defined functions. Abhineet Anand Function
  • 5. Need of Function Abhineet Anand Function
  • 6. Need of Function Several advantages of modularizing a program into function includes: Reduction in code redundancy Enabling code reuse Better readability Information Hiding Improved debugging and testing Improved maintainability Abhineet Anand Function
  • 7. Need of Function Several advantages of modularizing a program into function includes: Reduction in code redundancy Enabling code reuse Better readability Information Hiding Improved debugging and testing Improved maintainability Function interact with each other to accomplish a particular task. They are classified according to the following criteria: Based upon who develop the function Based upon the number of arguments a function accepts. Abhineet Anand Function
  • 8. Classification of Functions User-defined function Defined by user at the time of writing the program. There are three aspect of working with user-defined functions: Function Declaration, also known as function prototype Function Definition Function use, also known as function call or function invocation Abhineet Anand Function
  • 9. Classification of Functions User-defined function Defined by user at the time of writing the program. There are three aspect of working with user-defined functions: Function Declaration, also known as function prototype Function Definition Function use, also known as function call or function invocation Declaration Introduces the function name, function return type, and function parameters to the program. The function body (statements)is not part of the declaration A function must be declared before it is used Abhineet Anand Function
  • 10. Classification of Functions User-defined function Defined by user at the time of writing the program. There are three aspect of working with user-defined functions: Function Declaration, also known as function prototype Function Definition Function use, also known as function call or function invocation Declaration Introduces the function name, function return type, and function parameters to the program. The function body (statements)is not part of the declaration A function must be declared before it is used return_type function_name(parameter_list); parameter_list: type param1,type param2,type param3, etc double sqrt(double); int func1(); Abhineet Anand Function
  • 11. Function Definition Function Definition Function Definition, also known as function implementation, means composing a function. Every Function defination consists of two Parts: Header of the function, Body of the function. Abhineet Anand Function
  • 12. Function Definition Function Definition Function Definition, also known as function implementation, means composing a function. Every Function defination consists of two Parts: Header of the function, Body of the function. return_type function_name(parameter_list){ // Function body } Abhineet Anand Function
  • 13. Function Definition Function Definition The general form of header of a function is: [return_type] function_name([parameter_list]) The header of function is not terminated with a semicolon. The body of a function consist of a set of statements enclosed within braces. The return statement is used to return the result of the computations done in the called function and/or to return the program control back to the calling function. A function can be defined in any part of the program text or within a library. void print_func(){cout << hello world << endl;} Abhineet Anand Function
  • 14. Function Invocation/call/Use Function Invocation/call/Use Depending upon their inputs (i.e. parameters) and outputs, functions are classified as: Function with no input-output. Function with inputs and no output. Function with input and one output. Function with input and output. Abhineet Anand Function
  • 15. Function with no Input-Output Function with no Input-Output A function with no input-output does not accept any input and does not return any result. //Function with no input-output #include<stdio.h> printsum(); //Function deceleration main() //main function, the master function { printsum(); //Function Calling } printsum() //definition of function printsum { printf("Sum of 2 and 3 is %d",2+3); } Abhineet Anand Function
  • 16. Function with Input and No Output Function with Input and No Output A function can be made flexible by adding input to it. //Function with input and No output #include<stdio.h> void printsum(int, int); //Function deceleration main() //main function, the master function { int a,b; printf("Enter values of a & bt"); scanf("%d %d", &a,&b); printsum(a,b); //Function Calling } printsum(int x, int y) //definition of function printsum { printf("Sum of %d and %d is %d",x,y,x+y); } Abhineet Anand Function
  • 17. Function with Input and No Output Function with Input and No Output The expression that appear within parenthesis of function call are known as actual arguments. The variable declared in the parameter list in the function header are known as formal parameters. The below-mentioned steps are followed when a function with input is invoked: 1 2 3 The Actual arguments expression are evaluated. The program control is transfered to the called function and the result of the evaluation of the actual argument expression are assigned to the formal parameters on one-to-one basis. The execution of the calling function is suspended and the called function starts execution. Abhineet Anand Function
  • 18. More Generalization of the previous example //Function with three input and No output #include<stdio.h> void printsum(int, int, char); //Function deceleration void main() //main function, the master function { int a,b; char base; printf("Enter values of a & bt"); scanf("%d %d", &a,&b); printf("Enter base of output(O, D or H)t"); flushall(); scanf("%c", &base); printsum(a, b, base); //Function Calling } Abhineet Anand Function
  • 19. More Generalization of the previous example void printsum(int { if(base=’O’) printf("Sum of %d if(base=’D’) printf("Sum of %d if(base=’H’) printf("Sum of %d } x, int y, char base) //definition of functi and %d in octal is %o",x,y,x+y); and %d in decimal is %d",x,y,x+y); and %d in hexadecimal is %X",x,y,x+y); Abhineet Anand Function
  • 20. Function with Input and One Output Function with Input and One Output The Result of the computation may be required in the calling function for further processing. The best software engineering practices suggest the following: 1 2 The developed functions should be kept as general as possible so that they can be used in different situations. A function should receive inputs in the form of arguments and return the result of computation instead of directly printing it. A function should behave like a ’black box’ that receive inputs, and outputs the desired value. Abhineet Anand Function
  • 21. Function with Input and One Output Function with Input and One Output The Result of the computation may be required in the calling function for further processing. The best software engineering practices suggest the following: 1 2 The developed functions should be kept as general as possible so that they can be used in different situations. A function should receive inputs in the form of arguments and return the result of computation instead of directly printing it. A function should behave like a ’black box’ that receive inputs, and outputs the desired value. return statement The return statement is used to return the result of the computations performed in the called function and/or transfer the program control back to the calling function. There are two forms of the return statement: 1 2 return, return expression. Abhineet Anand Function
  • 22. Function with Input and One Output Abhineet Anand Function
  • 23. Function with Input and Output Function with Input and Output More than one value can be indirectly returned to the calling function by making the use of Pointer. Pointers can also be used to pass arguments to a function. Depending upon whether the values or addresses(i.e. pointer) are passed as arguments to a function, the argument passing methods in C language are classified as: 1 2 Pass by value, Pass by address Abhineet Anand Function
  • 24. Function with Input and Output Passing Arguments by Value The method of passing arguments by value is also known as call by value. The value of actual argument are copied to the formal parameters of the function. If the argument are passed by value, the change made in the values of formal parameters inside the called function are not reflected back to the calling function. Abhineet Anand Function
  • 25. Function with Input and Output Abhineet Anand Function
  • 26. Function with Input and Output Passing Arguments by Address/Reference The method of passing arguments by address or reference is also known as call by Address or Call by reference. The address of the actual arguments are passed to the formal parameters of the function. If, the arguments are passed by reference, the change made in the values pointed to by the formal parameter in the called function are reflected back to the calling function. Abhineet Anand Function
  • 27. Function with Input and Output Abhineet Anand Function
  • 28. Function with Input and Output Abhineet Anand Function
  • 29. Passing Arrays to function Passing Arrays to function Like simple variable, arrays can also be passed to functions. There are two ways to pass array to function: Passing individual elements of an array one by one. Passing an Entire array at a time. Abhineet Anand Function
  • 30. Passing Arrays to function Passing Arrays to function Like simple variable, arrays can also be passed to functions. There are two ways to pass array to function: Passing individual elements of an array one by one. Passing an Entire array at a time. Passing individual element Passing individual element of an array one by one is similar to passing basic variables. The individual elements of an array can be passed either by value or by reference. Abhineet Anand Function
  • 31. Passing Arrays to function Passing Arrays to function Like simple variable, arrays can also be passed to functions. There are two ways to pass array to function: Passing individual elements of an array one by one. Passing an Entire array at a time. Passing individual element Passing individual element of an array one by one is similar to passing basic variables. The individual elements of an array can be passed either by value or by reference. Passing entire array at a time Passing entire array at a time is preferred way of passing arrays to functions. The entire array is always passed by reference. Abhineet Anand Function