SlideShare a Scribd company logo
1 of 20
Functions
Md. Imran Hossain Showrov (showrovsworld@gmail.com)
11
1
Outline
 Definition of Functions
 Advantages of Functions
 Accessing a Function
 Function Prototypes
 Function Arguments
What are Functions?
 We have already seen that C supports a number of
library functions.
 However, C allows programmers to define their own
functions for carrying out various individual tasks that
allows a large program to be broken down into a
number of smaller, self-contained components, each
of which has some unique, identifiable purpose.
 C programs can be modularized through the
intelligent use of such functions.
What are Functions? (cont..)
 A function is a self contained program segment that
carries out some specific, well-defined task.
 Every C program consists of one or more functions.
 Remember, one of these functions must be called
main because execution of the program will always
begin by carrying out the instructions in main.
What are Functions? (cont..)
 A function will carry out its intended action
whenever it is accessed / called from some other
portion of the program.
 Once the function has carried out its intended action,
control will be returned to the point from which the
function was accessed.
 Generally, a function will process information that is
passed to it from the calling portion of the program,
and return a single value.
Advantages of Functions
There are several advantages of user-defined functions:
 Many programs require that a particular group of
instructions be accessed repeatedly from several
different places within the program.The repeated
program can be placed within a single function, which
can then be accessed whenever it is needed.
 Functions give the logical clarity as it decomposes the
program into several concise functions, where each
function represents some well-defined part of the
overall problem.
Advantages of Functions (cont..)
 The use of a function avoids the need for redundant
(repeated) programming of the same instructions.
 Functions make the program easier to write and
debug.
 The use of functions enables a programmer to build a
customized library of frequently used routines or of
routines containing system-dependent features.
 It promotes portability since programs can be written
that are independent of system-dependent features.
Defining a Function
A function definition in C programming consists of a
function header and a function body. Here are all the parts
of a function −
 ReturnType: 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.
Defining a Function (cont..)
 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. 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.
Defining a Function (cont..)
 A function declaration tells the compiler about a
function's name, return type, and parameters.
 A function definition provides the actual body of the
function.
return_type function_name( parameter list ) {
body of the function
}
Functions Example1
Functions Example2
// function returning the max between two numbers
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Accessing a Function
 A function can be accessed (called) by specifying its
name, followed by a list of arguments enclosed in
parentheses and separated by commas.
 If the function call does not require any argument, an
empty pair of parentheses must follow the name of
the function.
 If the function returns a value, the function access is
often written as an assignment statement-
y = polynomial(x);
Accessing A Functions (cont..)
 On the other hand, if the function does not return anything,
the function access appears by itself
display (a, b, c);
Function Prototypes
 A function prototype or function interface is a
declaration of a function that specifies the function's
name and type signature, but omits the function body.
 While a function definition specifies how the function
does what it does (the "implementation"), a function
prototype merely specifies its interface, i.e. what data
types go in and come out of it.
Function Prototypes: Purposes
The Function prototype serves the following purposes:
 It tells the return type of the data that the function
will return.
 It tells the number of arguments passed to the
function.
 It tells the data types of the each of the passed
arguments.
 Also it tells the order in which the arguments are
passed to the function.
Function Prototypes (example)
#include <stdio.h>
int myfunction(int n); /* Prototype */
int main() {
printf("%dn", myfunction());
}
int myfunction( int n) {
if (n == 0)
return 1;
else
return n * myfunction(n - 1);
}
Function Arguments
 If a function is to use arguments, it must declare
variables that accept the values of the arguments.
These variables are called the formal parameters of
the function.
 Formal parameters behave like other local variables
inside the function and are created upon entry into
the function and destroyed upon exit.
Function Arguments (cont..)
 While calling a function, there are two ways in which
arguments can be passed to a function:
1. Call by value: This method copies the actual value of an
argument into the formal parameter of the function. In this
case, changes made to the parameter inside the function have
no effect on the argument.
2. Call by reference: This method copies the address of an
argument into the formal parameter. Inside the function, the
address is used to access the actual argument used in the call.
This means that changes made to the parameter affect the
argument.
 By default, C uses call by value to pass arguments.
Lecture 11 - Functions

More Related Content

What's hot

What's hot (20)

Function in c
Function in cFunction in c
Function in c
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
C function
C functionC function
C function
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
Function in c
Function in cFunction in c
Function in c
 
Function in c program
Function in c programFunction in c program
Function in c program
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
Function in c
Function in cFunction in c
Function in c
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Functions in c
Functions in cFunctions in c
Functions in c
 
lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)
 
Functions
FunctionsFunctions
Functions
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Function in C Language
Function in C Language Function in C Language
Function in C Language
 
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)
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 

Similar to Lecture 11 - Functions

Functions assignment
Functions assignmentFunctions assignment
Functions assignmentAhmad Kamal
 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programmingnmahi96
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptxRhishav Poudyal
 
FUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdfFUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdfRITHIKA R S
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functionsnikshaikh786
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1YOGESH SINGH
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)Mansi Tyagi
 
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
 
function of C.pptx
function of C.pptxfunction of C.pptx
function of C.pptxshivas379526
 
Chapter 13.1.6
Chapter 13.1.6Chapter 13.1.6
Chapter 13.1.6patcha535
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1Jeevan Raj
 

Similar to Lecture 11 - Functions (20)

Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignment
 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programming
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
4. function
4. function4. function
4. function
 
FUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdfFUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdf
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 
Functions
Functions Functions
Functions
 
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)
 
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
 
function of C.pptx
function of C.pptxfunction of C.pptx
function of C.pptx
 
