Вы находитесь на странице: 1из 6

Lab‐06 

BS(ME)- Computing Fundamentals


p

About Functions 

The general form of a function definition in C++ is as follows:

1 2 3

return type function-name(parameter-list)


{
local variable-definitions; 4
function-implementation;
return value; 5
}
Parts Of Function 
1‐ Type of the value that the function will return 
2‐ Name of the function  
3‐ Zero or more variables that becomes the input to the functions 
4‐ Body of the function,  contains the code which is executed when the function is called 
5‐ The values that function wants to return  

Key Note: When a function don’t want to return a value, it tells the compiler by means of void,

There can be following forms of a functions 
A. Function with no return values and no arguments 
B. Functions with arguments with no return value 
C. Functions with return value with no arguments 
D. Functions with return value and arguments 
 
Example 1:
void DisplayMyName(void){
printf(”My Name is Ahmed\n”);
}

About this function:


Name: DisplayMyName
Return Type: void (The function will not return a value)
Parameter: void (The function will not take any parameters)
Body :{ printf(”My Name is Ahmed\n”);}

CFP‐Lab Exercise by Nauman Shamim  Page 1 
Example 2:
void EvenOrOdd(int num){
if(num%2==0)
printf(”Even”);
else
printf(”Odd”);
}

About this function:


Name:EvenOrOdd
Return Type : void (The function will not return a value)
Parameter :One parameter of type int
Body : {
if(num%2==0)
printf(”Even”);
else
printf(”Odd”);
}
To use this function or to call this function user has to pass an
integer type variable or integer constant.
Valid Function Call :
EvenOrOdd(45);
EvenOrOdd(a); (Where a is a variable of type int)
Invalid Calls:
evenOrOdd(45) : Function name is incorrect
EvenOrOdd(23,34): Number of parameters mismatched
EvenOrOdd(“45”): Parameter Type mismatched

Task‐01 
Try to identify the various parts of the following functions and valid function calls

Program-01
float DollaRate(void){
return 85.6;
}

CFP‐Lab Exercise by Nauman Shamim  Page 2 
Program-2

int makeEven(int x){


int result;
if(x%2==0)
result=x;
else
result=x+1;
return result;
}

Task‐02 
Look again at function in program-2 and try to understand what this function is doing

Declaring And Using A Function 
A function should be declared before main(), one way is to declare and define the function
before main, other way is to declare the function before main and define it after main

Example-3 (Method 1):


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

int add(int x,int y){ //Complete function , no prototyping


int result;
result=x+y;
return result;
}
main(){
int a;
a=add(10,20);
printf(”Sum of 10 and 20 is %d”,a);
getch();
}

CFP‐Lab Exercise by Nauman Shamim  Page 3 
Example-4 (Method 2):
#include<stdio.h>
#include<conio.h>

int add(int ,int );//Prototype , parameter type is specified


//only , Parameter names are not necessary here
main(){
int a;
a=add(10,20);
printf(”Sum of 10 and 20 is %d”,a);
}
int add(int x,int y){
int result;
result=x+y;
return result;
}

Example ‐5 Using Functions To Write Better Programs 
Let’s write a program that prints a produce a good looking output for even odd problem. 
#include<stdio.h> 
#include<conio.h> 
 
void printLine(void){ 
printf("\n**************************************************\n"); 

 
void main () { 
  int num; 
    Output
  printLine(); 
  printf("Enter a Number :");  ************************************************
  scanf("%d",&num);  Enter a Number :13
  Odd Number
  if(num%2==0) 
************************************************
  printf("Even Number"); 
  else 
  printf("Odd Number"); 
  printLine(); 
  getch(); 

CFP‐Lab Exercise by Nauman Shamim  Page 4 
Example‐5A Let’s add information about the author as well. 
#include<stdio.h>
#include<conio.h>

void author(void){
printf("\nAuthor : ABCD\n");
printf("Date: 26-Nov-2015\n");
printf("About: Lives in Islamabad Pakistan\n");
printf("email : abcd@hotmail.com\n");
}

void printLine(void){
printf("\n*************************************************\n");
}
void main () { Output
int num;
Author : ABCD
Date: 26-Nov-2015
About: Lives in Islamabad Pakistan
author();
email : abcd@hotmail.com
printLine();
printf("Enter a Number :"); ***********************************
scanf("%d",&num); Enter a Number :13
Odd Number
if(num%2==0) ***********************************
printf("Even Number");
else
printf("Odd Number");
printLine();

getch();
}
 
Task‐03 
1. Write a program that calls print line function 5 times using a loop.
2. Modify the example-5A and add a new function void evenOdd(int), the function
should print even or odd according to the number passed. Update the program to
use this function
3. Write a program that prints the status of 10 numbers as even or odd using your
evenOdd() function, the numbers can be as simple as 1-10 or can be between two
numbers A and B provided by the user.

CFP‐Lab Exercise by Nauman Shamim  Page 5 
Tasks‐04 
1. Write a function pow() that two integer parameters a and n and returns nth power of
a, write a program to use this function.
2. Write a function that returns the greater of the two numbers

Task‐05  
Try to write the following functions and programs ,
Area of a Rectangle (Function)
The function should accept two arguments i.e. length and width of the rectangle and should
return the area of the rectangle, the definition of the function is as under

int areaRect(int, int);


Use the function
Use the function areaRect() and develop a program that allow user to enter the length and
width of the rectangle, the program will display the area of the rectangle calculated by the
function areaRect
Square or Not (Function)
Develop a function that accepts two integer values as length and width of a shape and
returns 0 or 1. A 1 will be returned if the shape is a square otherwise a 0 will be returned,
the definition of the function is given below
int isSquare(int l, int )
Use the function
Write a program that will ask use to enter length and width of a shape and will inform the
user weather the shape is a rectangle or a square.
Extension
Develop a program that will use the functions areaRect() and isSquare(), the program will
ask user to enter two integer values as length and width of a shape, the program will
display type of shape i.e. square or rectangle and will display the area of the shape as well.
How Many Squares (Function)
Ahmed has large number of wooden boards, he wants to cut them into squares of different sizes to be 
used in decoration of his house, develop a function that will accept the length and width of the board 
along with one side of the square, the function will return the number of smaller square boards that can 
be made from the larger board.The definition of the function is given below 
 
int howManySuquares(int, int, int) 

CFP‐Lab Exercise by Nauman Shamim  Page 6 

Вам также может понравиться