0% found this document useful (0 votes)
19 views

CH 6 Functions

Uploaded by

Mausam Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

CH 6 Functions

Uploaded by

Mausam Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

C- Programming

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.

⚫ You can divide up your code into separate functions.


⚫ A C-program consists of one or more functions. C
program has at least one function, which is main(),
and all the additional functions are defined within it.

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

We can define any function of our own with the same


name as that of any function in the C
library. If we do so then the function that we have
defined will take precedence over the library
function with the same name.

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.

It informs the compiler about the following three things-


1. Name of the function
2. Number and type of arguments received by the function
3. Type of value returned by the function
Syntax:
return_type func_name(type1, type2,........);
e.g.
int add (int, int);
int add (int a, int b);
9
Function call/ Accessing a function
A function is called by simply writing its name followed by
the argument list inside the Parentheses and separated by
commas.
Syntax:
func_name(arg1, arg2, arg3,....);

These arguments arg1,arg2,arg3,...are called actual


arguments.

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;

1. Function with no arguments and no return value.


2. Function with no argument and return value.
3. Function with argument and no return value.
4. Function with argument 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

Library function is a predefined function User defined function is not a


in a header file or preprocessor predefined function, it is defined by the
directive. programmer according to the need.
Programmer can simply use this Programmer has to declare, define and
function by including respective header call this function by themself.
file.
Program development time will be Program development time will be
faster. usually slower.
Program will be simple. Program will be complex.

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.

This is another class of local variable. A static variable can


only be accessed from the function in which it was declared,
like a local variable. The static variable is not destroyed on
exit from the function; instead its value is preserved and
become available again when the function is called next time.
#include<stdio.h> #include<stdio.h>
void increment( ) { void increment( ) {
int i=1; static int i=1;
printf(“%d”, i); printf(“%d”, i);
i++; Output: i++; Output:
} 1 } 1
void main() { 1 void main() { 2
increment( ); 1 increment( ); 3
increment( ); 1 increment( ); 4
increment( ); increment( );
increment( ); increment( );
2 } }
3
⚫ Passing by value (call by value):
In pass by value, values of variables are passed to the function
from the calling function. This method copies the value of actual
parameters into formal parameters. In other words, when the
function is called, a separate copy of the variables is created in the
memory and the value of original variables is given to these
variables. So if any changes are made in the value (of the called
function) is not reflected to the original variable (of calling
function). It can return only one value.
void change(int);
void main( ) {
int a=15;
printf("Before calling function, a=%d", a);
change(a);
printf("After calling function, a=%d", a);
}
void change(int x) {
x=x+5;
2 }
4
⚫ Passing by reference (call by reference):
In passing by reference we pass the address or location of a
variable to the function during function call. Pointers are used to
call a function by reference. When a function is called by
reference, then the formal argument becomes reference to the
actual argument. This means that the called function doesn’t create
its own copy of values rather, it refers to the original values
only by reference name. Thus function works with original data and
the changes are made in the original data itself. It can return more
than one value at a time.
#include<stdio.h>
void change(int*);
void main() {
int a=15;
printf("Before calling function, a=%d", a);
change(&a);

2
5
⚫ Recursion
Recursion is a powerful technique of writing a complicated
algorithm in an easy way.

Recursive function is the one that calls to itself to solve a


smaller version of its task until a final call which doesn’t
require a self call.

A function will be recursive, if it contain following features:


i. Function should call itself.
ii. Function should have a stopping condition (base criteria)
and every time the function calls itself it must be closer to
base criteria.
2
6
⚫ Example: Write a program to calculate factorial of any given num
recursive function.
#include<stdio.h>
long int factorial(int);
void main() {
int num, fact;
printf("Enter a number\n");
scanf("%d", &num);
fact=factorial(num);
printf("Factorial of %d = %d", num, fact);
}

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

You might also like