Unit 4 Functions C Prog

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

Go, change the world

RV College of
Engineering

INTRODUCTION TO C PROGRAMMING (22ES14A)

UNIT IV
Functions
(User Defined Functions)
RV College of Go, change the world
Engineering

FUNCTIONS

A function in C language is a block of code that performs a specific task.

It has a name and it is reusable i.e. it can be executed from as many different parts in
a C Program as required.

It also optionally returns a value to the calling program.


RV College of Go, change the world
Engineering

Properties of C Functions:
Every function has a unique name. This name is used to call function from “main()” function.
A function can be called from within another function.
A function is independent and it can perform its task without intervention from or interfering
with other parts of the program.
A function performs a specific task. A task is a distinct job that your program must perform as
a part of its overall operation, such as adding two or more integer, sorting an array into
numerical order, or calculating a cube root etc.
RV College of Go, change the world
Engineering

Properties of C Functions:

A function returns a value to the calling program.

This is optional and depends upon the task your function is going to accomplish.

Suppose you want to just show few lines through function then it is not necessary to return a
value.

But if you are calculating area of rectangle and wanted to use result somewhere in program then
you have to send back (return) value to the calling function.
RV College of Go, change the world
Engineering

Types of functions in C

1. void function(void)

2. void function(int)

3. int function(void)

4. int function(int)
RV College of Go, change the world
Engineering

Type-1

void function(void)
RV College of Go, change the world
Engineering

Type-1: void function(void)


RV College of Go, change the world
Engineering

Type-1: void function(void)


#include<conio.h>
#include<stdio.h>
void rvce(void); //function declaration
void main()
{
clrscr();
rvce(); //function call
getch();
}
void rvce(void) //function definition
{
printf(”rvce");
}
RV College of Go, change the world
Engineering

Output:
Rvce

We can call a function for more than once in main.


RV College of Go, change the world
Engineering

#include<conio.h>
#include<stdio.h>
void rvce(void);

void main()
{
clrscr();

rvce();
rvce();
getch();
}
void rvce(void)
{
printf(”rvce");
}
RV College of Go, change the world
Engineering

Type-1: void function(void)


#include<conio.h>
#include<stdio.h>
void rvce(void); //function declaration
void main()
{
clrscr();
rvce(); //function call
getch();
}
void rvce(void) //function definition
{
printf(”rvce");
}
RV College of Go, change the world
Engineering

Output:
rvce rvce

We can build/call more than one functions in our program.


RV College of Go, change the world
Engineering

#include<stdio.h>
void rvce(void);
void college(void);
void main()
{
clrscr();
rvce();
college();
}
void rvce(void)
{
printf(”rvce");
}
void college (void)
{
printf(“college is rvce");
}
RV College of Go, change the world
Engineering

Output
rvce
college is rvce
RV College of Go, change the world
Engineering

Type-2

void function(int)
RV College of Go, change the world
Engineering
RV College of Go, change the world
Engineering

#include<conio.h>
#include<stdio.h>

void square(int);

void main()
{
clrscr();

square(5);

getch();
}

void square(int x)
{
printf("Square is %d",x*x);
}
RV College of Go, change the world
Engineering
Output
Square is 25

19
RV College of Go, change the world
Engineering

#include<conio.h>
#include<stdio.h>

void table(int);

void main()
{
clrscr();

table(5);

getch();
}

void table(int x)
{
for(int a=1;a<=10;a++)
printf("%d*%d=%d\n",a,x,a*x);
}
RV College of Go, change the world
Engineering
RV College of Go, change the world
Engineering

Output

1*5=5
2*5=10
3*5=15
4*5=20 We can pass more than one arguments to a function, each
5*5=25
6*5=30 can have different type/same type.
7*5=35
8*5=40
9*5=45
10*5=50
RV College of Go, change the world
Engineering

#include<conio.h>
#include<stdio.h>

void add(int, int);

void main()
{
clrscr();

add(5, 7);

getch();
}

void add(int x, int y)


{
printf(“Addition is %d",x+y);
}
RV College of Go, change the world
Engineering

Output

Addition is 12
RV College of Go, change the world
Engineering

Write a program to generate a loop between two numbers.(Use functions to achieve the task)
#include<conio.h>
#include<stdio.h>

Void makeloop(int, int);

void main()
{
clrscr();

makeloop (2, 15);

getch();
}

void makeloop (int x, int y)


{
for(int a=x; a<=y; a++)
printf(“%d,”, a);
}
RV College of Go, change the world
Engineering

Output

2,3,4,5,6,7,8,9,10,11,12,13,14,15
RV College of Go, change the world
Engineering

Edit same program, get two numbers input from user, then
make a loop between these numbers.

Ex:
Please input two numbers: 5 20

5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
RV College of Go, change the world
Engineering

#include<conio.h>
#include<stdio.h>

Void makeloop(int, int);

void main()
{
clrscr();
int start, end

printf(“Plzz input two numbers:”);


scanf(“%d%d”, start, end);

makeloop (start, end);

getch();
}

void makeloop (int x, int y)


{
for(int a=x; a<=y; a++)
printf(“%d,”, a);
}
RV College of Go, change the world
Engineering

Type-3

int function(void)
RV College of Go, change the world
Engineering
RV College of Go, change the world
Engineering

#include<conio.h>
#include<stdio.h>

int sum(void);

void main()
{
clrscr();
int a;

a=sum();
printf(“Sum is %d”, a);
getch();
}
int sum(void)
{
int x=2, y=3, z;
z=x+y;

return(z);
}
RV College of Go, change the world
Engineering

#include<conio.h>
#include<stdio.h>

int sum(void);

void main()
{
clrscr();
int a;

a=sum();
printf(“Sum is %d”, a);
getch();

int sum(void)
{
int x=2, y=3, z;
z=x+y;

return(z);
}
RV College of Go, change the world
Engineering

Return Keyword
In functions, where we return any value, we usually use a keyword
“return”, and the value, which is to be returned is written inside braces,
shown after return.
Example: return(z);
The above statement will pass the value of z to the left side variable of
calling function.
ie: a=sum();
Hence the value in z, which is 5 in our program is passed to a.
RV College of Go, change the world
Engineering

Type-4
Returns a value.

int function(int)

Passes the argument(s).


RV College of Go, change the world
Engineering

#include<conio.h>
#include<stdio.h>

int sum(int, int);

void main()
{
clrscr();
int a;

a=sum(2,3);
printf(“Sum is %d”, a);
getch();

int sum(int x, int y)


{

z=x+y;

return(z);
}
RV College of Go, change the world
Engineering

#include<conio.h>
#include<stdio.h>
int sum(int, int);

void main()
{
clrscr();
int a;

a=sum(2,3);
printf(“Sum is %d”, a);
getch();

int sum(int x, int y)


{

z=x+y;
return(z);
}
RV College of Go, change the world
Engineering

End of the Chapter

You might also like