Chapter - 6, Functions, HSSC-II (12th Class) Computer Science Key Point Notes

Chapter - 6, Functions, HSSC-II (12th Class) Computer Science Key Point Notes

12th Class (HSSC-II) Computer SLO Based Key Point Notes

(National Book Foundation - As Federal Textbook Board, Islamabad 
Based on National Curriculum Pakistan 2023-2024 and Onward prescribed by Federal Board of Intermediate and Secondary Education, Islamabad, and All Pakistan Boards) 

{Contact WhatsApp 03339719149 for Class-XII Computer Science Complete SLO-Based Key Points notes in pdf format as well as in PowerPoint Presentation for preparing/delivering the Lectures}

************************************

👉👉👉 Computer Science 12th Class Notes (Main Page)

************************************

Unit 6: Functions

6.1 Functions

Why do we need functions?

  • When solving large problems it is usually necessary to split the problem down into a series of sub-problems, which in turn may be split into further sub-problems etc. This is usually called a top-down approach. i.e. Divide the problem and Conquer the problem.
  • This process continues until problems become of such a size that they can be solved very easily.
  • This top-down approach is essential if the work has to be shared between a team of programmers, each programmer assigns a part of the problem to solve by writing separate code or function for that.
  • While writing a single function the programmer is able to concentrate on the solution of this one problem only and is thus more likely to be able to solve the problem and make less errors. This function can now be tested on its own for correctness.
  • C programs are made more flexible through the use of functions.

Function

  • A function is a group or set of statements that is designed to perform a specific task.
  • Functions are written once but can be used at several points.
  • The function enables the programmer to develop programs for complex problems by dividing them into a number of tasks.
  • Functions are independent of one another.


6.1.1 Types of function in C++

Library or Built-in functions:

Ready-made or predefined functions provided by C++ are known as built-in functions or library functions. These functions are stored in different header files. Examples:

            printf(), scanf(), getch(), clrscr(), strcpy(), etc.

User-defined functions:

The functions developed by or written by the programmer are known as User-defined functions. 

6.1.2 Advantages of using Functions

  • A program can be defined in logical blocks which will make the code clear and easy to understand.
  • It avoids typing the same pieces of code multiple times.
  • Individual functions can be easily tested.
  • The user can modify only the function without changing the structure of the program.
  • It is easy to change or update a code in a function, which needs to be done once.

6.1.3 Function Signature

6.1.4 Function Components

Each function consists of three components:
  1. Function Prototype or Function Declaration or Function Signature.
  2. Function Definition
  3. Function Call

1, Function declaration or Prototype or Signature

The function declaration ( or Prototype) tells the compiler the name of the function, the data type the function returns ( if any ), and the number and data types of the function’s arguments ( if any ). i.e.

Return_type Function_name ( parameters/Arguments );

Return_type: It indicates the type of value that will be returned by the function.
Function_name: It indicates the name of the function.
Parameters/Arguments: Parameters are the values that are provided to a function when it needs to execute.

Example:

            int myFunction(int a, float b);

  • If the function returns no value, the keyword “void” is used. i.e.
            void myFunction(int a, float b);

  • If there is no parameter, empty parentheses are used or keyword “void” is written in the parentheses. i.e. 
        void myFunction(); or void myFunction(void);
  • Just like a variable, a function must also be declared before its usage.

2. Function Definition

A set of statements that explains what a function does is called function definition.
A function definition consists of two parts:
  1. Function header or function declaration or function prototype.
  2. Function Body which appears after the function declarator and the statements are written in curly braces.
Function declaration is not required if the function definition
is written before main().
Function declaration is compulsory if the function definition is
written after the main().

3. Function Call

The statement that activates a function for execution is known as a function call. A function is called with its name followed by the brackets. The bracket tells the compiler that you are referring to a function and not to a variable. It will end with a semicolon as it is an executable statement.

When a function is called, the following steps take place:
  1. The control moves to the function that is called.
  2. All statements in the function body are executed.
  3. The control returns to the statement following the function call

EXAMPLE # 1

#include<iostream.h>
#include<conio.h>

void myfunction( );
// Function Prototype or function declaration or
                            function header

void main()
{
        clrscr( );
        int i = 10;
        cout<<“Function Example”;
        myfunction();
// Function Call
        myfunction(); // Function Call
        myfunction(); // Function Call
        getch();
}
void myfunction( ) // Function Definition
{
        cout<<“Function is called from main function \n”;
        cout<<“Bye\n”;
}

The above can also be written like this

#include<iostream.h>
#include<conio.h>

