0% found this document useful (0 votes)
9 views9 pages

ESC111 Tutorial 4

The document outlines the six golden rules of functions in programming, detailing how arguments are passed, type mismatches, and variable scope. It includes sample questions and code snippets to illustrate concepts such as function behavior, GCD calculation, and error identification in code. Additionally, it highlights common pitfalls and compiler error messages related to function usage.

Uploaded by

duvvurisamanvith
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)
9 views9 pages

ESC111 Tutorial 4

The document outlines the six golden rules of functions in programming, detailing how arguments are passed, type mismatches, and variable scope. It includes sample questions and code snippets to illustrate concepts such as function behavior, GCD calculation, and error identification in code. Additionally, it highlights common pitfalls and compiler error messages related to function usage.

Uploaded by

duvvurisamanvith
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

Tutorial Sheet 4

ESC111 – Fundamentals of Computing - I

The Six Golden Rules of Functions

1. RULE 1: When we give a variable as input, the value stored


inside that variable gets passed as an argument.
2. RULE 2: If we give an expression as input, the value generated
by that expression gets passed as argument. If that value is an
address (e.g. the expression may be &a) the address is passed.
3. RULE 3 (the type-mismatch rule): In case of a mismatch b/w
type of argument promised and type that is passed,
typecasting will be attempted. However, this may cause errors.
4. RULE 4 (the copy rule): All values passed to a function get
copied and stored in a fresh variable inside that function.
Modifying the copy does not modify the original variable.
5. RULE 5 (the return rule): Values returned by a function can be
used freely in any way values of that data-type could have
been used. However, make sure that the value suits the
operation you are performing.
a. If you are indexing an array with an int returned by a
function, verify that the integer is not negative or out of
bounds.
b. If taking the square root of a float returned by a function,
verify that the float is not negative.
6. RULE 6 (the address rule): Even though the clones may have

their own variable names without interfering, they use the same

memory address space. If one clone modifies a certain

memory location directly, all clones will see that change


Sample Questions to discuss

Q1. Describe the output/behavior of the following program:

#include <stdio.h>
int swap(int a, int b) {
int t = a;
a = b;
b = t;
printf("%d %d\n", a, b);
return 0;
}

int main() {
int a = 3;
int b = 10;
swap(b, a);
printf("%d %d\n", a, b);
return 0;
}

Output:
3 10
3 10
For first 3, 10- arguments are passed in opposite order(a and b are switched twice, once while
passing argument and once inside swap function before swapping. )
The value of a and b does not change inside main.

Q2. Complete the following blanks in given gcd() function, so that it


outputs the GCD (greatest common divisor) of two numbers:

int gcd(int a, int b) {


int ans = ((a < b) ? a : b);
while (.....) {
if (a % ans == 0 && .....) {
.....;
}
ans--;
}
return ans;

Ans is the variable we currently are checking is the GCD. It is set to the smaller of the two
numbers a and b as this is the highest possible factor or GCD to start checking with. The while
loop should contain ans>=1. The if statement should be a % ans == 0 && b % ans == 0,
within which we break out of the loop, and return ans outside.
Q3. Identify if the below snippets have any errors or not. If any,

correct the minimum possible code lines else write the output:

(a)

#include <stdio.h>

void introduction()
{
printf("Hi\n");
printf("I am Mr. C\n");
}

int main()
{
introduction();
return 0;
}

(b)

#include <stdio.h>

void square(float x);

int main()
{
float m, n;
//Enter some number for finding square
scanf("%f", &m);
n = square(m);
printf("\nSquare of the given number %f is %f", m, n);
}

void square(float x)
{
float p;
p = x * x;
return p;
}

(a) Is correct, prints


Hi
I am Mr. C
(b) The return type of square must be changed to float, to fix the program. Error will be
returned without changes.
Q4. Write the output of the following program. Give suitable
reasons for the same.
#include<stdio.h>
void change(int num)
{
printf("Before adding value inside function num=%d \n",num);
num=num+100;
printf("After adding value inside function num=%d \n", num);
}
int main()
{
int x=100;
printf("Before function call x=%d \n", x);
change(x);
printf("After function call x=%d \n", x);
return 0;
}
Output:
Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=100
#include <stdio.h>

int global_var = 5;

void function() {
static int static_var = 5;
int local_var = 5;

printf("Static variable: %d\n", static_var);


printf("Local variable: %d\n", local_var);
printf("Global variable: %d\n", global_var);

static_var++;
local_var++;
global_var++;
}

int main() {
function();
function();
function();

return 0;
}

Q5. Describe the output/behaviour of the following programs.


Give suitable reasons for the same.

(a)

#include <stdio.h>
int main()
{
printf("This is %d", main);
return 0;
}

This prints the address of the main function. NOTE: We don’t call the
function(notice the absence of parentheses).

(b)

#include <stdio.h>
int main()
{
printf("This is %d", main());
return 0;
}

This errors out due to calling the main() function infinitely many times.
Q6. Describe the output/behaviour of the following programs.
Give suitable reasons for the same(Using macros).

#include <stdio.h>
#define X 3

int main(){
if(!X)
printf("esc_quiz");
else
printf("esc_endsem");
return 0;
}

The X is replaced by 3, and “esc_endsem” is printed as the output.

Q7. Describe the output/behaviour of the following programs. Give

suitable reasons for the same(Sum of even and odd elements).

// C program to count number of even and odd elements in an

array. #include <stdio.h>

void CountingEvenOdd(int arr[], int arr_size)


{
int even_count = 0;
int odd_count = 0;

// loop to read all the values in the


array for (int i = 0; i < arr_size; i++) {

// checking if a number is completely


// divisible by 2
if (arr[i] & 1 == 1)
odd_count++;
else
even_count++;
}

printf("Number of even elements = %d \nNumber of odd "


"elements = %d",
even_count, odd_count);
}
int main()
{
int arr[] = { 2, 3, 4, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);

// Function Call
CountingEvenOdd(arr, n);
}

Some Pitfalls and recognizing compiler error messages

1. Give functions same number of arguments as promised. If you


mismatch in arguments, type-casting will take place – may
cause errors, loss of info.
2. Execution of a function stops immediately after any return
statement encountered. We can have multiple return
statements in a function. However, all of them must return the
same datatype as the return type of the function (for void
return type, and empty return statement i.e. return; should be
used.
3. When defining functions, you can name your input variables
anything you like (even if you have already used them inside
other functions like main()). Do not expect variables inside two
different functions to share values just because their names are
the same (unless it is a global variable).

You might also like