C function presentation

Touhidul Shawan
Touhidul ShawanStudent at Daffodil International University um Daffodil International University-DIU
FUNCTION
1
FUNCTION
What is function?
How the function works?
What is the advantage of using function?
How to call C functions in a program?
And classification of function.
2
FUNCTION
A function is a group of statements that together
perform a task. Every C program has at least one
function, which is main ()
3
ADVANTAGE OF USING FUNCTION
 It is much easier to write a structured program where a large program can be
divided into a smaller, simpler task.
 Allowing the code to be called many times
 Easier to read and update
 It is easier to debug a structured program where there error is easy to find and
fix
4
CLASSIFICATION OF
FUNCTION
There are two types of function:-
User defined function [ main () ]
Library function [ printf(), scanf(), getchar(), sqrt() ]
5
USER DEFINED FUNCTION
return_type function_name (parameters)
{
function_boady
return value
}
6
7
return_type: When
function finished its
work then what
kind of data it will
return
function_name:
Write a meaningful
name.
Parameters: For
function, here will
be enough data to
work properly.
function_body:
How the function will
work and what kind
of work it will do.
return_value: what
kind of data it will
return
EXAMPLE OF USER DEFINED
FUNCTION
#include<stdio.h>
int add(double num1, double num2)
{
double sum = num1+num2;
return sum;
}
int main ()
{
double a, b, c;
a = b = 2.5;
c = add(a, b);
printf(“%lfn”, c);
return 0 ;
} 8
int
called
return_type
add
function
name
num1, num2
Parameteres
sum =
num1+num2
sum
return_value
9
USER DEFINED FUNCTION
#include<stdio.h>
int main ()
{
double a, b, c;
a = b = 2.5;
c = add(a, b);
printf(“%lfn”, c);
return 0 ;
}
int add(double num1, double num2)
{
double sum = num1+num2;
return sum;
} 10
If we now write add function after main function then
what will happen. If we write add function after main
function then program will not be executed. Compiler
will show error “add was not declared in this scope.” So,
what can we do now to execute this program.
Note: Don`t put this box in slide. It`s only for your
understanding
USER DEFINED FUNCTION
#include<stdio.h>
int add(double num1, double num2) ;
int main ()
{
double a, b, c;
a = b = 2.5;
c = add(a, b);
printf(“%lfn”, c);
return 0 ;
}
int add(double num1, double num2)
{
double sum = num1+num2;
return sum;
}
11
We can write a line before the main function.
The line is int add (double num1, double
num2); Now the program will run. This line is
called prototype. Here it is the prototype of
the add function.
Note: Don`t put this box in slide. It`s only for
your understanding
USER DEFINED FUNCTION
#include<stdio.h>
int test_function(int x)
{
int y = x;
x = 2 * y;
return (x*y);
}
int main()
{
int x =10, y = 20, z = 30;
z = test_function(x);
printf("%d %d %dn“, x, y, z);
return 0;
}
12
Can you tell me what is the output of this program? If I tell
you the output is 20 10 200 (that`s mean x = 20, y = 10, z =
200). Do you agree with me?
Because we change the value of x and y in test_function.
First, we send the value of x = 10 like parameter then we
set up this value in y that`s mean y = 10. After that we set
the value of x in 2*y that`s mean 20. Then we return x*y (
that`s value is 20*10 = 200). So, we get the value of z is
200.
Note: Don`t put this box in slide. It`s only for your
understanding
USER DEFINED FUNCTION
13
But when we run
this program, the
output is 10 20 200
(x = 10 y = 20 z =
200)
Note: Don`t put
this box in slide. It`s
only for your
understanding
• Now we can ask what is the reason of this kind of output, there is no
confusion about the value of z. There is confusion about the value of x
and y. We change the value of x and y in test_function but it does not
effect in the value of x and y in main function. Because the value of
every function is different. It`s call local variable. We have printed the
value of x and y of main function but we do not print the value of x and
y of test_function. That`s mean one function`s variable does not exist
in another function. If we want to exist that variable in every function
of whole program, we can declare that variable as a global variable. We
declare the global variable before write the prototype.
• Note: Don`t put this slide in slide. This text only for your
understanding
14
USER DEFINED FUNCTION
#include<stdio.h>
double pi = 3.14;
void my_function()
{
pi = 3.1416; /*Here we change the value of pi */
return; /* If the return type of function is void then this return not necessary */
}
int main()
{
printf(“%lfn”,pi); /* Here the value of pi is 3.14 */
my_function();
printf(“%lfn”,pi); /* Here the value of pi is 3.1416 because we change the value
in my_function */
return 0;
} 15
Here pi = 3.14 is
the global
variable. If we
declared a
variable in
my_function
(double pi) that’s
called a local
variable and value
of global variable
pi did not change
Note: Don`t put
this box in slide.
It`s only for your
understanding
USER DEFINED FUNCTION
16
17
• Studying: B.Sc. In Computer Science
and Engineering
• Institute: Daffodil International
University
• FB:
www.facebook.com/touhidulshaon
• Note: This slide was created by Md.
Touhidul Islam Shawan. Here in this slide I
have written about some basic points of
function of c program and how the
function works.
1 von 17

Recomendados

Function in c program von
Function in c programFunction in c program
Function in c programumesh patil
1.3K views23 Folien
Function in c von
Function in cFunction in c
Function in cRaj Tandukar
6.8K views22 Folien
Function in C von
Function in CFunction in C
Function in CDr. Abhineet Anand
10.2K views31 Folien
Function in c von
Function in cFunction in c
Function in csavitamhaske
1.3K views18 Folien
C functions von
C functionsC functions
C functionsUniversity of Potsdam
9.9K views32 Folien
Function in C Language von
Function in C Language Function in C Language
Function in C Language programmings guru
3.9K views8 Folien

Más contenido relacionado

Was ist angesagt?

Functions in C von
Functions in CFunctions in C
Functions in CShobhit Upadhyay
4.1K views14 Folien
Function in C program von
Function in C programFunction in C program
Function in C programNurul Zakiah Zamri Tan
70.4K views23 Folien
Function von
FunctionFunction
Functionjayesh30sikchi
1.3K views22 Folien
Function lecture von
Function lectureFunction lecture
Function lectureDIT University, Dehradun
1.4K views54 Folien
Functions in C von
Functions in CFunctions in C
Functions in CKamal Acharya
25.2K views22 Folien
INLINE FUNCTION IN C++ von
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++Vraj Patel
5.9K views13 Folien

Was ist angesagt?(20)

INLINE FUNCTION IN C++ von Vraj Patel
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
Vraj Patel5.9K views
Pointers,virtual functions and polymorphism cpp von rajshreemuthiah
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah2.4K views
Presentation on Function in C Programming von Shuvongkor Barman
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman14.4K views
What are variables and keywords in c++ von Abdul Hafeez
What are variables and keywords in c++What are variables and keywords in c++
What are variables and keywords in c++
Abdul Hafeez632 views
RECURSION IN C von v_jk
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk14K views
basics of C and c++ by eteaching von eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteaching
eteaching2.4K views
Functions in c language von tanmaymodi4
Functions in c language Functions in c language
Functions in c language
tanmaymodi42.6K views
Chapter 4 : Balagurusamy Programming ANSI in C von BUBT
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in C
BUBT7.4K views
Call by value von Dharani G
Call by valueCall by value
Call by value
Dharani G8.6K views
Structure of C++ - R.D.Sivakumar von Sivakumar R D .
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
Sivakumar R D .4.3K views

Similar a C function presentation

Function von
FunctionFunction
FunctionRajat Patel
585 views20 Folien
Presentation 2 (1).pdf von
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdfziyadaslanbey
10 views16 Folien
Functions-Computer programming von
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programmingnmahi96
80 views19 Folien
Functions von
Functions Functions
Functions Dr.Subha Krishna
168 views25 Folien
Unit-III.pptx von
Unit-III.pptxUnit-III.pptx
Unit-III.pptxMehul Desai
3 views61 Folien
unit3 part2 pcds function notes.pdf von
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfJAVVAJI VENKATA RAO
7 views37 Folien

Similar a C function presentation(20)

Functions-Computer programming von nmahi96
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programming
nmahi9680 views
CH.4FUNCTIONS IN C_FYBSC(CS).pptx von SangeetaBorde3
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde329 views
c.p function von giri5624
c.p functionc.p function
c.p function
giri56241.4K views
Functions assignment von Ahmad Kamal
Functions assignmentFunctions assignment
Functions assignment
Ahmad Kamal2.4K views
C programming language working with functions 1 von Jeevan Raj
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1
Jeevan Raj150 views

Más de Touhidul Shawan

Memory management von
Memory managementMemory management
Memory managementTouhidul Shawan
550 views15 Folien
LINEAR DIFFERENTIAL EQUATION & BERNOULLI`S EQUATION von
LINEAR DIFFERENTIAL EQUATION & BERNOULLI`S EQUATIONLINEAR DIFFERENTIAL EQUATION & BERNOULLI`S EQUATION
LINEAR DIFFERENTIAL EQUATION & BERNOULLI`S EQUATIONTouhidul Shawan
6.4K views10 Folien
Propositional logic by shawan von
Propositional logic by shawanPropositional logic by shawan
Propositional logic by shawanTouhidul Shawan
492 views15 Folien
Atomic model and nuclear reaction von
Atomic model and nuclear reactionAtomic model and nuclear reaction
Atomic model and nuclear reactionTouhidul Shawan
348 views21 Folien
Math presentation on domain and range von
Math presentation on domain and rangeMath presentation on domain and range
Math presentation on domain and rangeTouhidul Shawan
4.9K views23 Folien
Presentation on lights [ physics ] von
Presentation on lights [ physics ]Presentation on lights [ physics ]
Presentation on lights [ physics ]Touhidul Shawan
4.2K views32 Folien

Más de Touhidul Shawan(6)

LINEAR DIFFERENTIAL EQUATION & BERNOULLI`S EQUATION von Touhidul Shawan
LINEAR DIFFERENTIAL EQUATION & BERNOULLI`S EQUATIONLINEAR DIFFERENTIAL EQUATION & BERNOULLI`S EQUATION
LINEAR DIFFERENTIAL EQUATION & BERNOULLI`S EQUATION
Touhidul Shawan6.4K views
Math presentation on domain and range von Touhidul Shawan
Math presentation on domain and rangeMath presentation on domain and range
Math presentation on domain and range
Touhidul Shawan4.9K views
Presentation on lights [ physics ] von Touhidul Shawan
Presentation on lights [ physics ]Presentation on lights [ physics ]
Presentation on lights [ physics ]
Touhidul Shawan4.2K views

Último

The Open Access Community Framework (OACF) 2023 (1).pptx von
The Open Access Community Framework (OACF) 2023 (1).pptxThe Open Access Community Framework (OACF) 2023 (1).pptx
The Open Access Community Framework (OACF) 2023 (1).pptxJisc
85 views7 Folien
ACTIVITY BOOK key water sports.pptx von
ACTIVITY BOOK key water sports.pptxACTIVITY BOOK key water sports.pptx
ACTIVITY BOOK key water sports.pptxMar Caston Palacio
430 views4 Folien
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively von
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks EffectivelyISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks EffectivelyPECB
545 views18 Folien
Plastic waste.pdf von
Plastic waste.pdfPlastic waste.pdf
Plastic waste.pdfalqaseedae
125 views5 Folien
UWP OA Week Presentation (1).pptx von
UWP OA Week Presentation (1).pptxUWP OA Week Presentation (1).pptx
UWP OA Week Presentation (1).pptxJisc
74 views11 Folien

Último(20)

The Open Access Community Framework (OACF) 2023 (1).pptx von Jisc
The Open Access Community Framework (OACF) 2023 (1).pptxThe Open Access Community Framework (OACF) 2023 (1).pptx
The Open Access Community Framework (OACF) 2023 (1).pptx
Jisc85 views
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively von PECB
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks EffectivelyISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
PECB 545 views
Plastic waste.pdf von alqaseedae
Plastic waste.pdfPlastic waste.pdf
Plastic waste.pdf
alqaseedae125 views
UWP OA Week Presentation (1).pptx von Jisc
UWP OA Week Presentation (1).pptxUWP OA Week Presentation (1).pptx
UWP OA Week Presentation (1).pptx
Jisc74 views
The basics - information, data, technology and systems.pdf von JonathanCovena1
The basics - information, data, technology and systems.pdfThe basics - information, data, technology and systems.pdf
The basics - information, data, technology and systems.pdf
JonathanCovena188 views
Drama KS5 Breakdown von WestHatch
Drama KS5 BreakdownDrama KS5 Breakdown
Drama KS5 Breakdown
WestHatch71 views
American Psychological Association 7th Edition.pptx von SamiullahAfridi4
American Psychological Association  7th Edition.pptxAmerican Psychological Association  7th Edition.pptx
American Psychological Association 7th Edition.pptx
SamiullahAfridi482 views
Class 10 English notes 23-24.pptx von TARIQ KHAN
Class 10 English notes 23-24.pptxClass 10 English notes 23-24.pptx
Class 10 English notes 23-24.pptx
TARIQ KHAN107 views
Education and Diversity.pptx von DrHafizKosar
Education and Diversity.pptxEducation and Diversity.pptx
Education and Diversity.pptx
DrHafizKosar118 views
Community-led Open Access Publishing webinar.pptx von Jisc
Community-led Open Access Publishing webinar.pptxCommunity-led Open Access Publishing webinar.pptx
Community-led Open Access Publishing webinar.pptx
Jisc74 views
OEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptx von Inge de Waard
OEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptxOEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptx
OEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptx
Inge de Waard167 views

C function presentation

  • 2. FUNCTION What is function? How the function works? What is the advantage of using function? How to call C functions in a program? And classification of function. 2
  • 3. FUNCTION A function is a group of statements that together perform a task. Every C program has at least one function, which is main () 3
  • 4. ADVANTAGE OF USING FUNCTION  It is much easier to write a structured program where a large program can be divided into a smaller, simpler task.  Allowing the code to be called many times  Easier to read and update  It is easier to debug a structured program where there error is easy to find and fix 4
  • 5. CLASSIFICATION OF FUNCTION There are two types of function:- User defined function [ main () ] Library function [ printf(), scanf(), getchar(), sqrt() ] 5
  • 6. USER DEFINED FUNCTION return_type function_name (parameters) { function_boady return value } 6
  • 7. 7 return_type: When function finished its work then what kind of data it will return function_name: Write a meaningful name. Parameters: For function, here will be enough data to work properly. function_body: How the function will work and what kind of work it will do. return_value: what kind of data it will return
  • 8. EXAMPLE OF USER DEFINED FUNCTION #include<stdio.h> int add(double num1, double num2) { double sum = num1+num2; return sum; } int main () { double a, b, c; a = b = 2.5; c = add(a, b); printf(“%lfn”, c); return 0 ; } 8
  • 10. USER DEFINED FUNCTION #include<stdio.h> int main () { double a, b, c; a = b = 2.5; c = add(a, b); printf(“%lfn”, c); return 0 ; } int add(double num1, double num2) { double sum = num1+num2; return sum; } 10 If we now write add function after main function then what will happen. If we write add function after main function then program will not be executed. Compiler will show error “add was not declared in this scope.” So, what can we do now to execute this program. Note: Don`t put this box in slide. It`s only for your understanding
  • 11. USER DEFINED FUNCTION #include<stdio.h> int add(double num1, double num2) ; int main () { double a, b, c; a = b = 2.5; c = add(a, b); printf(“%lfn”, c); return 0 ; } int add(double num1, double num2) { double sum = num1+num2; return sum; } 11 We can write a line before the main function. The line is int add (double num1, double num2); Now the program will run. This line is called prototype. Here it is the prototype of the add function. Note: Don`t put this box in slide. It`s only for your understanding
  • 12. USER DEFINED FUNCTION #include<stdio.h> int test_function(int x) { int y = x; x = 2 * y; return (x*y); } int main() { int x =10, y = 20, z = 30; z = test_function(x); printf("%d %d %dn“, x, y, z); return 0; } 12 Can you tell me what is the output of this program? If I tell you the output is 20 10 200 (that`s mean x = 20, y = 10, z = 200). Do you agree with me? Because we change the value of x and y in test_function. First, we send the value of x = 10 like parameter then we set up this value in y that`s mean y = 10. After that we set the value of x in 2*y that`s mean 20. Then we return x*y ( that`s value is 20*10 = 200). So, we get the value of z is 200. Note: Don`t put this box in slide. It`s only for your understanding
  • 13. USER DEFINED FUNCTION 13 But when we run this program, the output is 10 20 200 (x = 10 y = 20 z = 200) Note: Don`t put this box in slide. It`s only for your understanding
  • 14. • Now we can ask what is the reason of this kind of output, there is no confusion about the value of z. There is confusion about the value of x and y. We change the value of x and y in test_function but it does not effect in the value of x and y in main function. Because the value of every function is different. It`s call local variable. We have printed the value of x and y of main function but we do not print the value of x and y of test_function. That`s mean one function`s variable does not exist in another function. If we want to exist that variable in every function of whole program, we can declare that variable as a global variable. We declare the global variable before write the prototype. • Note: Don`t put this slide in slide. This text only for your understanding 14
  • 15. USER DEFINED FUNCTION #include<stdio.h> double pi = 3.14; void my_function() { pi = 3.1416; /*Here we change the value of pi */ return; /* If the return type of function is void then this return not necessary */ } int main() { printf(“%lfn”,pi); /* Here the value of pi is 3.14 */ my_function(); printf(“%lfn”,pi); /* Here the value of pi is 3.1416 because we change the value in my_function */ return 0; } 15 Here pi = 3.14 is the global variable. If we declared a variable in my_function (double pi) that’s called a local variable and value of global variable pi did not change Note: Don`t put this box in slide. It`s only for your understanding
  • 17. 17 • Studying: B.Sc. In Computer Science and Engineering • Institute: Daffodil International University • FB: www.facebook.com/touhidulshaon • Note: This slide was created by Md. Touhidul Islam Shawan. Here in this slide I have written about some basic points of function of c program and how the function works.