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

PPT
Lecture 13 - Storage Classes
PPT
Lecture 14 - Scope Rules
PPT
Lecture 9- Control Structures 1
PPT
Lecture 8- Data Input and Output
PPT
Lecture 10 - Control Structures 2
PPT
Lecture 6- Intorduction to C Programming
PPT
Lecture 12 - Recursion
PPTX
Function in C program
Lecture 13 - Storage Classes
Lecture 14 - Scope Rules
Lecture 9- Control Structures 1
Lecture 8- Data Input and Output
Lecture 10 - Control Structures 2
Lecture 6- Intorduction to C Programming
Lecture 12 - Recursion
Function in C program

What's hot (20)

PPTX
Function in c
PPTX
Presentation on function
PPTX
C function
PPTX
C programming(part 3)
PPTX
Programming in C (part 2)
PPSX
Function in c
PPTX
Function in c program
PPT
RECURSION IN C
PPTX
Storage classes in c language
PPT
Recursion in c
PPTX
PPTX
Function in c program
PPSX
Functions in c
PPT
lets play with "c"..!!! :):)
PDF
Functions
PPTX
Functions in C
PPT
Function in C Language
PPTX
Function in c language(defination and declaration)
PPTX
C Programming Unit-2
PPTX
Functions in c language
Function in c
Presentation on function
C function
C programming(part 3)
Programming in C (part 2)
Function in c
Function in c program
RECURSION IN C
Storage classes in c language
Recursion in c
Function in c program
Functions in c
lets play with "c"..!!! :):)
Functions
Functions in C
Function in C Language
Function in c language(defination and declaration)
C Programming Unit-2
Functions in c language
Ad

Similar to Lecture 11 - Functions (20)

PPTX
Function
PPTX
The function contains the set of programming statements enclosed by {}  when ...
PPTX
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
PPTX
FUNCTIONengineeringtechnologyslidesh.pptx
PPTX
FUNCTIONS IN C.pptx
PPTX
FUNCTIONS IN C.pptx
PPTX
functions in the c programming and the examples
PPTX
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
PPTX
PPTX
Programming Methodologies Functions - C Language
PPTX
Module 3-Functions
PDF
functionsinc-130108032745-phpapp01.pdf
PPT
C FUNCTIONS
PDF
Functions in c mrs.sowmya jyothi
PPTX
PPTX
Lecture 1_Functions in C.pptx
PPT
U19CS101 - PPS Unit 4 PPT (1).ppt
PDF
1.6 Function.pdf
PPTX
PPTX
unit_2 (1).pptx
Function
The function contains the set of programming statements enclosed by {}  when ...
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
FUNCTIONengineeringtechnologyslidesh.pptx
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
functions in the c programming and the examples
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Programming Methodologies Functions - C Language
Module 3-Functions
functionsinc-130108032745-phpapp01.pdf
C FUNCTIONS
Functions in c mrs.sowmya jyothi
Lecture 1_Functions in C.pptx
U19CS101 - PPS Unit 4 PPT (1).ppt
1.6 Function.pdf
unit_2 (1).pptx
Ad

More from Md. Imran Hossain Showrov (14)

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

Recently uploaded (20)

PPTX
Cell Structure & Organelles in detailed.
PDF
Computing-Curriculum for Schools in Ghana
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Weekly quiz Compilation Jan -July 25.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Classroom Observation Tools for Teachers
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
Lesson notes of climatology university.
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
master seminar digital applications in india
Cell Structure & Organelles in detailed.
Computing-Curriculum for Schools in Ghana
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Anesthesia in Laparoscopic Surgery in India
Weekly quiz Compilation Jan -July 25.pdf
Microbial diseases, their pathogenesis and prophylaxis
Supply Chain Operations Speaking Notes -ICLT Program
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Classroom Observation Tools for Teachers
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pharmacology of Heart Failure /Pharmacotherapy of CHF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Orientation - ARALprogram of Deped to the Parents.pptx
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Lesson notes of climatology university.
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Microbial disease of the cardiovascular and lymphatic systems
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
master seminar digital applications in india

Lecture 11 - Functions

  • 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.