0% found this document useful (0 votes)
5 views

LabManual 06

This lab manual provides an introduction to C functions, covering their definition, usage, and importance in programming. It explains concepts such as function declaration, passing values (call by value and call by reference), and recursion, along with practical examples. Additionally, it includes exercises and assignments to reinforce the understanding of these concepts.

Uploaded by

c.sunjid707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

LabManual 06

This lab manual provides an introduction to C functions, covering their definition, usage, and importance in programming. It explains concepts such as function declaration, passing values (call by value and call by reference), and recursion, along with practical examples. Additionally, it includes exercises and assignments to reinforce the understanding of these concepts.

Uploaded by

c.sunjid707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

AMERICAN INTERNATIONAL UNIVERSITY-BANGLADESH (AIUB)

FACULTY OF SCIENCE & INFORMATION TECHNOLOGY


DEPARTMENT OF COMPUTER SCIENCE

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);
}

WHY USE FUNCTION


Writing functions avoids rewriting the same code over and over. Suppose you have a section of
code in your program that calculates area of a triangle. If later in the program you want to
calculate the area of a different triangle, you won’t like it if you are required to write the same
instructions all over again. Instead, you would prefer to jump to a ‘section of code’ that calculates
area and then jump back to the place from where you left off. This section of code is nothing but
a function.
Also by using functions it becomes easier to write programs and keep track of what they are
doing. If the operation of a program can be divided into separate activities, and each activity
placed in a different function, then each could be written and checked more or less
independently. Separating the code into modular functions also makes the program easier to
design and understand.

PASSING VALUES BETWEEN FUNCTIONS


The functions that we have used so far haven’t been very flexible. We call them and they do what
they are designed to do. We haven’t been able to influence the functions in the way they carry
out their tasks. It would be nice to have a little more control over what functions do. In short,
now we want to communicate between the ‘calling’ and the ‘called’ functions.
The mechanism used to convey information to the function is the ‘argument’. You have
unknowingly used the arguments in the printf( ) and scanf( ) functions; the format string and the
list of variables used inside the parentheses in these functions are arguments. The arguments are
sometimes also called ‘parameters’.
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);
}
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);
}

FUNCTION DECLARATION AND PROTOTYPES


Any C function by default returns an int value. More specifically, whenever a call is made to a
function, the compiler assumes that this function would return a value of the type int. If we desire
that a function should return a value other than an int, then it is necessary to explicitly mention
so in the calling function as well as in the called function.
The program below will help clarify this concept

#include<stdio.h>
void main()
{
float swap(float,float); //prototype declaration
float a=10.5;
float b=20.5;
float f;
swap(a,b);
}

float swap(float c,float d)


{
printf("a= %f \n",c);
printf("b= %f \n",d);
float t;
t=c;
c=d;
d=t;
printf("a= %f \n",c);
printf("b= %f",d);
4
return 0;
}

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);
}

void swap(int x,int y)


{
int t;
t=x;
x=y;
y=t;
printf("a= %d \n",x);
printf("b= %d \n",y);
}

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.

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);
}
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

I. A function is a self-contained block of statements that perform a ______________ task


of some kind.
II. C functions can be called in two ways namely and .
III. If a program contains only one function, it must be .
IV. There is no limit on the number of that might be present in a C program.
V. Any C function by default returns an value.

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

Subject Mark Credit


Subject 1
Subject 2
Subject 3
……………
Subject n

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

Subject Mark Credit


Subject 1
Subject 2
Subject 3
……………
Subject n

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.

Subject Mark Credit

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

You might also like