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

Computer Language Lecture 25

The document discusses making a table by listing the subtasks of making a seat, legs for the table, and assembling them. It then provides examples of functions, how they are defined, declared, what values are passed and returned, and different types of functions including those that return values and those that do not. Examples are given of function declarations, definitions, return types, and calling functions by value.

Uploaded by

Azhar Hussain
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Computer Language Lecture 25

The document discusses making a table by listing the subtasks of making a seat, legs for the table, and assembling them. It then provides examples of functions, how they are defined, declared, what values are passed and returned, and different types of functions including those that return values and those that do not. Examples are given of function declarations, definitions, return types, and calling functions by value.

Uploaded by

Azhar Hussain
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Making a table

 Task: Making a table


 Subtask:
 Make a seat
 Make legs for the table
 Assemble them
What we will study today …

 What are functions?


 How are they defined ?
 How are they declared ?
 What values are passed to functions ?
 What values do functions return ?
Function

Function name
{
Body of the function
}
Function

Two types of functions:


 Functions that return a value
 Functions that do not return a value
Function

return-value-type function-name( argument-list )

declarations and statements

}
Declaration : Example

int function-name ( int , double ) ;

void main ( )
{
….

}
Definition of Function

int function-name ( int i , double j )


{

}
Return Type of Function

Declaration

int square ( int ) ;

Definition

int square ( int i )


{
return ( i * i ) ;
}
#include < iostream.h >
void starline ( ) ;
void main ( )
{
int i ;
starline();
cout << “ Work Hard Inshallah U will be Succeed ” <<endl ;
starline();
getch();
}
void starline ( )
{ for (int i=1;i<=30;i++ )
cout<<“ * ”;
cout<<endl;
}
#include < iostream.h >
void square ( int ) ;
void main ( )
{
int n ;
cout << “ Please enter the number = “ ; cin >> n ;
square(n);
getch();
}
void square ( int i )
{ int s;
s=i*I;
cout<< s ;
}
#include < iostream.h >
void square ( int ) ;
void main ( )
{
int n ;
cout << “ Please enter the number = “ ; cin >> n ;
square(n);
getch();
}
void square ( int i )
{ int s;
s=i*I;
cout<<“the square is” <<s ;
}
#include < iostream.h >
int square ( int ) ;
void main ( )
{
int n ;
cout << “ Please enter the number = “ ; cin >> n ;
cout<< “the square of the given no. ” <<n<< “is”;
square(n);
getch();
}
int square ( int i )
{ int s;
s=i*I
return<<s ;
}
Example: Function to calculate integer
power ( X )
n

double powerofx ( double x , int power )


{
double result ;
int i;
result = 1.0 ;
for ( i = 1 ; i <= power ; i ++ )
result = result *x ;
return ( result ) ;
}
Code to Call the powerofx Function
# include < iostream.h >
double powerofx(double, int);
void main ( )
{
double x ; int p ;
cout << “ Please enter the number “ ;
cin >> x ;
cout << “ Please enter the power“ ;
cin >> p ;
cout << x << “ raise to power “ << p << “is
equal to “ << powerofx( x , p ) ; }
Call By Value

calling function

called function
Area of the ring

Outer circle

Inner circle

Area of ring = Area of outer circle – Area of inner circle


Function to calculate the area of cirlce

double circleArea ( double radius )


{
return ( 3.1415926 * radius * radius ) ;
}
Program to Call the CircleArea Function

#include < iostream.h >


void main ( )
{
double rad1 ; double rad2 ;
double ringArea;
cout << “ Please enter the radius of outer circle” ;
cin >> rad1 ;
cout << “ \n Please enter the radius of the inner circle “ ;
cin >> rad2 ;
ringArea = circleArea ( rad1 ) – circleArea (rad2 ) ;
cout<< “ Area of ring “<< “ is “ << ringArea ;
}
Area of Ring without function

main( )
{
:
ringArea = ( 3.1415926 * rad1 * rad1 ) – ( 3.1415926 *
rad2 * rad2 ) ;

}
Prototype

 Syntax

functiontype functionname ( parameter list )

 Ex.

double circleArea ( double radius )

Or rewrite

double circleArea ( double )


Assignment using functions

 Write a program who prompt user to enter 10


numbers and return the largest number.

 Write a program who prompt user to enter 10


numbers and return the smallest number

You might also like