SlideShare a Scribd company logo
1 of 11
Functions
By Ziyad khalid
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 calculate t
he sum of two numbers:");
sum();
}
void sum()
{
int a,b;
printf("nEnter two numbers"
);
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 sum
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 the
squaren");
float area = square();
printf("The area of the square: %fn",are
a);
}
int square()
{
float side;
printf("Enter the length of the side in met
ers: ");
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
Presentation 2.pptx

More Related Content

Similar to Presentation 2.pptx

Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxvanshhans21102005
 
Functions in c++, presentation, short and sweet presentation, and details of ...
Functions in c++, presentation, short and sweet presentation, and details of ...Functions in c++, presentation, short and sweet presentation, and details of ...
Functions in c++, presentation, short and sweet presentation, and details of ...Sar
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptxmiki304759
 
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).pptManivannan837728
 
c.p function
c.p functionc.p function
c.p functiongiri5624
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxKhurramKhan173
 
Presentation1.pptx
Presentation1.pptxPresentation1.pptx
Presentation1.pptxAFANJIPHILL
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfJAVVAJI VENKATA RAO
 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programmingnmahi96
 
Function in c programming
Function in c programmingFunction in c programming
Function in c programmingSayeemAhmed8
 

Similar to Presentation 2.pptx (20)

Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptx
 
Functions in c++, presentation, short and sweet presentation, and details of ...
Functions in c++, presentation, short and sweet presentation, and details of ...Functions in c++, presentation, short and sweet presentation, and details of ...
Functions in c++, presentation, short and sweet presentation, and details of ...
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
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
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
C functions
C functionsC functions
C functions
 
c.p function
c.p functionc.p function
c.p function
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
C language function
C language functionC language function
C language function
 
Chapter 1.ppt
Chapter 1.pptChapter 1.ppt
Chapter 1.ppt
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Presentation1.pptx
Presentation1.pptxPresentation1.pptx
Presentation1.pptx
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
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
 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programming
 
Function in c
Function in cFunction in c
Function in c
 
Function in c programming
Function in c programmingFunction in c programming
Function in c programming
 
1.6 Function.pdf
1.6 Function.pdf1.6 Function.pdf
1.6 Function.pdf
 

Recently uploaded

Architecture case study India Habitat Centre, Delhi.pdf
Architecture case study India Habitat Centre, Delhi.pdfArchitecture case study India Habitat Centre, Delhi.pdf
Architecture case study India Habitat Centre, Delhi.pdfSumit Lathwal
 
PORTAFOLIO 2024_ ANASTASIYA KUDINOVA
PORTAFOLIO   2024_  ANASTASIYA  KUDINOVAPORTAFOLIO   2024_  ANASTASIYA  KUDINOVA
PORTAFOLIO 2024_ ANASTASIYA KUDINOVAAnastasiya Kudinova
 
Design principles on typography in design
Design principles on typography in designDesign principles on typography in design
Design principles on typography in designnooreen17
 
Top 10 Modern Web Design Trends for 2025
Top 10 Modern Web Design Trends for 2025Top 10 Modern Web Design Trends for 2025
Top 10 Modern Web Design Trends for 2025Rndexperts
 
办理学位证(TheAuckland证书)新西兰奥克兰大学毕业证成绩单原版一比一
办理学位证(TheAuckland证书)新西兰奥克兰大学毕业证成绩单原版一比一办理学位证(TheAuckland证书)新西兰奥克兰大学毕业证成绩单原版一比一
办理学位证(TheAuckland证书)新西兰奥克兰大学毕业证成绩单原版一比一Fi L
 
Call Girls Meghani Nagar 7397865700 Independent Call Girls
Call Girls Meghani Nagar 7397865700  Independent Call GirlsCall Girls Meghani Nagar 7397865700  Independent Call Girls
Call Girls Meghani Nagar 7397865700 Independent Call Girlsssuser7cb4ff
 
办理(UC毕业证书)查尔斯顿大学毕业证成绩单原版一比一
办理(UC毕业证书)查尔斯顿大学毕业证成绩单原版一比一办理(UC毕业证书)查尔斯顿大学毕业证成绩单原版一比一
办理(UC毕业证书)查尔斯顿大学毕业证成绩单原版一比一z xss
 
(办理学位证)埃迪斯科文大学毕业证成绩单原版一比一
(办理学位证)埃迪斯科文大学毕业证成绩单原版一比一(办理学位证)埃迪斯科文大学毕业证成绩单原版一比一
(办理学位证)埃迪斯科文大学毕业证成绩单原版一比一Fi sss
 
Passbook project document_april_21__.pdf
Passbook project document_april_21__.pdfPassbook project document_april_21__.pdf
Passbook project document_april_21__.pdfvaibhavkanaujia
 
How to Be Famous in your Field just visit our Site
How to Be Famous in your Field just visit our SiteHow to Be Famous in your Field just visit our Site
How to Be Famous in your Field just visit our Sitegalleryaagency
 
NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...
NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...
NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...Amil baba
 
How to Empower the future of UX Design with Gen AI
How to Empower the future of UX Design with Gen AIHow to Empower the future of UX Design with Gen AI
How to Empower the future of UX Design with Gen AIyuj
 
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)jennyeacort
 
