Unit 1 Functions
Unit 1 Functions
Unit 1 Functions
Rules:
example:
#include<iostream>
int Add (int, int); //function prototype for Add
int main()
{
Add(3); /* <- There is the error ! */
return 0;
}
int Add(int a, int b)
{
int sum;
sum=a+b;
cout<<sum;
}
When you try to compile this program, the compiler will flag an error
on the function call statement.
So in the future, use function prototypes for every function to save
your lot of time!
Function Definition
Here we write the actual code of the
function.
A function can be defined only once in a
program.
Syntax
return_type function_name (type1 name1, type2 name2,
...,type n name n)
{
....statements...
}
2.Name of a function
Used to identify the function
3.Parameter List
This indicates the type & sequence of data type that
should be given as i/p to the function.
Body of a function
Here we write a set of instruction that work
on the i/p data & generate the result.
Some examples
Function Prototype Examples
double squared (double number);
void print_report (int);
int get _menu_choice (void);
Function Definition Examples
double squared (double number) void parameter list means
{ it takes no parameters
return (number * number);
}
void print_report (int report_number)
{ if (report_number == 1)
cout<<“Print Report 1”; return type void means
it returns nothing
else
cout<<“Not printing Report 1”;
}
Function Arguments
A function represents the set of instruction to be
executed on the data that is given to it at the time of calling
it.
There are 2 ways in which this data can be given to
the function.
1) By value and
2) By reference
1) Passing values
passing values is known as call by value. It is actually
pass a copy of the variable to the function. If the function
modifies the copy , the original remains unaltered.
This creates a copy of i/p data which is local for the
function. The function uses the values from these local copies
& returns the o/p.
Example-
Write a program for doing addition of 3 nos. using function.
int calsum(int, int, int);
void main()
{
int num1,num2,num3,sum;
cout<<“enter 3 nos.”;
cin>>num1>>num2>>num3;
sum=calsum(num1,num2,num3);
cout<<sum;
}
int calsum(int x,int y,int z)
{
int sum1;
sum1=x+y+z;
return(sum1);
}
Return Statement
To return a value from a C++
function you must explicitly return
it with a return statement.
A return statement is placed within
the body of a function.
This statement terminates the
function that is currently executed.
Local Variables
Local Variables
int func1 (int y)
{
int a, b = 10;
float rate;
double cost = 12.55;
.......
}
Format: c[8] -3
c[9] 1
arrayname[ position number ] c[10] 6453
First element at position 0 c[11] 78
c[ 0 ], c[ 1 ]...c[ n – 1 ] Position number
of the element
within array c
Arrays
Array elements are like normal
variables
c[ 0 ] = 3;
cout<< c[ 0 ];
Perform operations in subscript. If x
equals 3
c[ 5 - 2 ] == c[ 3 ] == c[ x ]
Declaring Arrays
When declaring arrays, specify
Name
Type of array
Dimension
Number of elements/size
int c[10];
float myArray[3284];
Declaring multiple arrays of same type
Format similar to regular variables
Example:
int b[ 100 ], x[ 27 ];
Examples Using Arrays
Initializers
int n[ 5 ] = { 1, 2, 3, 4, 5 };
If not enough initializers, rightmost elements become
0
int n[ 5 ] = { 0 }
All elements 0
If too many a syntax error is produced
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>m1[i][j];
}
}
Structures
Structures are used to group together different types of
variables under the same name.
#include<iostream>
struct telephone
{
char name[10];
int number;
};
int main()
{
struct telephone index;
index.name = “xyz"; //if char put in quotations.
index.number = 12345;
cout<< index.name;
cout<<index.number;
return 0;
}
Write a program to assign the values and retrieve the values using structure.
int main()
{
TELEPHONE index;
index.number = 12345;
cout<<"Telephone number:”<< index.number;
return 0;
}
Note: The word struct is not needed before TELEPHONE index;
Arrays of Structures:
What is Array
What is Structure.
It makes programming easier.
Ex. Store record of 50 student’s. It
is must to use array of structures.
typedef struct student
{
int roll_no;
char name[20];
int marks;
}stud;
stud s[10];
int main()
{
int n,i;
cout<<“Enter total no. of students”;
cin>>n;
for(i=0;i<n;i++)
cin>>s[i].roll_no>>s[i].name>>s[i].marks;
for(i=0;i<n;i++)
cout<<s[i].roll_no<<s[i].name<<s[i].marks;
return 0;
}
Union
A union is like a structure contains members
whose individual data types may differ from one
another.
A union in which all members are stored at the
same address.
Members of a union can only be accessed one at a
time.
Just like with structures, the members of unions
can be accessed with the . and -> operators.
Take a look at the example:
union standard
{
int primary;
char pre_pri[10];
};