PF Lab 12

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Programming Fundamentals

Lab-12

For Students:

Roll No

Name

Class

Instructor

_________________________
Mr. Irfan Ullah

_____________________________________________________________________________1
28
Programming Fundamentals - Lab [COSC-1201]
12.1. OBJECTIVES

1. Functions
2. Types of Function
a) Built in Functions
b) User Defined Functions
3. How user defined functions work in c++
4. Declaring a user defined function
5. Defining a function
6. Passing Parameters to Functions
a) Pass by Value
b) Pass by Reference
7. Default Values in Parameters

12.2. Functions

Functions allow to structure programs in segments of code to perform individual tasks. In C++, a
function is a group of statements that is given a name, and which can be called from some point
of the program.

12.3. Types of Function

Depending on whether a function is predefined or created by programmer; there are two types of
function:

12.3.1. Library Function or Built in Functions

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

12.3.2. 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 the function is invoked from any part of program, it all executes the codes defined in the
body of function.

12.4. How User defined function works in C++

_____________________________________________________________________________1
29
Programming Fundamentals - Lab [COSC-1201]
When a program begins running, the system calls the main() function, that is, the system starts
executing codes from main() function.

When control of the program reaches to function_name() inside main(), it moves to void
function_name() and all codes inside void function_name() is executed.

Then, control of the program moves back to the main function where the code after the call to the
function_name() is executed as shown in figure above.

Example: A C++ program to display “Programming in C++ is fun” using function.

#include<iostream>
using namespace std;

void display()
{
cout<<"Programming in c++ is fun";
}

int main()
{
display();
}

12.5. Declaring a User Defined Function

Function declaration also known as function prototype provides information to the compiler
about the structure of function.
Function declaration is done as:
Return_type function _name (parameter_list);

_____________________________________________________________________________1
30
Programming Fundamentals - Lab [COSC-1201]
1. Return_type: Type of value that will be returned by function. For example, int is used as
a return type for functions that return integer value. Void is used as return type for
functions that do not return any value.
2. Function Name: Indicates the name of function. Rules for specifying a function name is
similar to the rules for declaring a variable.
3. Parameters: Parameters are the values that are provided to a function when the function
is called.

Note: You may have noticed that the return type of main is int, but in most cases main did not
actually return any value.
If the execution of main ends normally without encountering a return statement the compiler
assumes the function ends with an implicit return statement:
return 0; means that the program terminated successfully.
Note that this only applies to function main for historical reasons. All other functions with a
return type shall end with a proper return statement that includes a return value, even if this is
never used.

12.6. Passing Parameters to Functions


12.6.1. Pass by Value

A function declared with parameters, must be provided the parameters to execute. For example:
1. #include<iostream>
2. using namespace std;

3. void addition( int a, int b)


4. {
5. cout<< a+ b ;
6. }

7. int main()
8. {
9. addition(3,5);
10. }
The function addition, adds two values, 3 and 5 passed as parameters.
Parameters can also be passed using variables.

_____________________________________________________________________________1
31
Programming Fundamentals - Lab [COSC-1201]
1. #include<iostream>
2. using namespace std;

3. void addition( int a, int b)


4. {
5. cout<< a+ b ;
6. }

7. int main()
8. {
9. int x, y;
10. x = y = 3;
11. addition(x,y);
12. }

Copy of values of x and y are passed through function and stored in a and b. The function
addition here is not returning any value.
A function can return any value, if it has a return type. Values returned by functions must be
catched by the main method in any variable of the same datatype. For example:
#include<iostream>
using namespace std;

int add( int a, int b)


{
return a+ b;
}

int main()
{
int x, y;
x=y=4;
int z = add(x,y);
cout<< z;
}

Note: In pass by value, only copy of values of x and y are passed as parameters. Any
modification of these variables (x, y) within the function has no effect on the values of the
variables x and y outside it, because x and y were themselves not passed to the function on the
call, but only copies of their values at that moment.

12.6.2. Pass by Reference

_____________________________________________________________________________1
32
Programming Fundamentals - Lab [COSC-1201]
When a variable is passed by reference, what is passed is no longer a copy, but the variable itself.
Any modification on their corresponding local variables within the function are reflected in the
variables passed as arguments in the call.
To pass a parameter by reference, we use the following syntax:
#include<iostream>
using namespace std;

int add( int &a, int &b)


{
a +=2;
b +=2;
return a + b;
}

int main()
{
int x, y;
x=y=4;
int z = add(x,y);
cout<< z<<endl;

cout<<x <<endl<<y;
}

12.7. Default values in parameters

In C++, functions can also have optional parameters, for which no arguments are required in the
call, in such a way that, for example, a function with three parameters may be called with only
two. For this, the function shall include a default value for its last parameter, which is used by
the function when called with fewer arguments. For example:
// default values in functions

_____________________________________________________________________________1
33
Programming Fundamentals - Lab [COSC-1201]
#include <iostream>
using namespace std;

int divide (int a, int b=2)


{
int r;
r=a/b;
return (r);
}

int main ()
{
cout << divide (12) << '\n';
cout << divide (20,4) << '\n';
}

In this example, there are two calls to function divide. In the first one:
divide (12)
The call only passes one argument to the function, even though the function has two parameters.
In this case, the function assumes the second parameter to be 2 (notice the function definition,
which declares its second parameter as int b=2). Therefore, the result is 6.
In the second call:
divide (20,4)
The call passes two arguments to the function. Therefore, the default value for b (int b=2) is
ignored, and b takes the value passed as argument, that is 4, yielding a result of 5.

Note: Once default value is used for an argument, all subsequent arguments must have default
value.
// The function declaration below is Invalid because z has default value, but w after it doesn't
//have default value
int sum(int x, int y, int z=0, int w) // Invalid
int sum(int x, int y, int z=0, int w=0) // Valid

Note: If the function is returning a value, that value can be used by the main function in
following ways:
a. Output Statement
b. Assignment Statement
c. Arithmetic expression

_____________________________________________________________________________1
34
Programming Fundamentals - Lab [COSC-1201]
12.8. LAB TASKS
12.8.1. Program # 1

Write a program that inputs a number in main function and passes the number to a function. The
function displays the table of that number.

_____________________________________________________________________________1
35
Programming Fundamentals - Lab [COSC-1201]
12.8.2. Program # 2

Write a function that inputs two numbers and one arithmetic operator in main function, and
passes them (by reference) to a function. The function applies arithmetic operation on two
numbers on the basis of the operator entered by user.

_____________________________________________________________________________1
36
Programming Fundamentals - Lab [COSC-1201]
12.8.3. Program # 3
Write a program that inputs base and height of a triangle in main function and passes them to a
function. The function finds the area of triangle and returns it to main function where it is
displayed on screen. Area = ½(Base * height).

_____________________________________________________________________________1
37
Programming Fundamentals - Lab [COSC-1201]
12.8.4. Program # 4
Write a program that inputs two integers. It passes first integer to a function that calculates and
returns its square. It passes second integer to another function that calculates and returns its cube.
The main function adds both returned values and displays result.

_____________________________________________________________________________1
38
Programming Fundamentals - Lab [COSC-1201]
_____________________________________________________________________________1
39
Programming Fundamentals - Lab [COSC-1201]

You might also like