Functions in UNIt 3C Language
Functions in UNIt 3C Language
Functions in C Language
Function is a group of statements that together perform a task. Every C++ program has at
least one function, which is main(), and all the most trivial programs can define additional
functions.
We can divide up your code into separate functions. How you divide up your code among
different functions is up to you, but logically the division usually is such that each function
performs a specific task.
A function declaration tells the compiler about a function's name, return type, and
parameters. A function definition provides the actual body of the function.
The C++ standard library provides numerous built-in functions that your program can call. For
example, function strcat() to concatenate two strings, function memcpy() to copy one memory
location to another location and many more functions.
A function is known with various names like a method or a sub-routine or a procedure etc.
Defining a Function
#include<iostream.h>
void sum(int x, int y)
{
int z;
z = x + y;
cout << z;
}
int main()
{
int a = 10;
int b = 20;
// calling the function with name 'sum'
sum (a, b);
}
Here, a and b are two variables which are sent as arguments to the function sum, and x and y
are parameters which will hold values of a and b to perform the required operation inside the
function.
Function body: is the part where the code statements are written.
Calling a Function
Functions are called by their names. If the function is without argument, it can be called directly
using its name. But for functions with arguments, we have two ways to call them,
1. Call by Value
2. Call by Reference
Call by Value
In this calling technique we pass the values of arguments which are stored or copied into
the formal parameters of functions. Hence, the original values are unchanged only the parameters
inside function changes.
#include<iostream.h>
void calc(int x);
void main()
{
int x = 10;
calc(x);
printf("%d", x);
}
void calc(int x)
{
x = x + 10 ;
}
10
Call by Reference
In this we pass the address of the variable as arguments. In this case the formal parameter can be
taken as a reference or a pointer, in both the case they will change the values of the original
variable.
#include<iostream.h>
void calc(int *p);
void main()
{
int x = 10;
calc(&x); // passing address of x as argument
printf("%d", x);
}
void calc(int *p)
{
*p = *p + 10;
}
20
C++ Programs
1. Following is the program to check whether a number is prime or not.
#include<iostream.h>
void main()
{
int no, i;
cout<<"Enter a number to be checked (greater than 2) : ";
cin>>no;
for(i=2;i<no;i++)
{
if(no%i==0)
{
break;
}
}
if(no==i)
{
cout<<"\n"<<no<<" is a prime number";
}
else
{
cout<<"\n"<<no<<" is not a prime number";
}
}
2. Following is the program to check whether a number is armstrong or not.
#include<iostream.h>
void main()
{
int no, ld, nn=0, cn;
cout<<"Enter a number to be checked : "
cin>>no;
cn==no;
while(no > 0)
{
ld=no%10;
nn=nn+ld*ld*ld;
no=n/10;
}
if(nn==cn)
{
cout<<"\n"<<cn<<" is an armstrong number";
}
else
{
cout<<"\n"<<cn<<" is not an armstrong number";
}
}
5. Following is the program to swap two numbers with the help of a temporary variable.
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,x;
cout<<"Enter a and b:\n";
cin>>a>>b;
cout<<"Before swapping"<<endl<<"Value of a= "<<a<<endl<<"Value of b=
"<<b<<endl;
{
x=a;
a=b;
b=x;
cout<<"After swapping"<<endl<<"Value of a= "<<a<<endl<<"Value of b=
"<<b<<endl;
}
PROGRAMMING IN C... 2nd Unit
getch();
}
6. Following is a program to check whether a string is palindrome or not.
#includ
e<iostr
eam.h>
#includ
e<strin
g.h>
void
main()
{
char string[50];
int len, i, x, flag=0;
cout<<"Enter a String(max 49
characters): ";
cin.getline(string,50);
len = strlen(string);
for(i=0, x=len-1;i<=len/2;i++, x--)
{
if(string[i]!=string[x])
{
flag=1; break;
}
}
if(flag==0)
{
cout<<"\n\nString is palindrome";
}
else
{
cout<<"\n\nString is not a palindrome";
}
}
Page 9 of 17
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Laxman Anukali
PROGRAMMING IN C... 2nd Unit
Unit -3
Non-formatted and Formatted input/output Functions
1) getchar()
2) putchar()
3) gets()
4) puts()
5) getch()
6) putch()
1) getchar()
putchar(var);
3) gets()
This function reads an input string.
Syntax
Page 10 of 17
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Laxman Anukali
PROGRAMMING IN C... 2nd Unit
gets(var);
Here, var is a character string
4) puts(var)
int getch(void)
or
variableName = getch();
6) putch()
putch() is an unformatied I/O function defined in conio.h headerfile.
This function is used for displaying a single alphanumeric character on the screen.
Syntax
putch(variable);
Programs Example
char ch:
clrscr;
printf("Enter a character:");
ch = getchar();
printf("You entered:");
Page 11 of 17
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Laxman Anukali
PROGRAMMING IN C... 2nd Unit
putchar(ch);
getch();
return 0;
}
Output
Enter a character: s
You entered: s
void main()
{
char a[25];
clrscr();
printf("Enter the string :");
gets(a);
puts(a);
getch();
return;
Output
Enter the string : Hello
Hello
Example on getch() and putch()
#include<stdio.h>
#include<conio.h>
int main()
Page 12 of 17
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Laxman Anukali
PROGRAMMING IN C... 2nd Unit
{
char ch;
printf("Press any key\n");
ch = getch();
printf("The key pressed is:");
putch(ch),
return 0;
}
Output
Press any key The key
The key pressed is: s
Page 13 of 17
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Laxman Anukali
PROGRAMMING IN C... 2nd Unit
The input and output data which is ordered in specific format is called format input and
output data.
There are two formatted input/output functions.
1) scanf():
It is used for formatted input
2) printf()
1) It is used to obtain formatted output
1) scanf():
This function is used to read values for variables from keyboard.
Sýntax:-
scanf(“control string”, address_list);
Control String
➢ Control string is enclosed within double quotes.
➢ It specifies the type of values that have to be read from keyboard.
➢ It consists of field specification written in the form "%field specification”.
Page 14 of 17
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Laxman Anukali
PROGRAMMING IN C... 2nd Unit
%ox Hexadecimal
%o Octal
%i Decimal or hexadecimal or octal
2) printf()
This function is used to print result on monitor.
Syntax
printf("control string", argl, arg2...., argn);
Control String
Control string can have,
➢ Format specifier given in the form "% specifier"
➢ Escape sequence characters such as t (tab), \n (new line), \b (blank) etc.
➢ It can have a string that must be printed on console i.e., monitor.
➢ argl, arg2...., argn are the variables whose values must be printed on the monitor in
the format specified in control string
Examples
Page 15 of 17
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Laxman Anukali
PROGRAMMING IN C... 2nd Unit
Programme
#include<stdio.h>
#include<conio.b>
int main()
int s id;
char s_name[20];
float s percentage:
clrscr;
printf("Enter Student ID:");
scanf("%d",&s_id);
printf("Enter Student Name:");
scanf("%s",&s_name);
printf("Enter Student Percentage:”);
scanf("%f",&s_percentage);
printf("\nThe Student Details are:");
printf(" nStudent Id:%d\nStudent Name:%s\nStudent Percentage:%f",s_id,s_name,s
percentage);
getch();
return 0;
}
Output
Enter Student Id : 1
Enter Student Name: Sai
Enter Student Percentage:75
The student details are :-
Student Id : 1
Page 16 of 17
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Laxman Anukali
PROGRAMMING IN C... 2nd Unit
Student Percentage:75
Page 17 of 17
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Laxman Anukali