SlideShare a Scribd company logo
Functions
Functions
• A function is a named block of code that performs a specific task.
• It allows you to write a piece of logic once and reuse it wherever
needed in the program.
• This helps keep your code clean, organized, and easier to understand.
• A function is a set of statements enclosed within curly brackets ({})
Why Do We Need Functions in C
Programming?
Some of the key benefits of using functions are:
• Enables reusability and reduces redundancy
• Makes a code modular
• Provides abstraction functionality
• The program becomes easy to understand and manage
• Breaks an extensive program into smaller and simpler pieces
Basic Syntax of Functions
• The basic syntax of functions in C programming is:
return_type function_name(arg1, arg2, … argn)
{
Body of the function //Statements to be processed
}
Aspects of Functions in C Programming
1. Function Declaration
The function declaration lets the compiler know the name, number of parameters,
data types of parameters, and return type of a function. However, writing
parameter names during declaration is optional, as you can do that even while
defining the function.
2. Function Call
As the name gives out, a function call is calling a function to be executed by the
compiler. You can call the function at any point in the entire program. The only thing
to take care of is that you need to pass as many arguments of the same data type as
mentioned while declaring the function. If the function parameter does not differ,
the compiler will execute the program and give the return value.
3. Function Definition
It is defining the actual statements that the compiler will execute upon calling the
function. You can think of it as the body of the function. Function definition must
return only one value at the end of the execution.
Types of Functions in C
Functions in C programming are classified into two types:
1. Library Functions
2. User-Defined Functions
Library Functions
Also referred to as predefined functions, library functions are already
defined in the C libraries. This means that we do not have to write a
definition or the function’s body to call them. We can simply call them
without defining them as they are already defined. However, we need
to include the library at the beginning of the code for calling a library
function. We can then use the proper syntax of the function to call
them.
User-Defined Functions
• These are the functions that a developer or the user declares, defines,
and calls in a program.
• This increases the scope and functionality, and reusability of C
programming as we can define and use any function we want.
• A major plus point of C programming is that we can add a user-
defined to any library to use it in other programs.
Different Ways of Calling a Function
Depending on whether the function accepts arguments or not and
returns a value or not, there can be four different aspects of C function
calls, which are:
Function Prototype
• It specifies the type of value that the function will return and the
types of values that will be passed to it.
• It is defined at the beginning, before the function is called. Syntax:
return-type function-name(list of arguments);
Example: void sum(int, int);
Function Call
• A function can be called by specifying its name and a list of
arguments enclosed in parentheses and separated by commas.
• If there are no arguments, empty parentheses are placed after
the function name.
• If the function returns a value, the function call is written as an
assignment statement as:
A = sum(x, y);
Function Arguments and Parameters
• Arguments are also called actual parameters. Arguments are written
within parenthesis at the time of function call.
• Parameters are also called formal parameters. These are written
within parenthesis at the time of function definition.
Return Statement
• It is the last statement of the function that returns certain values.
• It returns specific types of values to the place from where the
function was called.
Syntax:
return(variable-name or constant);
Categories of Function
• Function with no arguments and no return
• Function with arguments but no return
• Function with no arguments and return
• Function with arguments and return
Methods of Calling a Function
• Call by Value
• Call by Reference
Call by value
• This method copies the value of actual parameters into the formal
parameters.
• During execution, any changes made to the formal parameters do not
affect the actual parameters.
Call by Value
Call by Reference
• The reference (address) of the original variable is passed.
• The function does not create its own copy; it refers to the original
values by reference.
• Functions work with the original data, so any changes affect the
original data.
Call by Reference
#include <stdio.h>
// Function declaration
int add(int a, int b);
int main()
{
int result = add(5, 10); // Function call
printf("Sum: %dn", result);
return 0;
}
// Function definition
int add(int a, int b)
{
return a + b;
}

More Related Content

PPTX
FUNCTIONS IN C.pptx
PPTX
FUNCTIONS IN C.pptx
PPTX
unit_2 (1).pptx
PPTX
Functions and structure in programming c
PPTX
Functions in C.pptx
PDF
VIT351 Software Development VI Unit1
PPTX
unit_2.pptx
PPTX
Module 3-Functions
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
unit_2 (1).pptx
Functions and structure in programming c
Functions in C.pptx
VIT351 Software Development VI Unit1
unit_2.pptx
Module 3-Functions

Similar to functions in the c programming and the examples (20)

PPTX
Functions in c language
PPTX
Functions in c language
PPTX
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
PPTX
FUNCTIONengineeringtechnologyslidesh.pptx
PPTX
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
PPTX
CH.4FUNCTIONS IN C (1).pptx
PDF
unit3 part2 pcds function notes.pdf
PPTX
PPTX
Functions in C
PPT
arrays.ppt
PPTX
PPTX
Detailed concept of function in c programming
PPTX
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
PDF
Functions in c mrs.sowmya jyothi
PPT
Lecture 11 - Functions
PPTX
Function C programming
DOC
4. function
PDF
User_Defined_Functions_ppt_slideshare.
PPT
User defined functions in C programmig
PPTX
function of C.pptx
Functions in c language
Functions in c language
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
FUNCTIONengineeringtechnologyslidesh.pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C (1).pptx
unit3 part2 pcds function notes.pdf
Functions in C
arrays.ppt
Detailed concept of function in c programming
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Functions in c mrs.sowmya jyothi
Lecture 11 - Functions
Function C programming
4. function
User_Defined_Functions_ppt_slideshare.
User defined functions in C programmig
function of C.pptx
Ad

