CH 6 Functions
CH 6 Functions
Sujan Karki
Email: [email protected]
Contact No.: 9819387234
Master’s in Information System Engineering (MscIne) *Ongoing
Bachelor in Computer Engineering – Purwanchal Campus,IOE
UNIT 6
FUNCTIONS
2
Functions
⚫ A function is a self contained sub program that is
meant to be some specific, well defined task.
⚫ A function is a group of statements that together
perform a task.
3
Why Functions?
⚫ Functions increases code reusability by avoiding
rewriting of same code over and over.
⚫ Program development will be faster.
⚫ Program debugging will be easier.
⚫ Function reduces program complexity.
⚫ Easier to understand logic involved in the program.
⚫ Recursive call is possible through function.
⚫ Easy to divide the work among programmers.
⚫ If a program is divided into multiple functions, then
each function can be independently developed. So
program development will be easier.
4
Types of functions
⚫ Library Functions
Library functions are supplied with every C compiler.
The source code of the library functions is not given to
the user. These functions are precompiled and the user
gets only the object code. This object code is linked to
the object code of your program by the linker.
Some examples are
printf(), scanf() are defined in header file stdio.h
getch(), clrscr() are defined in header file conio.h
strlen(), strupr() are defined in header file string.h
pow(), sqrt() are defined in header file math.h
5
…
To use library function in our program we should know-
i. Name of the function and its purpose
ii. Type and number of arguments it accepts
iii. Type of the value it returns
iv. Name of the header file to be included
6
Types of functions
⚫ User Defined Functions
Users can create their own functions for performing any
specific task of the program. These types of
functions are called user defined functions.
To create and use these functions, we should know about
these three things-
1. Function definition
2. Function declaration
3. Function call
7
Function definition
The function definition consists of the whole description and
code of a function. It tells what the function is doing and
that are its inputs and outputs. A function definition consists
of two parts - a function header and a function body.
Syntax:
return_type func_name(type1 arg1, type2 arg2, ........)
{
local variables declarations;
statements;
..............
return(expression);
}
8
Function declaration/ prototype
The calling function needs information about the called
function. If definition of the called function is placed before
the calling function, then declaration is not needed.
E.g.
Add ( a, b, c);
Multiply( x, y);
10
Return statement
The return statement is used in a function to return a value
to the calling function.
Syntax:
return;
or
return (expression);
Note:
The first form of return statement is used to terminate the
function without returning any value.
The second form of return statement is used to terminate a
function and return a value to the calling function.
11
Function arguments
The calling function sends some values to the called function
for communication; these values are called arguments or
parameters.
• Actual argument
The arguments which are mentioned in the function call are
known as actual arguments, since these are the values which
are actually sent to the called function.
• Formal argument
The name of the arguments, which are mentioned in the
function definition are called formal or
dummy arguments since they are used just to hold the values
that are sent by the calling function.
12
#include <stdio.h> #include <stdio.h>
int area (int a, int b); #include <stdio.h> int area (int , int );
void main() int area(int l, int b) { void main()
{ actual arguments int a; {
int x, y, z; a= ( l*b ); int x, y, z;
x = 5; return a; x = 5;
y = 5; } y = 5;
z = area(x, y); void main() z = area(x, y);
printf("%d", z); { printf("%d", z);
} Formal argumentsint x, y, z; }
x = 5;
int area(int l, int b) { y = 5; int area(int l, int b) {
int a; z = area(x, y); int a;
a= ( l*b ); printf("%d", z); a= ( l*b );
return a; } return a;
} }
13
#include <stdio.h> #include <stdio.h>
#include <stdio.h> int area ();
void area (int a, int b);int area(int l, int b) { void main()
void main() return ( l*b );
{ {
} int z;
int x, y, z; void main()
x = 5; z = area();
{ printf("%d", z);
y = 5; int x, y, z;
area(x, y); }
x = 5;
} y = 5; int area() {
z = area(x, y); int x, y, a;
void area(int l, int b) { printf("%d", z);
int a; x = 5;
} y = 5;
a= ( l*b );
printf("%d", z); a= ( x*y );
} return a;
}
14
Category of functions according
to the return values and
arguments
The functions can be classified into four categories on
the basis of the arguments and return value;
15
⚫ Example: Function with no arguments and no return value.
Program to add two numbers.
#include<stdio.h>
void sum();
void main() {
sum();
}
void sum() {
int x, y, s;
printf("Enter two numbers\n");
scanf("%d%d",&x,&y);
s=x+y;
printf("Sum =%d", s);
}
16
⚫ Example: Function with no argument and a return value.
Program to add two numbers.
#include<stdio.h>
int sum();
void main() {
int c;
c= sum();
printf("sum=%d", c);
}
int sum() {
int x, y, s;
printf("Enter two numbers\n");
scanf("%d%d",&x,&y);
s=x+y;
17 }
⚫ Example: Function with argument and no return value.
Program to add two numbers.
#include<stdio.h>
void sum(int, int);
void main() {
int a, b;
printf("Enter two numbers\n");
scanf("%d %d", &a, &b);
sum(a, b);
}
void sum(int x, int y) {
int s;
s=x+y;
printf("Sum =%d", s);
18 }
⚫ Example: Function with argument and return value.
Program to add two numbers.
#include<stdio.h>
int sum(int, int);
void main() {
int a, b, c;
printf("Enter two numbers\n");
scanf("%d %d", &a, &b);
c=sum(a, b);
printf("Sum =%d", c);
}
int sum(int x, int y) {
int s;
s=x+y;
return s;
19
}
Difference between library function
and user defined function
Library function User defined function
The program using library function will The program using user defined
be usually short as the programmer function will be usually lengthy as the
doesn’t have to define the function. programmer has to define the function.
20
⚫ Local variables
The variables that are defined within the body of a function or
a block, are local variables.
#include<stdio.h>
int sum(int, int);
void main() {
int a, b, c;
printf("Enter two numbers\n");
scanf("%d %d", &a, &b);
c=sum(a, b);
printf("Sum =%d", c);
}
int sum(int x, int y) { Here, s is the local variable
int s;
s=x+y; Note:
return s;
} Local variable initial value is
garbage value.
21
⚫ Global variables
The variables that are defined outside any function are called
global variables. All functions in the program can access and
modify global variables. It is useful to declare a variable
global if it is to be used by many functions in the program.
Global variables are automatically initialized to
0 at the time of declaration.
#include<stdio.h> Here, a is the global variable
int a=10;
void fun( ) {
a=20;
printf(“%d”, a);
}
void main() { Note:
printf(“%d“, a); Global variable initial value is 0.
fun( );
printf(“%d“, a);
2 }
2
Note:
⚫ Static variables Static variable initial value is 0.
2
5
⚫ Recursion
Recursion is a powerful technique of writing a complicated
algorithm in an easy way.
27
⚫ Example: Write a program to
calculate factorial of any given number using recursive function.
#include<stdio.h>
int sum(int);
void main( ) {
int n,s;
printf("Input a number\n");
scanf("%d", &n);
s=sum(n);
printf("Sum of natural numbers=%d", s);
}
28
int sum(int n)
. . . to be continued !!!
29