Note: This slide was created by me. I am Md. Touhidul Islam Shawan. Here in these slide I have written about some basic points of function of c program and how the function works.
Touhidul ShawanStudent at Daffodil International University um Daffodil International University-DIU
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
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.