void myfunction( )
// Function Definition and no need for function                                         Prototype.
{
        cout<<“Function is called from main function \n”;
        cout<<“Bye\n”;
}
void main()
{
        clrscr( );
        int i = 10;
        cout<<“Function Example \n”;
        myfunction(); // Function Call
        myfunction(); // Function Call
        myfunction(); // Function Call
        getch();
}


Example 2

Write a program that inputs two numbers in the main function, and passes these numbers to a function. The function displays the maximum number.

#include<iostream.h>
#include<conio.h>
void max(int a, int b);         
// Function Declaration
void main()
{
        clrscr();
        int x, y;
        cout<<"Enter a value: ";
        cin>>x;
        cout<<"Enter a value: ";
        cin>>y;
        max(x,y);         // Function Call
        getch();
}

void max(int a, int b)        
// Function Definition

        if(a>b)
                    cout<<"Maximum number is “<<a;
        else
                    cout<<"Maximum number is “<<b;
}


Functions that return a value

  • A function can return a single value. 
  • The return type in the function declaration indicates the type of value returned by a function.
  • If a function returns no value, the keyword “void” is used as the return type.
  • The “return” statement is used to return a value from a function. i.e. return constant/variable/expression;

Example 3

Write a program that inputs a number and passes it to a function that returns the factorial of a number?

#include<iostream.h>
#include<conio.h>

int factorial(int n)     //Function definition.
{
        int fact=1;
        for(int i=1; i<=n; i++)
        fact = fact * i;

        return fact;     //Return a value to a calling function.
}

void main()

{
        clrscr();
        int x;
        cout<<"Enter a value: ";
        cin>>x;
        cout<<"The Factorial of a entered number:     <<factorial(x) ; //function call
        getch();
}


6.1.5 Scope of Variable in Functions

Scope means that in which parts of the same program the variable can be accessed. There are three scopes of variables:
  1. Local scope
  2. Global scope
  3. Static scope

1. Local/Automatic Variable

Variables declared within a body of function are called local variables. These variables are not accessible from outside the body of function and thus their visibility remains only to that function. 

Example:

#include <iostream.h>
#include <conio.h>
void myfunction(void)
{
        int c=10, e=10, a=33;
}
void main()
{
        clrscr();
        int a=10, b=30, d=90;
        getch();
}


2. Global Variables

Variables declared at the top of a program before the main() function are called global Variables. • These variables are accessible by all the  functions of the same program.

Example:

#include <iostream.h>
#include <conio.h>
int x=2;
void myfunction()
{
        x= x*4;
        cout<<x;
}
void main()
{
        clsrcr();
        x = x + 5;
        cout<<x<<endl;
        myfunction();
        getch();
}


3. Static Varaible

  • Those variables that have the capability to preserve information about the last value are called a static variable.
  • To declare a variable static, the keyword static is sed i.e.

        static data_type variable_name = initial value;

  • Static variables are initialized once in the program and remains in memory until the end of the program.
  • Static variables are local in scope to their module or function in which they are defined.

Example

#include <iostream.h>
#include <conio.h>
void demo();
void main()
{
        int i=0;
        while (i < 3)
        {
        demo();
        i++;
        }
        getch();
}
void demo()
{
        int var1 = 0;
        static int var2 = 0;
        cout<<var1<<endl;
        cout<<var2<<endl;
        var1++;
        var2++;
}



6.1.6 Parameters

The variables and values written in the parenthesis of function declaration and function call are called parameters. It is also called arguments. There are two types of parameters i.e.:

1. Formal Parameters

Those parameters which appear in a function declaration or prototypes are called formal parameters.

2. Actual Parameters

Those parameters which appear in the function call are actual parameters.


Inline Function

  • The functions whose declaration is preceded by an inline keyword are called inline functions.
  • In the inline function, the compiler replaces all the function calls with a copy of the contents of the function itself during the compilation process.
  • The inline keyword requests the compiler to treat the function as an inline and not jump again and again from calling function to called function and vice versa to save the CPU time.
  • The disadvantage of the inline function is that it makes the complied code quite larger, especially when the inline function is long and is called for many times.

inline return_type  ( parameters );
inline float myfunction (int a , float b);


Example

#include<iostream.h>
#include<conio.h>

inline void max(int a, int b)    // Inline Function
{
        if(a>b)
                cout<<"Maximum number is “<<a;
        else
                cout<<"Maximum number is “<<b;
}
void main()
{
        clrscr();
        int x, y;
        cout<<"Enter a value: ";
        cin>>x;
        cout<<"Enter a value: ";
        cin>>y;
        max(x,y);     // Function Call
        getch();
}


6.2.1 Passing Arguments

Following are the different methods of passing arguments to the calling function:
  1. Passing arguments by constants
  2. Passing arguments by values
  3. Passing arguments by reference

