LabManual 06
LabManual 06
LAB MANUAL 06
CSC1102 Programming
Language 1
[EEE]
Prepared By:
M. Arifur Rahman
TITLE
A Brief Introduction to C Functions
PREREQUISITE
• To know about the main() function
• To be able to use printf() and scanf() function
• To know about C variables
• To know about C data types
• To know about C control structures
OBJECTIVE
• To know why C functions are used
• To be able to define and call C functions
• To be able to send and receive parameters in C function
• To know about scope rule of C function
• To know about function declaration and prototypes
• To know about call by value and call by reference
• To be able to understand the concept of recursion
• To be able to solve the exercises, lab work and assignments at the end of this manual
THEORY
FUNCTION
A function is a self-contained block of statements that perform a coherent task of some kind.
Every C program can be thought of as a collection of these functions. Any C program contains at
least one function. If a program contains only one function, it must be main( ). If a C program
contains more than one function, then one (and only one) of these functions must be main( ),
because program execution always begins with main( ). 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 each function has done its thing, control
returns to main( ). When main( ) runs out of function calls, the program ends.
The program below will help clarify this concept
#include<stdio.h>
void main()
{
swap();
}
void swap()
{
int a=10;
int b=20;
int t;
t=a;
a=b;
b=t;
2
printf("a= %d \n",a);
printf("b= %d",b);
}
#include<stdio.h>
void main()
{
int a=10;
int b=20;
swap(a,b);
printf("a= %d \n",a);
printf("b= %d",b);
}
void swap(int x,int y)
{
int t;
t=x;
x=y;
y=t;
printf("a= %d \n",x);
printf("b= %d \n",y);
}
3
SCOPE RULE OF FUNCTIONS
By default, the scope of a variable is local to the function in which it is defined.
The program below will help clarify this concept
#include<stdio.h>
void main()
{
int a=10;
int b=20;
swap(a,b);
}
void swap(int c,int d)
{
printf("a= %d \n",c);
printf("b= %d \n",d);
int t;
t=c;
c=d;
d=t;
printf("a= %d \n",c);
printf("b= %d",d);
}
#include<stdio.h>
void main()
{
float swap(float,float); //prototype declaration
float a=10.5;
float b=20.5;
float f;
swap(a,b);
}
FUNCTION CALL
C functions can be called in two ways. Such as:
• Call by value
• Call by reference
CALL BY VALUE
By now we are well familiar with how to call functions. But, if you observe carefully, whenever
we called a function and passed something to it we have always passed the ‘values’ of variables
to the called function. Such function calls are called ‘calls by value’. By this what we mean is, on
calling a function we are passing values of variables to it. In this method the ‘value’ of each of the
actual arguments in the calling function is copied into corresponding formal arguments of the
called function. Here the changes made to the formal arguments in the called function have no
effect on the values of actual arguments in the calling function.
If we want that the value of an actual argument should not get changed in the function being
called, pass the actual argument by value.
The program below will help clarify this concept
#include<stdio.h>
void main()
{
int a=10;
int b=20;
swap(a,b);
printf("a= %d \n",a);
printf("b= %d",b);
}
CALL BY REFERENCE
In case of call by reference the addresses of actual arguments in the calling function are copied
into formal arguments of the called function. This means that using these addresses we would
have an access to the actual arguments and hence we would be able to manipulate them.
If we want that the value of an actual argument should get changed in the function being called,
pass the actual argument by reference.
5
If a function is to be made to return more than one value at a time, then return these values
indirectly by using a call by reference.
#include<stdio.h>
void main()
{
int a=10;
int b=20;
swap(&a,&b);
printf("a= %d \n",a);
printf("b= %d",b);
}
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
printf("a= %d \n",*x);
printf("b= %d \n",*y);
}
RECURSION
In C, it is possible for the functions to call themselves. A function is called ‘recursive’ if a statement
within the body of a function calls the same function. Sometimes called ‘circular definition’,
recursion is thus the process of defining something in terms of itself.
The program below will help clarify this concept
#include<stdio.h>
void main()
{
swap();
}
void swap()
{
int a;
int b;
printf("\n");
printf("Enter Number 1: ");
scanf("%d",&a);
printf("Enter Number 2: ");
scanf("%d",&b);
printf("\n");
printf("After Swap: ");
printf("\n");
int t;
t=a;
a=b;
6
b=t;
printf("a= %d \n",a);
printf("b= %d",b);
swap();
}
EXERCISE
LAB WORK
I. Write a program that can add, subtract, multiply and divide two integer numbers. Hint:
Use the concept of functions
II. Write a program that can add, subtract, multiply and divide two integer numbers. Hint:
Use the concept of functions, parameters and calling function by value
III. Write a program that can add, subtract, multiply and divide two integer numbers. Hint:
Use the concept of functions, parameters and calling function by reference
IV. Write a program that can add, subtract, multiply and divide three integer numbers.
Send the third number as parameter from the main function to the other functions.
Hint: Use the concept of functions, parameters and scope rule of functions
V. Write a program that can add, subtract, multiply and divide two float numbers. Hint: Use
the concept of function declaration and prototypes
VI. Write a program that can ask user to input two numbers then choose from 4 options I.e.
addition, subtraction, multiplication and division. The program continues until the user
choose to exit from the program. Hint: Use the concept of functions, parameter and
recursion
VII. Write a program to calculate the factorial of a number. Input has to be taken from the
user. Hint: Use the concept of functions, parameter and recursion
ASSIGNMENT
I. Write a program that takes input from the user the no of subjects, mark and credit of
each subject and calculates CGPA. Hint: Use the concept of functions
7
II. Write a program that takes input from the user the no of subjects, mark and credit of
each subject and calculates CGPA. Hint: Use the concept of functions, parameter and
calling by value
III. Write a program that calculates the equivalent resistance of the following circuit where
R1=5 ohm, R2=3 ohm and R3=9 ohm. Hint: Use the concept of functions, parameters
and calling function by reference
IV. There are three resistors in a circuit. Resistor 1 has value 4 ohm. Resistor 2 has value 8
ohm and Resistor 3 has value 1 ohm. Write a program that stores the values of the
resistors in the main function but have to print the values from a separate function.
Hint: Use the concept of functions, parameters and scope rule of functions
V. Write a program that calculates the equivalent resistance of the following circuit where
R1=5.5 ohm, R2=3.7 ohm and R3=9.8 ohm. Hint: Use the concept of functions
declaration and prototypes
VI. Write a program that takes input from the user the no of subjects, mark and credit of
each subject and calculates CGPA. The program continues to calculate CGPA of 1 to n
students until the user quits the program. Hint: Use the concept of functions, parameter
and recursion.
8
Subject 1
Subject 2
Subject 3
……………
Subject n
VII. Write a program that can calculate the sum of a 5 digit positive number. Input has to be
taken from the user. Hint: Use the concept of function, parameter and recursion