IT Solution Menu

N O T I C E

Project on Computer topics, Programs for practicals for BCA Students, and Mobile Android app requirements. Please pay less and get more immediate delivery for school and higher level. Thank You. Contact me - at +91-8800304018(w)

Monday, March 7, 2022

The Void Function , Older, Modern, Prototype and Multi file functions by Farrukh






 

Function, a unique concept of C/C++
Coded in TurboC
in easy steps.

by  Md. Farrukh Asif

 

Dear viewers, you will learn in detail about
Functions and its type
in C/C++.

This topic will cover about the “Functions”.  Details are below:

·       What is Function?

·       How to Define a Function?

·       What is the Built-in / Library Function?

·       What is User Defined Function?

·       What is Void Function?

·       What is Called by value Function?

·       What is Call by Reference Function?

·       What are Multi-call by value/reference Functions?

·       What are Multi File (separate file) Functions?

 


C Language:

C Language:

C for the beginner 's by Md Farrukh Asif

Operators in C by Md. Farrukh Asif

C's Control Flow by Md. Farrukh Asif

String handling in C by Md Farrukh Asif

The Function of C Language by Md. Farrukh Asif

File Handling with various statements/syntaxes in Programming and their output in C Language.

C for beginner's programming with output. Course Code – 106 [‘C’]) TMBU,BGP

BCA SOLVED EXAMINATION QUESTIONS WITH ANSWERS Course Code – 106 [‘C’]) TMBU,BGP

Now come to the “Topic”….

 ·       What is Function?

 A function is a self-contained program segment that carries out some specific, well-defined task.

·       How to define a Function?

A function definition has three principal components.

a.     The First Line.

b.     The Argument declared.

c.      The Body of the function.

We can define a function using data type. Like void, int, float, char, etc…

The first line of a function definition contains the type specification of the value returned by the function, followed by the function name, and (optional) a set of arguments, separated by a comma and enclosed in parenthesis.

In general terms, the first line can be written as,

Data-Type Name_of_Function(format argument 1, format argument 2,  format argument 3,…… format argument n)

Where the data type represents the data type of the value that is returned and the name represents the function name that can be defined by the user.

The format arguments allow information to be transferred from the calling portion of the program to the function. They are also known as parameters formal parenthesis.

The identifiers used as formal arguments are “Local”. in the sense that they are not recognized outside of the function.

Hence, the name of the formal arguments may be the same as the names of other identifiers that appear outside the function definition.

·       What are Built-in / Library Functions?

As we know there are a lot of built-in functions for the users and these are defined in the header files.

Suppose, if you are calling/using a clear screen, colors, wait for press any key, are the parts of <conio.h> header file.

For example:

            clrscr(), getch(), textcolor() etc.

All types of string functions like string comparing, copying, making upper case, lower case, etc are the parts of <string.h> header file.

            upper(), lower(),strcmp(), strcmpi(), strcpy(), strcat() etc.

All mathematics type of functions may be called by including <math.h> header file.

            sqrt(), sin(), cos(), log() etc.

·       What is User Defined Function?

When we define a function to perform a particular task by defining a set of identifiers, with a specific data type, the returned value as per user wish is called UDF (User Define Function).

It may be of two types, (1) No returned value( void type), and with returned value (prototype).

Example:

To convert an uppercase to lowercase with the help of user-defined function.

1.                    // This User Define Program is coded in

2.                    //Turbo for best performance...

3.                    #include<stdio.h>

4.                    #include<conio.h>

5.                    main()

6.                    {

7.                    char lower, upper;

8.                    char utolow(char upper); // This is Declaration part...

9.                    clrscr();

10.               printf("enter a upper case character");

11.               scarf("%c",&upper);

12.               lower=utolow(upper);

13.               printf("The Lower case is : %c",lower);

14.               getch(); //pausing screen

15.               }

16.               char utolow(char c1) //This is Definition part...

17.               {

18.               char c2;

19.               c2=(c1>='A' && c1<='Z')?(c1+32):c1;

20.               return(c2);

21.               }

22.               // Code ends here…

 

Many compilers support a more Comprehensive System for handling argument specification in function declaration and function definition. In particular, the proposed ANSI standard permits each of the argument data types within a function declaration to be followed by the argument's name that is …

Data-type name(type 1 argument 1, type 2 argument 2, type 3 argument 3, …… type n argument n)

Function declarations in this form are called function prototypes.

Their use is not mandatory in ‘C’ Function prototypes are desirable however because they further facilitate error checking between the calls to a function and the corresponding function definition.

 

Older Method

Newer Method

Int sample(int, int);

Float add(float, float);

Void myfunc(char, long, double);

Void demo(void);

Int sample(int a, int b);

Float add(float x, float y);

Void myfunc(char c1, long n, double d);

Void demo(void);

 

·       What is Void Function?

In this type of function, there is no need for a data type and argument in parenthesis. Function Declaration is a void type meaning no argument and no data type is declared and the function definition part also is a void type.

1.                       #include<stdio.h>

2.                       #include<conio.h>

3.                       main()

4.                       {

5.                       clrscr();

6.                       add(); // Function declared here as void (no data type & argument)…

7.                       getche();

8.                       }

9.                       add()     // Function definition with no data type and argument…