Recently uploaded (20)

PDF
01-Introduction-to-Information-Management.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Computing-Curriculum for Schools in Ghana
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
master seminar digital applications in india
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Institutional Correction lecture only . . .
01-Introduction-to-Information-Management.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Pre independence Education in Inndia.pdf
Microbial diseases, their pathogenesis and prophylaxis
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Computing-Curriculum for Schools in Ghana
Microbial disease of the cardiovascular and lymphatic systems
master seminar digital applications in india
Abdominal Access Techniques with Prof. Dr. R K Mishra
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Supply Chain Operations Speaking Notes -ICLT Program
Renaissance Architecture: A Journey from Faith to Humanism
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
human mycosis Human fungal infections are called human mycosis..pptx
Complications of Minimal Access Surgery at WLH
Institutional Correction lecture only . . .
Ad

functions in the c programming and the examples

  • 2. Functions • A function is a named block of code that performs a specific task. • It allows you to write a piece of logic once and reuse it wherever needed in the program. • This helps keep your code clean, organized, and easier to understand. • A function is a set of statements enclosed within curly brackets ({})
  • 3. Why Do We Need Functions in C Programming? Some of the key benefits of using functions are: • Enables reusability and reduces redundancy • Makes a code modular • Provides abstraction functionality • The program becomes easy to understand and manage • Breaks an extensive program into smaller and simpler pieces
  • 4. Basic Syntax of Functions • The basic syntax of functions in C programming is: return_type function_name(arg1, arg2, … argn) { Body of the function //Statements to be processed }
  • 5. Aspects of Functions in C Programming 1. Function Declaration The function declaration lets the compiler know the name, number of parameters, data types of parameters, and return type of a function. However, writing parameter names during declaration is optional, as you can do that even while defining the function. 2. Function Call As the name gives out, a function call is calling a function to be executed by the compiler. You can call the function at any point in the entire program. The only thing to take care of is that you need to pass as many arguments of the same data type as mentioned while declaring the function. If the function parameter does not differ, the compiler will execute the program and give the return value. 3. Function Definition It is defining the actual statements that the compiler will execute upon calling the function. You can think of it as the body of the function. Function definition must return only one value at the end of the execution.
  • 6. Types of Functions in C Functions in C programming are classified into two types: 1. Library Functions 2. User-Defined Functions
  • 7. Library Functions Also referred to as predefined functions, library functions are already defined in the C libraries. This means that we do not have to write a definition or the function’s body to call them. We can simply call them without defining them as they are already defined. However, we need to include the library at the beginning of the code for calling a library function. We can then use the proper syntax of the function to call them.
  • 8. User-Defined Functions • These are the functions that a developer or the user declares, defines, and calls in a program. • This increases the scope and functionality, and reusability of C programming as we can define and use any function we want. • A major plus point of C programming is that we can add a user- defined to any library to use it in other programs.
  • 9. Different Ways of Calling a Function Depending on whether the function accepts arguments or not and returns a value or not, there can be four different aspects of C function calls, which are:
  • 10. Function Prototype • It specifies the type of value that the function will return and the types of values that will be passed to it. • It is defined at the beginning, before the function is called. Syntax: return-type function-name(list of arguments); Example: void sum(int, int);
  • 11. Function Call • A function can be called by specifying its name and a list of arguments enclosed in parentheses and separated by commas. • If there are no arguments, empty parentheses are placed after the function name. • If the function returns a value, the function call is written as an assignment statement as: A = sum(x, y);
  • 12. Function Arguments and Parameters • Arguments are also called actual parameters. Arguments are written within parenthesis at the time of function call. • Parameters are also called formal parameters. These are written within parenthesis at the time of function definition.
  • 13. Return Statement • It is the last statement of the function that returns certain values. • It returns specific types of values to the place from where the function was called. Syntax: return(variable-name or constant);
  • 14. Categories of Function • Function with no arguments and no return • Function with arguments but no return • Function with no arguments and return • Function with arguments and return
  • 15. Methods of Calling a Function • Call by Value • Call by Reference
  • 16. Call by value • This method copies the value of actual parameters into the formal parameters. • During execution, any changes made to the formal parameters do not affect the actual parameters.
  • 18. Call by Reference • The reference (address) of the original variable is passed. • The function does not create its own copy; it refers to the original values by reference. • Functions work with the original data, so any changes affect the original data.
  • 20. #include <stdio.h> // Function declaration int add(int a, int b); int main() { int result = add(5, 10); // Function call printf("Sum: %dn", result); return 0; } // Function definition int add(int a, int b) { return a + b; }