SlideShare a Scribd company logo
1 of 16
Download to read offline
Functions
By Ziyad khalid
Agenda -
1. Introduction
2. Why We Use Functions?
3. Advantages of functions
4. Function aspects
5. Types of functions
6. Return value
7. Different aspects of function calling (with some examples)
8. C library functions
Introduction
In c, we can divide a large program into the basic building blocks known as function.
The function contains the set of programming statements enclosed by {}. A function
can be called multiple times to provide reusability and modularity to the C program.
In other words, we can say that the collection of functions creates a program. The
function is also known as procedureor subroutinein other programming languages.
Why We Use Functions?
The first reason is reusability. Once a function is defined, it can be used over and over and over again. You can invoke the
same function many times in your program, which saves you work. Imagine what programming would be like if you had to
teach the computer about sines every time you needed to find the sine of an angle! You'd never get your program finished!
Another aspect of reusability is that a single function can be used in several different (and separate) programs. When you
need to write a new program, you can go back to your old programs, find the functions you need, and reuse those functions
in your new program. You can also reuse functions that somebody else has written for you, such as the sine and cosine
functions.
The second reason is abstraction. In order to use a particular function you need to know the following things:
The name of the function;
What the function does;
What arguments you must give to the function; and
What kind of result the function returns.
But notice: If you just want to use the function in your program, you don't have to know how it works inside! You don't have
to understand anything about what goes on inside the function.
It's sort of like driving a car or using a telephone. With an automobile, you don't need to understand every detail about the
engine and drive train and wheels, if all you want to do is drive the car. Similarly, with a telephone, you don't have to
understand everything about the phone system in order to make a call.
The only time you need to know how a function works inside is when you need to write the function, or change how it works.
(It's like a car again; you need to know how a car works in order to build one or fix one.) But once a function is written and
working, you never need to look at its insides again.
Together, these two reasons make functions extremely useful--practically essential!-for programmers who write large
programs. The ability to divide a program into abstract, reusable pieces is what makes it possible to write large programs
that actually work right.
Advantages of functions in C
• There are the following advantages of C functions.
• By using functions, we can avoid rewriting same logic/code again and
again in a program.
• We can call C functions any number of times in a program and from any
place in a program.
• We can track a large C program easily when it is divided into multiple
functions.
• Reusability is the main achievement of C functions.
• However, Function calling is always a overhead in a C program.
Function Aspects
There are three aspects of a C function.
Function declaration A function must be declared globally in a c program to tell the compiler about the function
name, function parameters, and return type.
Function call Function can be called from anywhere in the program. The parameter list must not differ in function
calling and function declaration. We must pass the same number of functions as it is declared in the function
declaration.
Function definition It contains the actual statements which are to be executed. It is the most important aspect to
which the control comes when the function is called. Here, we must notice that only one value can be returned from
the function.
The syntax of creating function in c language is given below:
SN C function aspects Syntax
1 Function declaration
return_type function_name
(argument list);
2 Function call
function_name
(argument_list)
3 Function definition
return_type function_name
(argument list) {function
body;}
return_type function_name (data_type parameter...){
//code to be executed }
Types of Functions
There are two types of functions in C programming:
Library Functions:
Are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(),
floor() etc.
User-defined functions:
Are the functions which are created by the C programmer, so that he/she can use it many times. It
reduces the complexity of a big program and optimizes the code.
Functions
User-Defined functions Library functions
Return Value
A C function may or may not return a value from the function. If you don't have to return any value from the
function, use void for the return type.
Let's see a simple example of C function that doesn't return any value from the function.
Example without return value:
If you want to return any value from the function, you need to use any data type such as int, long, char, etc. The
return type depends on the value to be returned from the function.
Let's see a simple example of C function that returns int value from the function.
Example with return value:
In the above example, we have to return 10 as a value, so the return type is int. If you want to return floating-point
value (e.g., 10.2, 3.1, 54.5, etc), you need to use float as the return type of the method.
void hello(){
printf("hello c");
}
int get(){
return 10;
}
OUTPUT :
hello c
OUTPUT :
10
float get(){
return 10.2;
}
OUTPUT :
10.2
Now, you need to call the function, to get the value of the function.
Different aspects of function calling
A function may or may not accept any argument. It may or may not return
any value. Based on these facts, There are four different aspects of
function calls.
• function without arguments and without return value
• function without arguments and with return value
• function with arguments and without return value
• function with arguments and with return value
Example for Function without argument and return value.
Example 1.
#include<stdio.h>
void printName();
void main ()
{
printf("Hello ");
printName();
}
void printName()
{
printf("India");
}
OUTPUT :
Hello India
Example 2.
#include<stdio.h>
void sum();
void main()
{
printf("nGoing to calculat
e the sum of two numbers:
");
sum();
}
void sum()
{
int a,b;
printf("nEnter two number
s");
scanf("%d %d",&a,&b);
printf("The sum is %d",a+
b);
}
OUTPUT :
Going to calculate the
sum of two numbers:
Enter two numbers 10
24
The sum is 34
Example for Function without argument and with return value
Example 1
#include<stdio.h>
int sum();
void main()
{
int result;
printf("nGoing to calculate the s
um of two numbers:");
result = sum();
printf("%d",result);
}
int sum()
{
int a,b;
printf("nEnter two numbers");
scanf("%d %d",&a,&b);
return a+b;
}
OUTPUT :
Going to calculate the
sum of two numbers:
Enter two numbers 10
24
The sum is 34
Example 2: program to calculate the
area of the square
#include<stdio.h>
int sum();
void main()
{
printf("Going to calculate the area of t
he squaren");
float area = square();
printf("The area of the square: %fn",
area);
}
int square()
{
float side;
printf("Enter the length of the side in
meters: ");
scanf("%f",&side);
return side * side;
}
OUTPUT :
Going to calculate the
area of the square
Enter the length of the
side in meters: 10
The area of the square:
100.000000
Example for Function with argument and without return value
Example 1
#include<stdio.h>
void sum(int, int);
void main()
{
int a,b,result;
printf("nGoing to calculate the sum of
two numbers:");
printf("nEnter two numbers:");
scanf("%d %d",&a,&b);
sum(a,b);
}
void sum(int a, int b)
{
printf("nThe sum is %d",a+b);
}
OUTPUT :
Going to calculate the sum
of two numbers:
Enter two numbers 10
24
The sum is 34
#include<stdio.h>
void average(int, int, int, int, int);
void main()
{
int a,b,c,d,e;
printf("nGoing to calculate the
average of five numbers:");
printf("nEnter five numbers:");
scanf("%d %d %d %d
%d",&a,&b,&c,&d,&e);
average(a,b,c,d,e);
}
void average(int a, int b, int c, int d,
int e)
{
float avg;
avg = (a+b+c+d+e)/5;
printf("The average of given five
numbers : %f",avg);
}
OUTPUT :
Going to calculate the
average of five numbers:
Enter five numbers:10
20
30
40
50
The average of given five
numbers : 30.000000
Example 2: program to calculate the
average of five numbers.
Example for Function with argument and with return value
Example 1
#include<stdio.h>
int sum(int, int);
void main()
{
int a,b,result;
printf("nGoing to calculate the sum of two
numbers:");
printf("nEnter two numbers:");
scanf("%d %d",&a,&b);
result = sum(a,b);
printf("nThe sum is : %d",result);
}
int sum(int a, int b)
{
return a+b;
}
OUTPUT :
Going to calculate
the sum of two
numbers:
Enter two
numbers:10
20
The sum is : 30
#include<stdio.h>
int even_odd(int);
void main()
{
int n,flag=0;
printf("nGoing to check whether a number is
even or odd");
printf("nEnter the number: ");
scanf("%d",&n);
flag = even_odd(n);
if(flag == 0)
{ printf("nThe number is odd");
} else
{ printf("nThe number is even");
} }
int even_odd(int n)
{
if(n%2 == 0)
{
return 1;
}
else
{ return 0;
}
}
OUTPUT :
Going to check
whether a number is
even or odd
Enter the number:
100
The number is even
Example 2: Program to check whether a number is even
or odd
C Library Functions
Library functions are the inbuilt function in C that are grouped and placed at a
common place called the library. Such functions are used to perform some specific
operations. For example, printf is a library function used to print on the console.
The library functions are created by the designers of compilers. All C standard library
functions are defined inside the different header files saved with the extension .h. We
need to include these header files in our program to make use of the library functions
defined in such header files.
For example, To use the library functions such as printf/scanf we need to include
stdio.h in our program which is a header file that contains all the library functions
regarding standard input/output.
The list of mostly used header files is given in the following table.
SN Header file Description
1 stdio.h This is a standard input/output header file. It contains all the library functions
regarding standard input/output.
2 conio.h This is a console input/output header file.
3 string.h It contains all string related library functions like gets(), puts(),etc.
4 stdlib.h This header file contains all the general library functions like malloc(), calloc(),
exit(), etc.
5 math.h This header file contains all the math operations related functions like sqrt(), pow(),
etc.
6 time.h This header file contains all the time-related functions.
7 ctype.h This header file contains all character handling functions.
8 stdarg.h Variable argument functions are defined in this header file.
9 signal.h All the signal handling functions are defined in this header file.
10 setjmp.h This file contains all the jump functions.
11 locale.h This file contains locale functions.
12 errno.h This file contains error handling functions.
13 assert.h This file contains diagnostics functions.
</thanks>

More Related Content

Similar to Presentation 2 (1).pdf

Similar to Presentation 2 (1).pdf (20)

Modular Programming in C
Modular Programming in CModular Programming in C
Modular Programming in C
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
C function
C functionC function
C function
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
 
U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).ppt
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Functions in c language
Functions in c languageFunctions in c language
Functions in c language
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Chapter 1.ppt
Chapter 1.pptChapter 1.ppt
Chapter 1.ppt
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
C function presentation
C function presentationC function presentation
C function presentation
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docxJLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
 

Recently uploaded

chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 

Recently uploaded (20)

chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 

Presentation 2 (1).pdf

  • 2. Agenda - 1. Introduction 2. Why We Use Functions? 3. Advantages of functions 4. Function aspects 5. Types of functions 6. Return value 7. Different aspects of function calling (with some examples) 8. C library functions
  • 3. Introduction In c, we can divide a large program into the basic building blocks known as function. The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the C program. In other words, we can say that the collection of functions creates a program. The function is also known as procedureor subroutinein other programming languages.
  • 4. Why We Use Functions? The first reason is reusability. Once a function is defined, it can be used over and over and over again. You can invoke the same function many times in your program, which saves you work. Imagine what programming would be like if you had to teach the computer about sines every time you needed to find the sine of an angle! You'd never get your program finished! Another aspect of reusability is that a single function can be used in several different (and separate) programs. When you need to write a new program, you can go back to your old programs, find the functions you need, and reuse those functions in your new program. You can also reuse functions that somebody else has written for you, such as the sine and cosine functions. The second reason is abstraction. In order to use a particular function you need to know the following things: The name of the function; What the function does; What arguments you must give to the function; and What kind of result the function returns. But notice: If you just want to use the function in your program, you don't have to know how it works inside! You don't have to understand anything about what goes on inside the function. It's sort of like driving a car or using a telephone. With an automobile, you don't need to understand every detail about the engine and drive train and wheels, if all you want to do is drive the car. Similarly, with a telephone, you don't have to understand everything about the phone system in order to make a call. The only time you need to know how a function works inside is when you need to write the function, or change how it works. (It's like a car again; you need to know how a car works in order to build one or fix one.) But once a function is written and working, you never need to look at its insides again. Together, these two reasons make functions extremely useful--practically essential!-for programmers who write large programs. The ability to divide a program into abstract, reusable pieces is what makes it possible to write large programs that actually work right.
  • 5. Advantages of functions in C • There are the following advantages of C functions. • By using functions, we can avoid rewriting same logic/code again and again in a program. • We can call C functions any number of times in a program and from any place in a program. • We can track a large C program easily when it is divided into multiple functions. • Reusability is the main achievement of C functions. • However, Function calling is always a overhead in a C program.
  • 6. Function Aspects There are three aspects of a C function. Function declaration A function must be declared globally in a c program to tell the compiler about the function name, function parameters, and return type. Function call Function can be called from anywhere in the program. The parameter list must not differ in function calling and function declaration. We must pass the same number of functions as it is declared in the function declaration. Function definition It contains the actual statements which are to be executed. It is the most important aspect to which the control comes when the function is called. Here, we must notice that only one value can be returned from the function. The syntax of creating function in c language is given below: SN C function aspects Syntax 1 Function declaration return_type function_name (argument list); 2 Function call function_name (argument_list) 3 Function definition return_type function_name (argument list) {function body;} return_type function_name (data_type parameter...){ //code to be executed }
  • 7. Types of Functions There are two types of functions in C programming: Library Functions: Are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc. User-defined functions: Are the functions which are created by the C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code. Functions User-Defined functions Library functions
  • 8. Return Value A C function may or may not return a value from the function. If you don't have to return any value from the function, use void for the return type. Let's see a simple example of C function that doesn't return any value from the function. Example without return value: If you want to return any value from the function, you need to use any data type such as int, long, char, etc. The return type depends on the value to be returned from the function. Let's see a simple example of C function that returns int value from the function. Example with return value: In the above example, we have to return 10 as a value, so the return type is int. If you want to return floating-point value (e.g., 10.2, 3.1, 54.5, etc), you need to use float as the return type of the method. void hello(){ printf("hello c"); } int get(){ return 10; } OUTPUT : hello c OUTPUT : 10 float get(){ return 10.2; } OUTPUT : 10.2 Now, you need to call the function, to get the value of the function.
  • 9. Different aspects of function calling A function may or may not accept any argument. It may or may not return any value. Based on these facts, There are four different aspects of function calls. • function without arguments and without return value • function without arguments and with return value • function with arguments and without return value • function with arguments and with return value
  • 10. Example for Function without argument and return value. Example 1. #include<stdio.h> void printName(); void main () { printf("Hello "); printName(); } void printName() { printf("India"); } OUTPUT : Hello India Example 2. #include<stdio.h> void sum(); void main() { printf("nGoing to calculat e the sum of two numbers: "); sum(); } void sum() { int a,b; printf("nEnter two number s"); scanf("%d %d",&a,&b); printf("The sum is %d",a+ b); } OUTPUT : Going to calculate the sum of two numbers: Enter two numbers 10 24 The sum is 34
  • 11. Example for Function without argument and with return value Example 1 #include<stdio.h> int sum(); void main() { int result; printf("nGoing to calculate the s um of two numbers:"); result = sum(); printf("%d",result); } int sum() { int a,b; printf("nEnter two numbers"); scanf("%d %d",&a,&b); return a+b; } OUTPUT : Going to calculate the sum of two numbers: Enter two numbers 10 24 The sum is 34 Example 2: program to calculate the area of the square #include<stdio.h> int sum(); void main() { printf("Going to calculate the area of t he squaren"); float area = square(); printf("The area of the square: %fn", area); } int square() { float side; printf("Enter the length of the side in meters: "); scanf("%f",&side); return side * side; } OUTPUT : Going to calculate the area of the square Enter the length of the side in meters: 10 The area of the square: 100.000000
  • 12. Example for Function with argument and without return value Example 1 #include<stdio.h> void sum(int, int); void main() { int a,b,result; printf("nGoing to calculate the sum of two numbers:"); printf("nEnter two numbers:"); scanf("%d %d",&a,&b); sum(a,b); } void sum(int a, int b) { printf("nThe sum is %d",a+b); } OUTPUT : Going to calculate the sum of two numbers: Enter two numbers 10 24 The sum is 34 #include<stdio.h> void average(int, int, int, int, int); void main() { int a,b,c,d,e; printf("nGoing to calculate the average of five numbers:"); printf("nEnter five numbers:"); scanf("%d %d %d %d %d",&a,&b,&c,&d,&e); average(a,b,c,d,e); } void average(int a, int b, int c, int d, int e) { float avg; avg = (a+b+c+d+e)/5; printf("The average of given five numbers : %f",avg); } OUTPUT : Going to calculate the average of five numbers: Enter five numbers:10 20 30 40 50 The average of given five numbers : 30.000000 Example 2: program to calculate the average of five numbers.
  • 13. Example for Function with argument and with return value Example 1 #include<stdio.h> int sum(int, int); void main() { int a,b,result; printf("nGoing to calculate the sum of two numbers:"); printf("nEnter two numbers:"); scanf("%d %d",&a,&b); result = sum(a,b); printf("nThe sum is : %d",result); } int sum(int a, int b) { return a+b; } OUTPUT : Going to calculate the sum of two numbers: Enter two numbers:10 20 The sum is : 30 #include<stdio.h> int even_odd(int); void main() { int n,flag=0; printf("nGoing to check whether a number is even or odd"); printf("nEnter the number: "); scanf("%d",&n); flag = even_odd(n); if(flag == 0) { printf("nThe number is odd"); } else { printf("nThe number is even"); } } int even_odd(int n) { if(n%2 == 0) { return 1; } else { return 0; } } OUTPUT : Going to check whether a number is even or odd Enter the number: 100 The number is even Example 2: Program to check whether a number is even or odd
  • 14. C Library Functions Library functions are the inbuilt function in C that are grouped and placed at a common place called the library. Such functions are used to perform some specific operations. For example, printf is a library function used to print on the console. The library functions are created by the designers of compilers. All C standard library functions are defined inside the different header files saved with the extension .h. We need to include these header files in our program to make use of the library functions defined in such header files. For example, To use the library functions such as printf/scanf we need to include stdio.h in our program which is a header file that contains all the library functions regarding standard input/output.
  • 15. The list of mostly used header files is given in the following table. SN Header file Description 1 stdio.h This is a standard input/output header file. It contains all the library functions regarding standard input/output. 2 conio.h This is a console input/output header file. 3 string.h It contains all string related library functions like gets(), puts(),etc. 4 stdlib.h This header file contains all the general library functions like malloc(), calloc(), exit(), etc. 5 math.h This header file contains all the math operations related functions like sqrt(), pow(), etc. 6 time.h This header file contains all the time-related functions. 7 ctype.h This header file contains all character handling functions. 8 stdarg.h Variable argument functions are defined in this header file. 9 signal.h All the signal handling functions are defined in this header file. 10 setjmp.h This file contains all the jump functions. 11 locale.h This file contains locale functions. 12 errno.h This file contains error handling functions. 13 assert.h This file contains diagnostics functions.