0% found this document useful (0 votes)
84 views25 pages

Unit 4

The document provides an overview of functions in C programming, explaining their definition, advantages, types, and structure. It details the process of creating user-defined functions, including function prototypes, definitions, and calls, as well as the concepts of call by value and call by reference. Additionally, it discusses storage classes and their impact on variable scope and lifetime, along with examples of function usage.

Uploaded by

anjalivp1704
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views25 pages

Unit 4

The document provides an overview of functions in C programming, explaining their definition, advantages, types, and structure. It details the process of creating user-defined functions, including function prototypes, definitions, and calls, as well as the concepts of call by value and call by reference. Additionally, it discusses storage classes and their impact on variable scope and lifetime, along with examples of function usage.

Uploaded by

anjalivp1704
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

UNIT-4 FUNCTIONS

1
What is Function?
▪ A function is a group of statements that perform a specific task.
▪ It divides a large program into smaller parts.
▪ Function is a group of statements as a single unit known by some
name which is performing some well defined task.
▪ Every C program can be thought of as a collection of these
functions.
▪ Program execution in C language starts from the main function.

void main()
{
// body part
▪ Why function ? }
• Avoids rewriting the same code over and over.
• Using functions it becomes easier to write programs and keep track of what
they doing
Advantages of Functions
▪ It facilitates top-down modular programming, whereby the large
problem is divided into small parts.
▪ It reduces the size of code, if that code is repetitively used in
program.
▪ Function can be used in other program also, so labor is reduced.
▪ Function can call itself, which is called as recursion, it reduces the
coding, some problems are recursive in nature.
▪ It allows team-work for large project, individual members of team
has to concentrate on the function given to them and not on the
whole complex problem.
Types of Function

Function

Library Function User Defined Function (UDF)

Predefined or inbuilt Created by User


Declarations inside header files Programmer need to
Eg. printf() – stdio.h declare it
pow() – math.h Eg. findSimpleInterest()
strcmp() – string.h areaOfCircle()
Program Structure for Function
▪ When we use a user-defined function program structure is divided
into three parts.

void func1(); Function Prototype

void main()
{
....
func1(); Function call
}

void func1()
{
.... Function definition
//function body
....
}
Function Prototype
▪ A function Prototype also know as function declaration.
▪ A function declaration tells the compiler about a function name
and how to call the function.
▪ It defines the function before it is being used or called.
▪ A function prototype needs to be written at the beginning of the
program.

Syntax
return-type function-name (arg-1, arg 2, …);

Example
void addition(int, int);
Function Definition
▪ A function definition defines the functions header and body.
▪ A function header part should be identical to the function
prototype.
• Function return type
• Function name
• List of parameters
▪ A function body part defines function logic.
• Function statements

Syntax
return-type function-name (arg-1, arg 2,
…) Example
{
void addition(int x, int y)
//... Function body
{
}
printf("Addition is=%d“,(x+y));
}
Function call
▪ While creating a C function, you give a definition of what the
function has to do. To use a function, you will have to call that
function to perform the defined task.
▪ When a program calls a function, the program control is
transferred to the called function.
▪ A called function performs a defined task and when its return
statement is executed or when its function-ending closing brace is
reached, it returns the program control back to the main program.
▪ To call a function, you simply need to pass the required
parameters along with the function name, and if the function
returns a value, then you can store the returned value.
Syntax Example
function-name (arg-1, arg 2, addition(varname ,varname );
…);
WAP to add two number using add(int, int)
Function
#include <stdio.h>
void add(int, int); // function declaration

void main()
{
int a = 5, b = 6;
add(a, b); // function call
}

void add(int x, int y) // function definition


{
printf("Addition is = %d", x + y);
}

Output
Addition is = 11
Return Statement
▪ If function is returning a value to calling function, it needs to use
the keyword return.
▪ The called function can only return one value per call.

Syntax
return;
Or

return (expression);
Category of Function
(1) Function with no argument and but no return value
No
void main() Input void fun1()
{ {
..... .....
No return .....
fun1();
value .....
.....
} }

(2) Function with no argument and return value

No
void main() Input int fun1(void)
{ {
..... .....
a = fun1() Function .....
..... result return b;
} }
Category of Function cont.
(3) Function with argument and no return value
Value of
void main() Argument void fun1(int f)
{ {
..... .....
fun1(a); No Return .....
..... value .....
} }

(4) Function with argument and return value

Value of
void main() Argument int fun1(int f)
{ {
..... .....
b = fun1(a); Function .....
..... Result return e;
} }
WAP to create simple calculator using
Function
#include <stdio.h> void add()
{
void add(); //no argument no return type int a=5,b=6,sum;
void sub(int , int); sum=a+b;
// with argument no return type printf(“sum =%d”,sum);
int mult(); }
//no argument with return type
float div(int ,int); void sub(int x,int y)
// with argument with return type {
int sub1;
void main() sub1=x-y;
{ printf(“sub =%d”,sub1);
int a = 5, b = 6,mul,division; }
add(); int mult()
sub(a,b); {
mul=mult(); int m,a=5,b=4;
Printf(“multiplication : m=a*b;
%d”,mul); return m;
} }
WAP to create simple calculator using
Function
#include <stdio.h>