办理学位证(SFU证书)西蒙菲莎大学毕业证成绩单原版一比一
办理学位证(SFU证书)西蒙菲莎大学毕业证成绩单原版一比一办理学位证(SFU证书)西蒙菲莎大学毕业证成绩单原版一比一
办理学位证(SFU证书)西蒙菲莎大学毕业证成绩单原版一比一F dds
 
Pharmaceutical Packaging for the elderly.pdf
Pharmaceutical Packaging for the elderly.pdfPharmaceutical Packaging for the elderly.pdf
Pharmaceutical Packaging for the elderly.pdfAayushChavan5
 
在线办理ohio毕业证俄亥俄大学毕业证成绩单留信学历认证
在线办理ohio毕业证俄亥俄大学毕业证成绩单留信学历认证在线办理ohio毕业证俄亥俄大学毕业证成绩单留信学历认证
在线办理ohio毕业证俄亥俄大学毕业证成绩单留信学历认证nhjeo1gg
 
Top 10 Modern Web Design Trends for 2025
Top 10 Modern Web Design Trends for 2025Top 10 Modern Web Design Trends for 2025
Top 10 Modern Web Design Trends for 2025Rndexperts
 
Cosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable BricksCosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable Bricksabhishekparmar618
 
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Call In girls Bhikaji Cama Place 🔝 ⇛8377877756 FULL Enjoy Delhi NCR
Call In girls Bhikaji Cama Place 🔝 ⇛8377877756 FULL Enjoy Delhi NCRCall In girls Bhikaji Cama Place 🔝 ⇛8377877756 FULL Enjoy Delhi NCR
Call In girls Bhikaji Cama Place 🔝 ⇛8377877756 FULL Enjoy Delhi NCRdollysharma2066
 

Recently uploaded (20)

Architecture case study India Habitat Centre, Delhi.pdf
Architecture case study India Habitat Centre, Delhi.pdfArchitecture case study India Habitat Centre, Delhi.pdf
Architecture case study India Habitat Centre, Delhi.pdf
 
PORTAFOLIO 2024_ ANASTASIYA KUDINOVA
PORTAFOLIO   2024_  ANASTASIYA  KUDINOVAPORTAFOLIO   2024_  ANASTASIYA  KUDINOVA
PORTAFOLIO 2024_ ANASTASIYA KUDINOVA
 
Design principles on typography in design
Design principles on typography in designDesign principles on typography in design
Design principles on typography in design
 
Top 10 Modern Web Design Trends for 2025
Top 10 Modern Web Design Trends for 2025Top 10 Modern Web Design Trends for 2025
Top 10 Modern Web Design Trends for 2025
 