10.                  {

11.                  int a,b;

12.                  printf("Enter a value for a : ");

13.                  scanf("%d",&a);

14.                  printf("Enter a value for b : ");

15.                  scanf("%d",&b);

16.                  printf("The Sum is : %d",a+b);

17.                  //add();

18.                  }

 

·       What is Call by value Function?

This is older model declaration and definition of a function. This is an example of also called by value function.

Example 1:

Write a program for Addition by UDF.

int add(int, int); // Function declared here…

#include<stdio.h>

#include<conio.h>

main()

{

int a,b,s=0;

clrscr();

 

printf("Enter a value for a : ");

scanf("%d",&a);

printf("Enter a value for b : ");

scanf("%d",&b);

s=add(a,b);  // values are passed here to process…

printf("the Sum is : %d ",s);

getche();

}

int add(int a, int b)           // Definition of function that returns value

{                                              // to the function called section.

return (a+b);

}

 

Example 2:

Write a program for Addition by UDF.

 

// This is an illustration of call by values function(Prototype)

int add(int, int);   // Function Declaration....

#include<stdio.h>

#include<conio.h>

int add(int x, int y)

{

// Here X and Y is local variables for the called function of Prototype.

// and referenced by A and B.

return (x+y);

}

main()

{

int a,b,s=0;

clrscr();

printf("Enter a value for a : ");

scanf("%d",&a);

printf("Enter a value for b : ");

scanf("%d",&b);

s= add(a,b);    // Function Definition...

// Here A and B is local variable for Prototype...

printf("the Sum is : %d ",s);

getche();

}

// Code ends here…

 

 

This is an example of multi variables called by reference.

You can call the function either in a variable;e called ‘S’ and then print the sum, or you can be called it directly in printf() as mentioned in the program.

 

Example 3:

Write a program for Addition by UDF for multi variables that has to be called in the program.

 

1.              // Programm Coded in Turboc3 (Dos Box)

2.              // This is an illustration of call by values function(Prototype)

3.              int add(int*, int*, int*);   // Function Declaration for three variables....

4.              #include<stdio.h>

5.              #include<conio.h>

6.              int add(int *a, int *b, int *c)

7.              {

8.              // Here X and Y is local variables for called function of Prototype.

9.              // and referenced by A and B.

10.         return (*a+*b+*c);

11.         }

12.         main()

13.         {

14.         int a,b,c;

15.         clrscr();

 

16.         printf("Enter a value for a : ");

17.         scanf("%d",&a);

18.         printf("Enter a value for b : ");

19.         scanf("%d",&b);

20.         printf("Enter a value for c : ");

21.         scanf("%d",&c);

 

22.         //add(&a,&b,&c);    // Function Definition...

23.         // Here A ,B and C are local variable for Prototype ...

24.         printf("the Sum is : %d ",add(&a,&b,&c));           // Function Definition...

25.         // you can use this statement directly…

26.         getche();

27.         }

28.         // Code ends here…

 

·       What are Multi File (separate file) Functions?

There is a type of Function which can be stored in separate file and called it as per requirement, is known as “Multi File Function”.

You can write the declarative part in a file called “File1.c” and definition part in a separate file called “File2.c” now compile and call it together with.

Here we will use an “extern” keyword for both the stored/saved files.

When it is executed, it shows the result.

Write a program for Addition by UDF for multi variables that has to be called in the prigram.

 

 

Example 1:

First file is saved with the name “ F1.c

 

1.     // Code written for the Turboc

2.     // Multi file calling function…

3.     #include<stdio.h>

4.     /* Simple Multifile program to write "Hello, My dear viewers!" */

5.     extern void output(void);

6.     /* External Function Declaration */

7.     main()

8.     {

9.     output();

10.                        return;

11.                        }

12.                        // Code ends here…

 

 

Second file is saved with the name “ F2.c

 

1.     //Code written for the Turboc

2.     // Multi file calling function…

3.     #include<stdio.h>

4.     extern void output(void)

5.     /* External Function Definition */

6.     {

7.     printf("Hello, My Dear Viewers!/n you are watching me.");

8.     return;

9.     }

10.                        //Code ends here…

 

How to compile and run together with...?

Answer is here:

Follow these steps if you are in Turboc (DosBox) for Windows.

1.    Open TC.exe.

2.    From project select Open Project.

3.    Enter the name of project eg: MyProj. prj and Press ok.

4.    From project select Add item.

5.    Locate all the source files and add them.

6.    compile and build.

Yes, you have dun it.

 

Follow these steps if you are using Unix OS.

1.    Write a program and save it “f1.c” &  f2.c”on the Desktop using (Dos Shell) command prompt of TC.

  1. Then type gcc f1.c f2.c
  2. Then use the command, “Combine” to make and executable file, like this

Combine f1 f2 <output mode> <new file name>

4.    Combine f1 f2 -o  f1_f2

5.    -o creates a new file called f1_f2 which is an executable file.

  1. Now, type the file name only to execute the program.

From the project select Add


Note: Download a content of First Semester Communication of Networking


See the full source code on my Blogger

NCERT Solution Click Me

 

*** Thanks for watching me. ***

 


No comments:

Post a Comment

Top Popular Post