Sie sind auf Seite 1von 9

Lab‐09 

CF-Zero Semester

 
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”);}

CF‐Lab Exercise prepared 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

CF‐Lab Exercise prepared by Nauman Shamim  Page 2 
Task‐01 
Try to identify the various parts of the following functions and valid function calls

Function-01

float DollaRate(void){
return 85.6;
}

Function -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,
expect what this function will do, write a program to use this function, compare expected
working of the function with actual output

Declaring and Using a Function 
Following are two main approaches to define a function
1. Using a prototype
2. Without Prototype

Prototype: The prototype of a function is header of the function without its body, it is
necessary to define the type of the parameters, writing names of the parameters is
optional. To better understand the concept look at the following prototype

int add(int, int); Or int add(int a, int b);

important points about function prototype

1. A prototype is terminated with a semicolon


2. A prototype is declared before main
3. Body of the function can be defined before or after main.

CF‐Lab Exercise prepared by Nauman Shamim  Page 3 
Example-3 (Approach 1):
#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-4 (Approach 2):


#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();
}

CF‐Lab Exercise prepared by Nauman Shamim  Page 4 
Q: Why use prototype? 
To understand why prototyping is important look at the following program

void a(void){ Can we call function b() inside function a() ?


printf(“Function a”); No, as function a() does not know anything
b(); about function b()
} A function can call only those functions which
void b(void){ are defined or declared before it
printf(“Function b”);
}
Can we call function a() and b() inside
void main(){ main() ? Yes as both functions are defined
a(); before main
b();
}

Same program with prototyping


void a(void)
void b(void)

void a(void){
Can we call function a() and b()
printf(“Function a”);
inside main() ? Yes as the prototype
b(); of b() is declared before a()
}
void b(void){
printf(“Function b”);
}

void main(){
a();
b();
}

Another Version
void a(void)
void b(void)
Prototype of functions a() and b() is
void main(){ declared before main, the program will
a(); try to locate the function inside header
b(); files, before main and after main.
}
void a(void){
printf(“Function a”);
b();
}
void b(void){
printf(“Function b”);
}

CF‐Lab Exercise prepared by Nauman Shamim  Page 5 
Important Point
Prototyping allows user to forget about the order in which functions are defined

Example ‐5 Using Functions to Write Better Programs 

Let’s write a program that produces a good looking output for even odd problem. 
#include<stdio.h> 
#include<conio.h> 
 
void printLine(void){ 
printf("\n**************************************************\n"); 

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

 
  Output
 
************************************************
 
Enter a Number :13
 
Odd Number
 
************************************************
 
 
 

CF‐Lab Exercise prepared by Nauman Shamim  Page 6 
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 () {
int num;

author();
printLine();
printf("Enter a Number :");
scanf("%d",&num);

if(num%2==0)
printf("Even Number");
else
printf("Odd Number");
printLine();

getch();
}
 
 
 
 
  Output
  Author : ABCD
  Date: 26-Nov-2015
  About: Lives in Islamabad Pakistan
  email : abcd@hotmail.com
 
 
 
***********************************
  Enter a Number :13
  Odd Number
  ***********************************
 
 
 

CF‐Lab Exercise prepared by Nauman Shamim  Page 7 
 
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.

Tasks‐04 
1. Write a function pow() which takes 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 i.e. make a function
that takes two integer arguments and returns the greater of the two. Write program
to use this function.

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 )

CF‐Lab Exercise prepared by Nauman Shamim  Page 8 
Use the function
Write a program that will ask user 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) 
RollDice Funciton
A normal dice has 6 sides numbered from 1 to 6, when the dice is rolled the number on top
side of the dice is considered to be the result of rolling the dice. Dices are used in many
games etc. Your task is to write a function rollDice(), the function should return an integer
value ranging from 1 to 6, to make function return a random number each time use the
library function rand() and srand() (covered in previous labs). Write a program that allow
the user to call this function multiple times , display the value returned by the function.

Try implementing the following functions that are already available in header file
string.h
1. int strlen(char str[])
The function should return the length of the string passed. The character passed
should must be null terminated

2. int strCopy(char source[], char target[])


the function should copy the character source into array target, if for some reason
copy operation cannot be performed such as size of target array is smaller than
source array the function should return -1 otherwise it should return 0

CF‐Lab Exercise prepared by Nauman Shamim  Page 9 

Das könnte Ihnen auch gefallen