Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Friday, 11 March 2022

45.Functions

 

Functions:

To develop a complex and big application, the total application has to divide into small and easily manageable parts called modules.

A module is a part or unit of total application.

In C language a module is defined using function.

C language supports modularity using functions so is called modular programming language.

 

Definition:

Function is a self-contained block of code that performs a particular task. It is capable to communicate with other units of application.

 

Advantages:

A complex application can be divided into number of simple and easily manageable parts called functions.

It is easy to develop, test and debug a small function.

It improves the maintainability of application because a problem (bug) in a function can be easily detected and modified.

It improves the reusability because the same function can be reused in different places.

It reduces the application development time because multiple modules can be developed at a time and then integrated as a single application.

It reduces the required memory to run the application.

 

Foundation to functions: (7 steps to functions)

The following 7 points must be kept in mind while working with functions.

 

Point 1. Undefined variable can’t be used with in a function.

 

Example:

#include<stdio.h>

void main()

{

int a,b;

clrscr();

a=20;

b=25;

c=a+b;

printf("Sum of two numbers %d",c);

getch();

}

Output:

Error: Undefined symbol "c" in function main()

 

Example explained:

Variable "c" is not declared with in main() which results error.

 

 

Point 2. Multiple variables can’t be declared with the same name with in a function.

 

Example:

#include<stdio.h>

void main()

{

int x=25;

float x=12.25;

clrscr();

printf("x=%d",x);

printf("\nx=%f",x);

getch();

}

Output:

Error: Re declaration of "x" in function main.

 

Example explained:

Here "x" is declared for two times with in a single function main() which results error.

 

Point 3. If a variable is not assigned with any value then it is initialized with an unknown value called garbage value.

 

Example:

#include<stdio.h>

void main()

{

int x;

clrscr();

printf("%d",x);

getch();

}

Output:

26500

 

Example explained:

It is garbage value differs from computer to computer and execution to execution.

 

Point 4. A function is a self-contained block of code that performs a particular task. It has a name which must be a valid identifier.

A C program may have any number of functions but defining of main() function is mandatory because execution of any C program starts and completes from the main()

Functions other than the main() are called sub functions.

Sub functions are not executed unless we call them from the main() directly or indirectly.

 

 

Example1:

#include<stdio.h>

main()

{

     clrscr();

     printf("Hello world");

     getch();

}

first()

{

    printf("\nNice World");

}

 

Output:

Hello world

 

Example explained:

The function first() is not executed because it is not called from the main().

 

Example2:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Output:

One

Three

Two

 

Example explained:

The function first() is called from the main.

 

 

 

 

Example3:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Output:

One

Three

Five

Four

Two

 

Example explained:

Function first() is called directly from the main()

Function second() is called from the main() indirectly that is through the first()

 

Example4:

#include<stdio.h>

main()

{

    int i;

    clrscr();

    for(i=1;i<=5;i++)

        hello();

    getch(); 

}             

hello()

{

     printf("\nHello World");

}

Output:

Hello World

Hello World

Hello World

Hello World

Hello World

 

Example explained:

The function hello() is called for 5 times.

0 comments:

Post a Comment