Beruflich Dokumente
Kultur Dokumente
CF-Zero Semester
About Functions
The general form of a function definition in C++ is as follows:
1 2 3
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”);
}
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
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
CF‐Lab Exercise prepared by Nauman Shamim Page 3
Example-3 (Approach 1):
#include<stdio.h>
#include<conio.h>
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 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
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.
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
CF‐Lab Exercise prepared by Nauman Shamim Page 9