0% found this document useful (0 votes)
109 views

Lab 08 Function 1 PDF

This document is a lab assignment for a computer programming lab course. It discusses functions in C++, including function declaration, definition, parameters, return values, and side effects. The lab objectives are to learn how to apply user-defined functions in C++ and understand functions with and without return values. The document provides examples and exercises for students to write programs using different types of functions.

Uploaded by

Fawad FAdi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

Lab 08 Function 1 PDF

This document is a lab assignment for a computer programming lab course. It discusses functions in C++, including function declaration, definition, parameters, return values, and side effects. The lab objectives are to learn how to apply user-defined functions in C++ and understand functions with and without return values. The document provides examples and exercises for students to write programs using different types of functions.

Uploaded by

Fawad FAdi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CSL-113: Computer Programming Lab

Semester: Spring 2019


Lab Engineer: Tarwan Kumar

Lab 08: Functions – I

Objective(s) : Upon completion of this lab session, learners will be able to:

1. Apply user defined functions in C++


2. Function declaration, calling and definition.
3. Functions with Side Effects
4. Functions with Return Value

Function

A function is a group of statements that together perform a task. Every C++ program has at least one
function, which is main(), and all the most trivial programs can define additional functions. The
execution of the program always starts with main, but it can call other functions to do some part of
the job.

A function in C++ can have a value, a side effect, or both.

 The side effect occurs before the value is returned.


 The function’s value is the value of the expression in the return statement.
 A function can be called for its value, its side effect, or both.

Department of Computer Sciences 1/4 Semester BSCS 01 & BS IT 02


CSL-113: Computer Programming Lab Lab 08: Function-1
User Defined Function

Like any other object in C++, functions must be declerad and defined. The function declaration is
done first with a prototype decleration. A function can be called with main() function or other user
defined fuction(s). the function definition, which is traditionally coded after the function that makes
the call, contains the code required.

Function with only side effects.

Department of Computer Sciences 2/4 Semester BSCS 01 & BS IT 02


CSL-113: Computer Programming Lab Lab 08: Function-I
Function with return value.

Department of Computer Sciences 3/4 Semester BSCS 01 & BS IT 02


CSL-113: Computer Programming Lab Lab 08: Function-I
EXERCISES

Exercise 1

Write a C++ Program that contains one user defined function month().

 In main() function:
o Read an integer input in between (1 to 12) and store it month_of_year.
o Call month(month_of_year)
 In month() function:
o Print the corresponding month of year in month().
o Example: Value of parameter is 4… Print “April”.

Exercise 2

Write a C++ Program that contains one user defined function cal_grades().

 In main() function:
o Prompt user to enter obtained(0 - 100) marks for one subject.
o Call cal_grades(marks_subject).
o Print the corresponding Grade with respect to Marks.
 In user defined function:
o Perform conditioning with else if statement return char value.
o Function must return value.

Exercise 3

Write a C++ Program that contains four user defined function(s) addition(), subtraction(), division(),
multiplication(). Develop a calculator as follows

 In main() function:
o A menu with choices addition,subtraction,division and multiplication must be displayed.
o Get two numbers and a choice from user
o Call the respective functions with user given number as parameter using switch statement
o Print the result from addition(), subtraction, division(), multiplication().
 In user defined functions:
o Plus and Minus function get two integer values and return integer.
o Multiply and Divide functions get two integer values and return float.

Department of Computer Sciences 4/4 Semester BSCS 01 & BS IT 02


CSL-113: Computer Programming Lab Lab 08: Function-I

You might also like