办理学位证(TheAuckland证书)新西兰奥克兰大学毕业证成绩单原版一比一
办理学位证(TheAuckland证书)新西兰奥克兰大学毕业证成绩单原版一比一办理学位证(TheAuckland证书)新西兰奥克兰大学毕业证成绩单原版一比一
办理学位证(TheAuckland证书)新西兰奥克兰大学毕业证成绩单原版一比一
 
Call Girls Meghani Nagar 7397865700 Independent Call Girls
Call Girls Meghani Nagar 7397865700  Independent Call GirlsCall Girls Meghani Nagar 7397865700  Independent Call Girls
Call Girls Meghani Nagar 7397865700 Independent Call Girls
 
办理(UC毕业证书)查尔斯顿大学毕业证成绩单原版一比一
办理(UC毕业证书)查尔斯顿大学毕业证成绩单原版一比一办理(UC毕业证书)查尔斯顿大学毕业证成绩单原版一比一
办理(UC毕业证书)查尔斯顿大学毕业证成绩单原版一比一
 
(办理学位证)埃迪斯科文大学毕业证成绩单原版一比一
(办理学位证)埃迪斯科文大学毕业证成绩单原版一比一(办理学位证)埃迪斯科文大学毕业证成绩单原版一比一
(办理学位证)埃迪斯科文大学毕业证成绩单原版一比一
 
Passbook project document_april_21__.pdf
Passbook project document_april_21__.pdfPassbook project document_april_21__.pdf
Passbook project document_april_21__.pdf
 
How to Be Famous in your Field just visit our Site
How to Be Famous in your Field just visit our SiteHow to Be Famous in your Field just visit our Site
How to Be Famous in your Field just visit our Site
 
NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...
NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...
NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...
 
How to Empower the future of UX Design with Gen AI
How to Empower the future of UX Design with Gen AIHow to Empower the future of UX Design with Gen AI
How to Empower the future of UX Design with Gen AI
 
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
 
办理学位证(SFU证书)西蒙菲莎大学毕业证成绩单原版一比一
办理学位证(SFU证书)西蒙菲莎大学毕业证成绩单原版一比一办理学位证(SFU证书)西蒙菲莎大学毕业证成绩单原版一比一
办理学位证(SFU证书)西蒙菲莎大学毕业证成绩单原版一比一
 
Pharmaceutical Packaging for the elderly.pdf
Pharmaceutical Packaging for the elderly.pdfPharmaceutical Packaging for the elderly.pdf
Pharmaceutical Packaging for the elderly.pdf
 
在线办理ohio毕业证俄亥俄大学毕业证成绩单留信学历认证
在线办理ohio毕业证俄亥俄大学毕业证成绩单留信学历认证在线办理ohio毕业证俄亥俄大学毕业证成绩单留信学历认证
在线办理ohio毕业证俄亥俄大学毕业证成绩单留信学历认证
 
Top 10 Modern Web Design Trends for 2025
Top 10 Modern Web Design Trends for 2025Top 10 Modern Web Design Trends for 2025
Top 10 Modern Web Design Trends for 2025
 
Cosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable BricksCosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable Bricks
 
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
 
Call In girls Bhikaji Cama Place 🔝 ⇛8377877756 FULL Enjoy Delhi NCR
Call In girls Bhikaji Cama Place 🔝 ⇛8377877756 FULL Enjoy Delhi NCRCall In girls Bhikaji Cama Place 🔝 ⇛8377877756 FULL Enjoy Delhi NCR
Call In girls Bhikaji Cama Place 🔝 ⇛8377877756 FULL Enjoy Delhi NCR
 

Presentation 2.pptx

  • 2. 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.
  • 3. 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.
  • 4. 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.
  • 5. 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 }
  • 6. 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
  • 7. 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.
  • 8. 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
  • 9. 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 calculate t he sum of two numbers:"); sum(); } void sum() { int a,b; printf("nEnter two numbers" ); 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
  • 10. 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 sum 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 the squaren"); float area = square(); printf("The area of the square: %fn",are a); } int square() { float side; printf("Enter the length of the side in met ers: "); 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