You are on page 1of 7

Collage of Engineering (COE) EEEB114: Programming For Engineers

LAB 7: Functions
Student Name: Student ID: Section:

7.1) LEARNING OBJECTIVES


By the end of this experiment, you should be able to:
Explain the concepts of functions.
Explain what a function prototype is and how that is different from the function definition.
Identify different types of functions depending on the return type and arguments.
Understand how recursive function operates

7.2) PRE LAB ASSIGNMENT


1. What must the return type be if the function returns nothing?
Answer:-

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.

Three things are necessary while using a function.

i. Declaring a function or prototype:

The general structure of a function declaration is as follows:

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.

ii. Calling a function:

The function call is made as follows:

return_type = function_name(arguments);

iii. Defining a function:

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

7.4) IN LAB ACTIVITIES


Functions Activity 1: without arguments and without return values

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

Functions Activity 2: with arguments and without return values

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

void Check_Display(int n) //Function definition


{ /*Note: There is no return value to the Main program. Hence return type
of Check_Display() is void*/
int i;

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

Functions Activity 3: without arguments and with return values


#include <stdio.h>
int input(); //Function declaration
int main()
{ int num, i;

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

int input() //Function definition


{
int n;
printf("Enter a positive integer and press enter button to check:\n");
scanf("%d", &n);
return 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 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().

Functions Activity 4: with arguments and with return values


1. Write a main program with 1 function that is with argument and with return value. This
program should display exactly the same outputs when 70, 75 and 0 are inputted. The
main program should get the input from the user and pass the input to the function
Check(). Function Check() will check whether the number is zero, odd or even. Function
Check() will return a value of 0 if the number is ZERO, return a value of 1 if the number
is an ODD number and return a value of 2 if the number is an EVEN number. Then based
on the return value, the main function will display the output to the user.

2. Show and execute your program to your lab demonstrator for verification.

7.5) STATE YOUR LEARNING CURVE


Note: conclude what youve learned from this lab activity.

Page 7 of 7
Prepared By Prajindra June 2015

You might also like