Lab 12
Lab 12
Date: 17/9/2021
Objective:
1. Passing Argument by reference
2. Recursive Function
A function’s parameter that receives the location (memory address) of the corresponding actual
variables is said passing the arguments by reference.
When we attach * (star) after the arg_type in the parameter list of a function, then the variable
following that arg_type is passed by reference
It stores the address of the actual variable, NOT the value. During program execution to manipulate
the data, the address stored will direct control to the memory space of the actual variable
Syntax
1 In function protoype and function definition, put the * (star) after the data type
Example function prototype:
void inputmarks (float*, float*);
2 In function call, put the &(ampersand) before the argument name to be passed by reference
Example calling the above function
Inputmarks(&mark1, &mark2);
3. Recursive Function.
Recursive function is a function that calls itself. We usually use a recursive function when we have a
series of same job to be completed. As an example solving a Fibonacci series.
f( 3 )
return f( 2 ) + f( 1 )
return f( 1 ) + f( 0 ) return 1
return 1 return 0
Question1:
Example of functions that “return” more than one value. To pass the address of the
arguments. Type, save and compile the below program.
#include <stdio.h>
void readmarks(float*, float*); //the prototype of function that pass by reference
float calc_avg(float, float); //the prototype of calc_avg function
void print(float); //the prototype of print function
int main(void)
{
float marks1, marks2, avg;
void readmarks(&marks1, &marks2); //calling function readmarks, pass address of marks1 &
marks2
avg = calc_avg(marks1, marks2);
print(avg);
return 0;
}
void read_marks(float *m1, float *m2)
{
printf("Enter marks for test1 and test2 : ");
scanf("%f %f", m1,m2); //notice no &, this is because m1 and m2 is already adresses
}
float calc_avg(float m1, float m2)
{
return((m1 + m2)/2); //do the calculation and return the answer
}
void print(float average)
{
printf("\nAverage marks are :%.2f\n",average);
}
#include <stdio.h>
void fun1(int, int*); //function prototype
int main(void)
{
int a=5, b=10; //
printf("Before fun 1\n“);
printf(" a = %d b = %d“,a, b);
fun1(a, &b); //function call, passing value of a and address of b
printf(“\n\nAfter fun 1\n“);
printf("a = %d b = %d\n“,a,b);
return 0;
}
void fun1(int aa, int * bb) //function definition
{
aa++;
*bb--;
printf("\n\nInside fun 1\n“);
printf("aa = %d bb = %d“,aa,bb);
}
Write the output of the program. Explain about the output of the program
Before fun 1: a = 5 b = 10
Inside fun 1: aa = 6 bb = 9
After fun 1 : a = 5 b = 9
#include <stdio.h>
int Factorial(int);
void main()
{
int n=4;
printf(“Factorial %d is %d“,n, Factorial(n));
}
int Factorial(int n)
{
if (n <= 1)
return 1; //when the n is 1 or less then it return to main.
else
return ( n * Factorial(n-1)); //here the factorial function call itself
}
Write down the output of the program
Factorial 4 is 24
Press any key to continue...
Question 4
Problem Solving question.
Write various type of functions appropriately (void, with input parameters and return parameters, i.e.
call by value and call by reference) to perform the following tasks:
a) Accept input such as matrix no. and marks for test1, test2 and final exam. Total marks for
test1 and test2 is 25. Total mark for final exam is 50.
b) Compute the sum of marks for test1, test2, and final exams for each student.
c) Compute grade obtained based on the following table:
Marks Grade
>= 80 A
>= 65 B
>= 50 C
>= 40 D
>= 25 E
< 25 F
Sample Output
int main ( )
{
char answer ;
float test1, test2, final, total;
char grade;
long int matrix;
do
{
void input (long int* id, float* t1, float* t2, float* last )
{
printf ("Insert matrix no. of the student\t\t\t: ");
scanf ("%ld", id);
printf ("Insert their marks for Test 1 (total of 25)\t: ");
scanf ("%f", t1);
printf ("Insert their marks for Test 2 (total of 25)\t: ");
scanf ("%f", t2);
printf ("Insert their marks for Final Exam (total of 50)\t: ");
scanf ("%f", last );
}
void sum_marks ( float t1, float t2, float last, float* sum )
{
*sum = t1+t2+last;
}
char compute_grade (float sum)
{
char grad;
if (sum >=80)
{
grad = 'A';
}
else if (sum >=65)
{
grad = 'B';
}
else if (sum >=50)
{
grad = 'C';
}
else if (sum >=40)
{
grad = 'D';
}
else if (sum >=25)
{
grad = 'E';
}
else if (sum <25)
{
grad = 'F';
}
return ( grad);
}