void add(); //no argument no return type


void sub(int , int);
// with argument no return type
int mult(); float div(int x,int y)
//no argument with return type {
float div(int ,int); float d;
// with argument with return type d=(float)(x)/y;
return d;
void main() }
{
int a = 5, b = 6,mul;
Float division;
add();
sub(a,b); Example:: function_demo.c
mul=mult();
printf(“multiplication : %d”,mul);

division=div(a,b);
printf(“division : %f”,division);
Actual parameters and Formal parameters
▪ Values that are passed to the called function from the main
function are known as Actual parameters.
▪ The variables declared in the function prototype or definition are
known as Formal parameters.
▪ When a method is called, the formal parameter is temporarily
"bound" to the actual parameter.
Actual parameters Formal parameters
void main() void add(int x, int y)
{ // x and y are formal parameters.
int a = 5, b = 6; {
add(a, b); printf("Addition is = %d", x
// a and b are the actual + y);
parameters in this call.
} }
WAP to find maximum number from two
number
#include <stdio.h>
int max(int a, int b);
void main() Output
{ Max value is : 200
int a = 100;
int b = 200;
int maxvalue;
maxvalue = max(a, b);
printf("Max value is : %d\n",
maxvalue); Example::max.c
}
int max(int a, int b)
{
if (a > b)
return a; // return a
else
return b; // return b
}
Programs
▪ WAP to find Factorial of a Number using function.(Example::
factorial.c)
▪ WAP to check Number is Prime or not .(Example:: Prime.c)
Call by value and Call by reference in C
▪ here are two methods to pass the data into the function in C
language, i.e., call by value and call by reference.
Call by value in C
▪ In call by value method, the value of the actual parameters is
copied into the formal parameters.
▪ In call by value method, we can not modify the value of the actual
parameter by the formal parameter.
▪ In call by value, different memory is allocated for actual and
formal parameters since the value of the actual parameter is
copied into the formal parameter.
▪ The actual parameter is the argument which is used in the
function call whereas formal parameter is the argument which is
used in the function definition.
Swapping the values of the two variables
using call by value(Example:: callbyvalaue.c)
#include<stdio.h>
void swap(int x, int y)
void swap(int ,int); // x and y are formal parameters.
{
void main() int t=x;
{ x=y;
int a = 5, b = 6; y=t;
}
printf(“before swapping of
element”); At the time of function call
printf(“a=%d \t b=%d”,a,b); a 5 x 5

swap(a, b); b 6 y 6
// a and b are the actual
parameters in this call. After function definition
printf(“after swapping of
element”); a 5 x 6
printf(“a=%d \t b=%d”,a,b);
b 6 y 5
}
Call by reference in C
▪ In call by reference, the address of the variable is passed into the
function call as the actual parameter.
▪ The value of the actual parameters can be modified by changing
the formal parameters since the address of the actual parameters
is passed.
▪ In call by reference, the memory allocation is similar for both
formal parameters and actual parameters.
▪ All the operations in the function are performed on the value
stored at the address of the actual parameters, and the modified
value gets stored at the same address.
Swapping the values of the two variables
using call by address(Example::
callbyreference.c)
#include<stdio.h>
void swap(int x, int y)
void swap(int &,int &); // x and y are formal parameters.
{
void main() int t=x;
{ x=y;
int a = 5, b = 6; y=t;
}
printf(“before swapping of
element”); At the time of function call
printf(“a=%d \t b=%d”,a,b); a 5 x

swap(&a, &b); b 6 y
// a and b are the actual
parameters in this call. After function definition
printf(“after swapping of
element”); a 6 x
printf(“a=%d \t b=%d”,a,b);
b 5 y
}
Storage Classes
▪ Storage class decides the scope, lifetime and memory allocation of
variable.
▪ Scope of a variable is the boundary within which a variable can be used.

Storage Initial
Storage Scope Life Example
Specifier Value

Automatic Within End of int a;


Stack Garbage
{auto} block block auto int a;

Within End of
Register CPU Garbage register int
block block
{register} register var;

Global Till end


External Data
Zero Multiple of extern int var;
{extern} segment
file program
Till end static extern
Within
Static Data Zero of int var;
block
{static} segment program static int
var;
Static Example(Example:: staticvar.c)
#include <stdio.h>
int incrementCounter();
Output
void main() Counter = 1
{ Counter = 2
printf("Counter = %d \n", incrementCounter());
printf("Counter = %d \n", incrementCounter());
}

int incrementCounter()
{
static int count = 0; // static variable
count++;
return count;
}
Advantages of Function
▪ Using function we can avoid rewriting the same logic or code again
and again in a program.
▪ We can track or understand large program easily when it is divide
into functions.
▪ It provides reusability.
▪ It help in testing and debugging because it can be tested for errors
individually in the easiest way.
▪ Reduction in size of program due to code of a function can be used
again and again, by calling it.

You might also like