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

Lab Manual # 13: Title: Functions

This document is a lab manual for programming fundamentals that discusses functions in C++. It covers library functions, which are predefined functions that can be used by including header files. It also discusses user-defined functions, which are created by the programmer and consist of a declaration, definition, and calls. The document provides examples of using library and user-defined functions and explains concepts like function prototypes, parameters, return types, and return statements. It includes three programming problems to test comprehension of functions.

Uploaded by

Usama Saghar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Lab Manual # 13: Title: Functions

This document is a lab manual for programming fundamentals that discusses functions in C++. It covers library functions, which are predefined functions that can be used by including header files. It also discusses user-defined functions, which are created by the programmer and consist of a declaration, definition, and calls. The document provides examples of using library and user-defined functions and explains concepts like function prototypes, parameters, return types, and return statements. It includes three programming problems to test comprehension of functions.

Uploaded by

Usama Saghar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab # 13 – Programming Fundamentals (CPE-121)

Lab Manual # 13

Title: Functions

CLO: CLO-1

Student Name: Class Roll:

Subject Teacher : Engr. Muhammad Baqer


Lab Engineer : Engr. Abdul Rehman
Lab Performed In : Networking Lab (CPED)

LAB POINTS SCORE


Lab Performance [10] 0 1 2 3 4 5
Lab Participation
Lab Activity Completion on the Same Day

Lab Submission[10] 0 1 2 3 4 5
Completeness & Correctness
Required Conclusion & Results

No of Checks
SUB TOTAL
TOTAL SCORE

______________________
Course Instructor / Lab Engineer

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 13 – Programming Fundamentals (CPE-121)
In programming, function refers to a segment that groups code to perform a specific task.
Depending on whether a function is predefined or created by programmer; there are two types
of function:
1. Library Function
2. User-defined Function

Library Function in C++


Library functions are the built-in function in C++ programming. Programmer can use library
function by invoking function directly; they don't need to write it themselves.

Example 1: Library Function

# include <iostream>
#include <cmath>
using namespace std;
int main() {
double number, squareRoot;
cout<<"Enter a number: ";
cin>>number;
/* sqrt() is a library function to calculate square root */
squareRoot = sqrt(number);
cout<<"Square root of "<<number<<" = "<<squareRoot;
return 0;
}

Some Other Built-in Function Of <cmath> Library


1. abs(int) computes absolute value of an integral value (|x|)
2. div(int) computes quotient and remainder of integer division
3. remainder signed remainder of the division operation
4. fmax larger of two floating point values 
5. fmin smaller of two floating point values
6. pow raises a number to the given power (xy) 
7. sin computes sine (sin(x)) 

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 13 – Programming Fundamentals (CPE-121)
In example above, sqrt() library function is invoked to calculate the square root of a number.
Notice code #include <cmath> in the above program. Here, cmath is a header file. The function
definition of sqrt()(body of that function) is written in that file. You can use all functions defined
in cmath when you include the content of file cmath in this program using #include <cmath> .

User-defined Function

C++ allows programmer to define their own function. A user-defined function groups code to
perform a specific task and that group of code is given a name(identifier). When that function is
invoked from any part of program, it all executes the codes defined in the body of function.

A user defined function consists of the following

 Function Declaration
 Function Definition
 Function Calling

Consider the figure above. When a program begins running, the system calls
the main()function, that is, the system starts executing codes from main() function. When
control of program reaches to function_name() inside main(), the control of program moves
tovoid function_name(), all codes inside void function_name() is executed and control of
program moves to code right after function_name() inside main() as shown in figure above.

Example 1: User Define Function

# include <iostream>
usingnamespacestd;

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 13 – Programming Fundamentals (CPE-121)

int add(int,int);//Function prototype(declaration)

int main(){
int num1, num2, sum;
cout<<"Enters two numbers to add: ";
cin>>num1>>num2;
sum= add(num1,num2);//Function call
cout<<"Sum = "<<sum;
return0;
}

int add(inta,int b){//Function declarator


int add;
add=a+b;
return add;//Return statement
}

Function prototype(declaration)

If an user-defined function is defined after main() function, compiler will show error. It is


because compiler is unaware of user-defined function, types of argument passed to function
and return type.In C++, function prototype is a declaration of function without function body to
give compiler information about user-defined function. Function prototype in above example:

int add(int, int);

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 13 – Programming Fundamentals (CPE-121)
You can see that, there is no body of function in prototype. Also there are only return type of
arguments but no arguments. You can also declare function prototype as below but it's not
necessary to write arguments.

int add(int a, int b);

Function Call

To execute the codes of function body, the user-defined function needs to be invoked(called).
In the above program, add(num1,num2); inside main() function calls the user-defined function.
In the above program, user-defined function returns an integer which is stored in variable add.

Function Definition

The function itself is referred as function definition. Function definition in the above program:

int add(inta,int b) { // Function definitiondeclarator

int add;

add = a+b;

return add; // Return statement

Return Statement

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 13 – Programming Fundamentals (CPE-121)
P-1: Write a program which input principal, rate and time from user and calculate
compound interest. You can use library function. CI = P(1+R/100)T  - P
Your Code:

Paste your output here

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 13 – Programming Fundamentals (CPE-121)
P-2 Write a program which display a number between 10 to 100 randomly.
Your Code:

Paste your output here

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 13 – Programming Fundamentals (CPE-121)
P-3 Raising a number to a power p is the same as multiplying n by itself p times. Write a
function called power that takes two arguments, a double value for n and an int value
for p, and return the result as double value.Write the main function that gets value from
the user to test power function
Your Code:

Paste your output here

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 13 – Programming Fundamentals (CPE-121)
Comments:

Version 1.0 Department of Computer Engineering UCE&T BZU Multan

You might also like