Professional Documents
Culture Documents
LAB 7: Functions
Student Name: Student ID: Section:
2. What is the default return type of a function if the return type of the function is not
specified? For example, what is the return type of:- FunctionCalculateVoltage(int current)?
Answer:-
3. How many value(s) can be returned from a C function to the main program?
Answer:-
4. List the 4 types of functions based on the return type and argument:
i.
ii.
iii.
iv.
Page 1 of 7
Prepared By Prajindra June 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers
7.3) BACKGROUND
Functions are used normally in those programs where some specific work is required to be done
repeatedly and looping fails to do the same.
return_type function_name(arguments);
Before defining a function, it is required to declare the function i.e. to specify the function prototype. A
function declaration is followed by a semicolon ;. Unlike the function definition only data type are to be
mentioned for arguments in the function declaration.
return_type = function_name(arguments);
All the statements or the operations to be performed by a function are given in the function definition
which is normally given at the end of the program outside the main. Function is defined as follows
return_type function_name(arguments)
Statements;
There are certain functions that you have already used e.g: printf( ), scanf( ), etc.
A function that returns nothing must have the return type void. If nothing is specified then the return
type is considered as int. The summary of four types of functions are displayed below:
Page 2 of 7
Prepared By Prajindra June 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers
No C function Syntax
1 without arguments and without void function (); // function declaration
return values
function (); // function call
void function () // function definition
{
statements;
}
2 with arguments and without void function (int); // function declaration
return values
function (a); // function call
void function (int a) // function definition
{
statements;
}
3 without arguments and with int function (); // function declaration
return values
function (); // function call
int function( ) // function definition
{
statements;
return a;
}
4 with arguments and with int function (int); // function declaration
return values
function (a); // function call
int function(int a) // function definition
{
statements;
return a;
}
Page 3 of 7
Prepared By Prajindra June 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers
1. Type and execute the program above. What does the function EvenOdd() do?
2. Write down the outputs at the console window for the 3 different inputs:- 70, 75, and 0.
Page 4 of 7
Prepared By Prajindra June 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers
#include <stdio.h>
void Check_Display(int n); //Function declaration
int main()
{
int num;
printf("Enter a positive integer and press enter button to check:\n");
scanf("%d", &num);
Check_Display(num);
return 0;
}
i = n%2;
if (n==0)
printf("\nThe number is ZERO\n");
else if (i==0)
printf("\n%d is an EVEN number\n",n);
else
printf("\n%d is an ODD number\n",n);
}
1. Copy and paste program above. Execute it and write down the outputs at the console
window for the 3 different inputs:- 70, 75, and 0.
2. Although the outputs at the console window are the same, identify and explain the
difference between this program and the earlier program in activity 1 based on the coding
in Function Check_Display() and Function EvenOdd().
Page 5 of 7
Prepared By Prajindra June 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers
num=input();
i = num%2;
if (num==0)
printf("\nThe number is ZERO\n");
else if (i==0)
printf("\n%d is an EVEN number\n",num);
else
printf("\n%d is an ODD number\n",num);
return 0;
}
1. Copy and paste program above. Execute it and write down the outputs at the console
window for the 3 different inputs:- 70, 75, and 0.
2. Although the outputs at the console window are the same, identify and explain the
difference between this program and the earlier programs in activity 1 and 2 based on
the coding in Function input and Function Check_Display().
Page 6 of 7
Prepared By Prajindra June 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers
3. Delete the last statement in function input(), i.e. return n;. Execute the program a few
times and try to input 2, 4, and 10. Explain what happen when the statement is deleted?
Briefly describe what does return n; do in the function input().
2. Show and execute your program to your lab demonstrator for verification.
Page 7 of 7
Prepared By Prajindra June 2015