Monday, 28 December 2020

Functions in Computer Programming? (Example In C)

 

            Functions in Computer Programming

Objectives:

  To familiarize students with the function declaration, definition, prototypes, and function call. Also to show values passing as call by value and call by reference. 

Tools:

Turbo C IDE  

Procedure:

 Writing a C Program which will perform the following tasks
 State a function which receives a float and an int from main(), finds the
   product of these two and returns the product which is printed through main().

The function is a block of statements and is calling for specific tasks to be performed in a program. Through function, we can divide the whole program in a module, and thus it will be very easy to deal with modules or small pieces of programs. Any C program contains at least one function, which is main() function. There is no limit on the number of functions that might be present in a C program. Each function in a program is called in the sequence specified by the function calls in main(). After performing tasks by each function, control returns to the main(). The function cannot be defined inside another function

Every function in C programming language should be declared before they are used. These type of declaration are also called function prototype. Function prototype gives compiler information about function name, type of arguments to be passed and return type. The syntax of function prototype is as follow:
Return-type <function-name> (type(1) argument(1),....,type(n) argument(n)); For example:
     int add(int a, int b);
In the above example, int add(int a, int b); is a function prototype which provides following information to the compiler: 
       return type of the function is int. 
       name of the function is add() 
       two arguments of type int are passed to function. 

Function prototype is not needed if user-definition function is written before main() function.
For a function to perform specific tasks, it must be called or invoked which is called a function call. The function can be called or invoked using the following syntax:
function-name (argument(1),….argument(n));
When a function is invoked using the above syntax, control of the program jump from that statement to function definition and execute the code inside that function.

The function definition contains programming codes to perform specific tasks. The syntax of function definition can be as follows:


return-type function-name
{
                          Block of statements;
}

Call by value is used when called function does not need to modify the value of the caller’s original variable. In this case, a copy of the variable is passed to the function and when the function changes its values then it has no effect on the value of the variable in the main function as shown below:

main function

 
The output of variables a and b won’t change and will be printed as a = 10 and b = 20.

Call by reference means that the compiler will not create a local copy of the variable which you are referencing to. It will get access to the memory where the variable saved. When you are doing any operations with a referenced variable you can change the value of the variable. The address is passed by using the symbol “&” and is received by using the asterisk symbol “*” shown as:

main program and output



The output of variables and changes because it was passed by reference. 

Step 1: Create a New C Program File and save it as lab7.c
Step 2: Write the following code as shown in figures below

c program
 

Figure 20 (a): Product of float and integer using the function

c program
 

Figure 20 (b): Product of float and integer using the function

 Step 3:Save file again, compile and run it for required output as shown in the figure below
   

output

Figure 21: Product of float and integer using the function

No comments:

Post a Comment