Академический Документы
Профессиональный Документы
Культура Документы
About Functions
The general form of a function definition in C++ is as follows:
1 2 3
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”);
}
CFP‐Lab Exercise by Nauman Shamim Page 1
Example 2:
void EvenOrOdd(int num){
if(num%2==0)
printf(”Even”);
else
printf(”Odd”);
}
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
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
CFP‐Lab Exercise by Nauman Shamim Page 3
Example-4 (Method 2):
#include<stdio.h>
#include<conio.h>
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
CFP‐Lab Exercise by Nauman Shamim Page 6