Functions, prototyping
And parameter list

Objective: this lab Manual is to get hands-on experience of Functions, prototyping, and parameter list in C++. Get familiar with the syntax, Errors, and implementations.

Background: The function is a good mechanism to encapsulate code used repeatedly in a program so that it can be called from other parts of the code. A function does not use a keyword called function but instead, the programmer has to define a function prototype before the main function and then define the function again later. A function has the following format: type function_name (optional parameter list) { function code; return value; }

The return data type of function is in general the types of C++ variable types including int, double, char, etc. The function does some processing and the calculated value is returned using the return value.  In the main function or the other functions calling this function_name, the value returned is used like the instruction: calling_value = function_name (parameters);

A function does not need to always return a value. A function not returning a value can omit the return statement and the function type is void in this case.

Function prototype has the following format: type function_name (list of variable types); Examples are:

Example 1:  int compute_sum (int); Example 2: void tryout ();

Function prototypes differ from the function definitions in two places: there is no code (no {} with code in between) and the variable names do not follow the types. A function prototype can return nothing, in which case void is the type returned; also it may have no parameters at all like example 2 above. The function prototype is declared before the main function with the function calls inside the main function or the other functions calling this function.

 The function definitions are put after the main function.
C++ Standard Library: is a collection of  functions, which are written in the core language and part of the C++ ISO Standard itself to provides several generic functions that utilize and manipulate these containers, function objects, generic strings, and streams (including interactive and file I/O), support for some language features, and everyday functions for tasks such as finding the square root of a number. Using sqrt in <cmath> header file.

Pre-labs:

In order to do this lab, you should know how to write, compile, and run a C++ program, and you should understand what functions are and how to invoke them. You should also be familiar with an editor

Assessment tools:         
The assessment is according to the student participation in the lab if the student solves the exercise, he will get the participation mark for this lab.


Lab Assignment:
                                                            

Q1- Write a program in C++ to declare a prototype of a user defines a function to find a sum of 4 integers?

Q2- write a program in C++ to declare a global variable and take input and display on the console?’
           
Q3- Write a program in C++ to declare a prototype of user define a function to find multiply and subtraction of more than 2 floats and integers?
        
Q4- write a program in C++ to create a user-defined the function of the sum of two integers your program should take 2 numbers from the user and on calling the function your function will return the sum?

Q5- write a program in C++ to create a user-defined the function of Multiply of four integers your program should take 4 numbers from the user and on calling the function your function will return the Multiply?

Q6- write a program in C++ to create a user-defined function which only uses the global variable to take input in the main function and then change the input in the define function and display it in the main function?

Q7-Write a program in C++ to find the Size of fundamental data types.
Sample Output:
Find the Size of fundamental data types:
------------------------------------------
The sizeof(char) is : 1 bytes
The sizeof(short) is : 2 bytes
The sizeof(int) is : 4 bytes
The sizeof(long) is : 8 bytes
The sizeof(long long) is : 8 bytes
The sizeof(float) is : 4 bytes
The sizeof(double) is : 8 bytes
The sizeof(long double) is : 16 bytes
The sizeof(bool) is : 1 bytes

Q8-Write a program in C++ to swap two numbers.
Sample Output:
Swap two numbers :
-----------------------
Input 1st number: 25
Input 2nd number: 39
After swapping the 1st number is: 39
After swapping the 2nd number is: 25


Q9- Write a program in C++ to print alphabets from A-Z using the function. Your function should print all the alphabets?

Q10- Write a program in C++ to calculate the volume of a cube.
Sample Output:
Calculate the volume of a cube :
---------------------------------------
Input the side of a cube: 5 The volume of a cube is: 125

Q11- write a program in c++ to calculate the value of 2X^2+bx+c by using functions. Your function should compute the equation and then return the value to the main function?

Click for Assignment Submission:















3 Comments

  1. Muhammad Sheraz A1
    CMS 405646

    #include
    using namespace std;

    int add(int, int);
    int main()
    {
    int num1, num2, sum;
    cout<<"Enters 1st number : ";
    cin >> num1;
    cout<<"Enter 2nd number :";
    cin>>num2;


    sum = add(num1, num2);
    cout << "Sum = " << sum<<endl;
    system("pause");
    return 0;
    }

    int add(int a, int b)
    {
    int add;
    add = a + b;

    return add;
    }

    ReplyDelete
  2. #include
    using namespace std;


    int a=125;

    int main()
    {

    int a=150;
    cout<<"local a: "<<a<<" Global a: "<<::a;


    ::a=40;
    cout<<"\nlocal a: "<<a<<" Global a: "<<::a<<endl;
    system("pause");
    return 0;
    }

    ReplyDelete
  3. #include
    #include
    using namespace std;
    int main()
    {
    double number, squareRoot;
    cout << "Enter a number: ";
    cin >> number;

    squareRoot = sqrt(number);
    cout << "Square root of " << number << " = " << squareRoot;
    return 0;
    }

    aqeel ejaz

    ReplyDelete

Post a Comment

Previous Post Next Post