Functions
FunctionsFunctions
Functions
 
Chapter 13.1.6
Chapter 13.1.6Chapter 13.1.6
Chapter 13.1.6
 
Functions in C
Functions in CFunctions in C
Functions in C
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1
 
Chapter 1.ppt
Chapter 1.pptChapter 1.ppt
Chapter 1.ppt
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 

More from Md. Imran Hossain Showrov (14)

Lecture 22 - Error Handling
Lecture 22 - Error HandlingLecture 22 - Error Handling
Lecture 22 - Error Handling
 
Lecture 21 - Preprocessor and Header File
Lecture 21 - Preprocessor and Header FileLecture 21 - Preprocessor and Header File
Lecture 21 - Preprocessor and Header File
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
 
Lecture 19 - Struct and Union
Lecture 19 - Struct and UnionLecture 19 - Struct and Union
Lecture 19 - Struct and Union
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
Lecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional ArrayLecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional Array
 
Lecture 17 - Strings
Lecture 17 - StringsLecture 17 - Strings
Lecture 17 - Strings
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
 
Lecture 7- Operators and Expressions
Lecture 7- Operators and Expressions Lecture 7- Operators and Expressions
Lecture 7- Operators and Expressions
 
Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language
 
Lecture 4- Computer Software and Languages
Lecture 4- Computer Software and LanguagesLecture 4- Computer Software and Languages
Lecture 4- Computer Software and Languages
 
Lecture 3 - Processors, Memory and I/O devices
Lecture 3 - Processors, Memory and I/O devicesLecture 3 - Processors, Memory and I/O devices
Lecture 3 - Processors, Memory and I/O devices
 
Lecture 2 - Introductory Concepts
Lecture 2 - Introductory ConceptsLecture 2 - Introductory Concepts
Lecture 2 - Introductory Concepts
 
Lecture 1- History of C Programming
Lecture 1- History of C Programming Lecture 1- History of C Programming
Lecture 1- History of C Programming
 

Recently uploaded

ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 

Lecture 11 - Functions

  • 1. Functions Md. Imran Hossain Showrov (showrovsworld@gmail.com) 11 1
  • 2. Outline  Definition of Functions  Advantages of Functions  Accessing a Function  Function Prototypes  Function Arguments
  • 3. What are Functions?  We have already seen that C supports a number of library functions.  However, C allows programmers to define their own functions for carrying out various individual tasks that allows a large program to be broken down into a number of smaller, self-contained components, each of which has some unique, identifiable purpose.  C programs can be modularized through the intelligent use of such functions.
  • 4. What are Functions? (cont..)  A function is a self contained program segment that carries out some specific, well-defined task.  Every C program consists of one or more functions.  Remember, one of these functions must be called main because execution of the program will always begin by carrying out the instructions in main.
  • 5. What are Functions? (cont..)  A function will carry out its intended action whenever it is accessed / called from some other portion of the program.  Once the function has carried out its intended action, control will be returned to the point from which the function was accessed.  Generally, a function will process information that is passed to it from the calling portion of the program, and return a single value.
  • 6. Advantages of Functions There are several advantages of user-defined functions:  Many programs require that a particular group of instructions be accessed repeatedly from several different places within the program.The repeated program can be placed within a single function, which can then be accessed whenever it is needed.  Functions give the logical clarity as it decomposes the program into several concise functions, where each function represents some well-defined part of the overall problem.
  • 7. Advantages of Functions (cont..)  The use of a function avoids the need for redundant (repeated) programming of the same instructions.  Functions make the program easier to write and debug.  The use of functions enables a programmer to build a customized library of frequently used routines or of routines containing system-dependent features.  It promotes portability since programs can be written that are independent of system-dependent features.
  • 8. Defining a Function A function definition in C programming consists of a function header and a function body. Here are all the parts of a function −  ReturnType: 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.
  • 9. Defining a Function (cont..)  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. 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.
  • 10. Defining a Function (cont..)  A function declaration tells the compiler about a function's name, return type, and parameters.  A function definition provides the actual body of the function. return_type function_name( parameter list ) { body of the function }
  • 12. Functions Example2 // function returning the max between two numbers int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; }
  • 13. Accessing a Function  A function can be accessed (called) by specifying its name, followed by a list of arguments enclosed in parentheses and separated by commas.  If the function call does not require any argument, an empty pair of parentheses must follow the name of the function.  If the function returns a value, the function access is often written as an assignment statement- y = polynomial(x);
  • 14. Accessing A Functions (cont..)  On the other hand, if the function does not return anything, the function access appears by itself display (a, b, c);
  • 15. Function Prototypes  A function prototype or function interface is a declaration of a function that specifies the function's name and type signature, but omits the function body.  While a function definition specifies how the function does what it does (the "implementation"), a function prototype merely specifies its interface, i.e. what data types go in and come out of it.
  • 16. Function Prototypes: Purposes The Function prototype serves the following purposes:  It tells the return type of the data that the function will return.  It tells the number of arguments passed to the function.  It tells the data types of the each of the passed arguments.  Also it tells the order in which the arguments are passed to the function.
  • 17. Function Prototypes (example) #include <stdio.h> int myfunction(int n); /* Prototype */ int main() { printf("%dn", myfunction()); } int myfunction( int n) { if (n == 0) return 1; else return n * myfunction(n - 1); }
  • 18. Function Arguments  If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function.  Formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit.
  • 19. Function Arguments (cont..)  While calling a function, there are two ways in which arguments can be passed to a function: 1. Call by value: This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. 2. Call by reference: This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.  By default, C uses call by value to pass arguments.