1. Passing arguments by constants

In this method, the actual constant values are passed to the calling function.
Example

#include <iostream.h>
#include <conio.h>
void show(int x)
{
        cout<<“x = “<<x;
}
void main()
{
        clrscr();
        show(49);
        getch();
}



2. Passing arguments by values

In this method, a copy of arguments (i.e. copy of the variable’s value) is passed to the calling function.

Example

#include <iostream.h>
#include <conio.h>
void add(int x, int y)
{
        cout<<“sum of values is “<<x+y;
}
void main()
{
        clrscr();
        int a, b;
        cout<<“Enter a value “;
        cin>>a;
        cout<<“Enter a value “;
        cin>>b;
        add(a, b);
        getch();
}



3. Passing arguments by reference

• In this method, the reference of the function parameters are passed instead variable’s values.
• Using this method, the called function can use or modify the value of arguments using its reference.
• To use this method, the actual variable is preceded by the ampersand sign (&) during the function declaration.

Example

#include <iostream.h>
#include <conio.h>
void add(int &x)
{
        x++;
}
void main()
{
        clrscr();
        int y = 10;
        cout<<“value of y before calling function is “<<y<<endl;
        add(y);
        cout<<“value of y after calling function is “<<y<<endl;
        getch();
}


6.2.2 Default Arguments

  • A default argument is a value provided in function declaration that is automatically assigned by the compiler if the caller of the function doesn’t provide a value for the argument in function call.
  • If a value is not passed when the function is called, the default value is used.
  • If a value is passed then the default value is ignored and the passed value is used.

General syntax:

Return_type Function_name (parameter1=value, ……);

Example

#include <iostream.h>
#include <conio.h>
int divide(int x, int y=2)
{
        return x/y;
}
void main()
{
        clrscr();
        int a, b;
        cout<<“Enter a value “;
        cin>>a;
        cout<<“Enter a value “;
        cin>>b;
        cout<<divide(a,b)<<endl;
        cout<<divide(a)<<endl;
        getch();
}

6.3 Function Overloading

• It is a feature of C++ that allows to creation of multiple functions with the same name but have different numbers or types of parameters.

Example

#include <iostream.h>
#include <conio.h>
int multiply(int x, int y)
{
        return x*y;
}
float multiply(float x, float y)
{
        return x*y;
}
void main()
{
        clrscr();
        cout<<“integer multiplication “<<multiply(4,5)<<endl;
        cout<<“float multiplication “<<multiply(3.5,4.5)<<endl;
        getch();
}



6.3.1 Advantages of Function Overloading

  • The programmer can declare multiple functions with the same name that have slightly different purposes.
  •  It can significantly lower the complexity of programs.
  • As multiple functions have the same name, therefore, remembering them is easier as compared to remembering more names.
  • It increases the readability of the program.
  • It exhibits the behavior of polymorphism.

6.3.2 How a function can be overloaded?

A function can be overloaded with the following methods:

1• Number of Arguments:

  • Functions can be overloaded if they have different numbers of parameters.
  • More than one function with the same name but a different number of parameters can be used in a single program.
  • The compiler disambiguates the calls by looking at the number of actual parameters in the function call and formal parameters in the function declaration.

2• Data types of Arguments:

  • Function overloading is achieved by defining multiple functions with the same name and number of parameters but of different data types.
  • The compiler looks at the type of arguments and calls the corresponding function.

3• Return Type:

  • The return type of a function is not considered when the function is overloaded.
  • If two or more functions with the same name have the same number of parameters of the same data type but a different return type, then the compiler will generate an error message.
Example

#include <iostream.h>
#include <conio.h>
void print (int a)
{
        cout<<“A = “<<a<<endl;
}
void print (float a)
{
        cout<<“A = “<<a<<endl;
}
int addition(int a, int b, int c)
{
        retrun (a+b+c);
}
Int addition(int a , int b)
{
        return (a+b);
}
Void main()
}
        clrscr();
        print(6);
        print(6.5);
        addition(4,5,6);
        addition(4,5);
        getch();
}



************************************
👉👉👉 Computer Science 12th Class Notes (Main Page)

************************************


************************************
Shortcut Links For:

1.  5th Class All Subjects Notes

2.  8th Class All Subjects Notes

3.  Easy English Grammar Notes



1. Website for School and College Level Physics   
2. Website for School and College Level Mathematics  
3. Website for Single National Curriculum Pakistan - All Subjects Notes 

© 2023 & onwards Academic Skills and Knowledge (ASK  

Note:  Write me in the comments box below for any query and also Share this information with your class-fellows and friends.

Post a Comment

0 Comments

cwebp -q 80 image.png -o image.webp