Chap 5: Functions & Pointers Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Program execution begins with which function:

A

main()
is present in every C program. Calls other functions.
drives other function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Control flow when calling function calls a function:

A

The control from calling function is moved to called function and when its execution is completed control is sent back to calling function where it left.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

All function enjoy a state of equality?

A

Yes, except for the fact tht execution starts with main. otherwise, nobody is nobody’s boss, all are treated equal. anybody calls anyone. main can be called from somewhere else as well.
Call can be many times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how to call a function? syntax..

A

functionName();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to define a function?

A

FunctionName()
{
statement(s);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Order of defining and order of calling is how?

A

Not necessarily same. but for readability of program advised to keep it same

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Recursion:

A

A program calling itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Where is defining done?

A

A program can be called from other function but cannot be defined in other function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Types of function:

A

User-Defined
Or
Library functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why use functions?

A
  1. Reduce Retyping of code.

2. Allows separating code in modular functions, which makes it easier to design and understand and test.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Passing values between functions:

Syntax

A

Kernighan and Ritchie method:

fName( x, y, z)
int x,y,z;

OR

ANSI method:
fName( int x, int y, intz )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Formal and actual Arguments?

Rules:

A

Actual arguments are passed when we call the function.
Formal Arg are present with the function where its created.
type, order, number of actual and formal argument should always be the same.
They can have same names but are still treated differently as they are present in different functions.

Arguments aka Parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Pass a, b, c to calsum() from main:

A
main()
{
a =4 ; b = 5; c = 6;
sum = calsum( a, b, c);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Return Statement:

A

to return a meaningful value back to the calling function.
On executing return the control is immediately sent to the calling function.
return (value);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What will happen?

return;

A

A garbage value is returned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to prevent function from returning any value?

A
using void keyword:
void fName()
{  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What will happen?
return(a,b);
return(x,12):

A

A function can return only one value hence they are both invalid.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What if the value of a formal argument is changed?

what happens to actual arguments value in the calling function?

A

In calling function the value of actual argument does not change.
As a photocopy of values in actual args is made into formal arg. Actual args don’t physically move.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Default Scope of a variable?

A

It’s local to the function in which it is defined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does calling convention decide?

A
  1. The order in which the arguments are passed to the function.
  2. Which function(calling or called) performs clean Up of variables when control returns to called function.
21
Q

When a funct call is encountered and the args are to be passed to a function, what possibilities exist?

A
  1. Args can be passed Left to right
    or
  2. R to L
22
Q

The memory of function theory:

A

The formal args and the local vars defined inside a function are created at a place in memory called stack.
On returning the control, the stack is cleaned up.

23
Q

Output? and explain it.
int a = 1
printf( “%d%d%d”, a, ++a, a++ )

A

331
not 122, as C’s calling convention, is R to L.
So 1 goes through a++ (1 passed and then incremented to 2) first
then ++a ( 2 incremented to 3 , 3 passed)
and then a (a is now 3, 3 passed).
printf() collects the values in the order in which we have asked it to get them printed (not in the order in which they were passed).

24
Q

Standard calling convention:

A

Args are passed to function right to left.

Stack is cleaned up by the called function.

25
Q

math.h

Whats .h for? and other theory regarding it..

A

Whenever we call the library functions we must write their prototype before making a call, helps compiler to check whether the values being passed and returned are as per the prototype declaration.
But we don’t define them, just call them so we may not necessarily know the prototype. Hence library of functions is provided a set of ‘ .h ‘ files.

These contain prototypes.
Lib functions are divided in different groups and one file is provided for each group. Ex: math functions has ‘ math.h’

26
Q

Default return value:

As when prototypes were not used.

A

int

27
Q

Prototype Declaration:

A
Example:
float Fname( float );
 It says Fname receives float and returns float.
in main() its done in the body, but if other functions are calling Fname then just make one declaration at the top, outside all those functions.
28
Q

How to make a function return a value other than int?

A
To do so, it is necessary to explicitly mention so in calling function and called function.
While declaring:
float Fname( float x)
{                    
   return (y);
}
and prototype declaration in calling func.
29
Q

Calls by value?

A

Passing values of variables to the called function.

30
Q

What is an address?

A

location number of variable.

31
Q

Calls by reference:

A

Passing the location number of variable(address) to a function.

32
Q

%u

A

Format specifier for an unsigned integer.

Like address of variables is not associated with any sign so %u could be used for address.

33
Q

What is operator * ?

Syntax to use?

A
  • Value at address
  • Indirection Operator.
    *(address), *(&i)
    i = *(&i)
34
Q

int i = 3;

What 3 things does this tell to compiler?

A
  1. Reserve space in memory to hold int value.
  2. Name that location i.
  3. Store value 3 in that location.
35
Q

What is a pointer?

A

A pointer is a variable that contains address, and addresses are always a whole number so, pointers always contain whole number.

36
Q

what do u understand by
int *j;
char *ch;

A

well j and ch are pointers who will contain address (whole numbers).
Value at address contained in j is integer.
Value at address contained by ch is a char.

37
Q

Whats a pointer containing pointer?

A

that will be k
int i,*j,
k;
*j = i; k = &j;
so, i = *j, &i = *k, i = **k

38
Q

Consequences of call by reference:

2

A
  1. since we are sending the address to the function, the function will now be manipulating the actual arguments.
    also in functions, now use pointers to do operations, (value at address is to be manipulated not address)
  2. Allows us to return more than one value.
39
Q

Recursion: how’s it possible?

A

aka Circular definition, recursion is a process of defining something in terms of itself.
Successive INVOCATIONS of the same function are possible because the compiler keeps track of which invocation calls which.

40
Q

Data Structure:

A

Ways of organising the data.

41
Q

LIFO:

A

Last in First Out data structure is the one where the last item to get stored on the stack( Push operation ) is the first one to get out of it (Pop Operation).

42
Q

What do you mean by stack?

[not the working]

A

The stack is a data structure for implementing normal, recursive function.
It’s a Last In First Out (LIFO) data structure.

43
Q

Cdecl Calling Convetion:

A

called function clears the retunring value and return address, when control passed to calling function at return address it cleans the parameters and therefore entire stack.

44
Q

How is normal function implemented using Stack:

A

When calling F calls f, the values of parameters are pushed in the stack ( remember C using std calling convention, Right to left) followed by the return address ( address of statement right after).
Then the control is sent to f, during execution local variables get pushed on the stack. On encountering return, return value is popped and then the return address and the control goes back to F, before execution of next statement stack is cleared.

45
Q

How are values being pushed and popped even though we didn’t write any code for it?

A

The compiler on encountering a function call generates a code to push parameters and the address. lly it generates code to clear the stack when control returns from f to F.

46
Q

Blunder resulting stack is full:

A

Not using the base case in recursive Func.
It must include an if statement to force return won’t recursive call being executed otherwise it would keep pushing parameters n return address and u would fall in indefinite loop and stack would get full.

47
Q

Implementation of recursive function:

A

Whenever a recursive call is made the parameters and return address gets pushed on the stack. The stack gets unwound when called function returns control back.
- Thus during every recursive function call we are working with a fresh set of parameters.

48
Q

Benefit of adding function to the lib?

A

F added to the lib are first compiled and then added. so saves compilation time.

49
Q

Creating a new lib:

A

Its possible, make sure u make header file (.h) which contains the prototypes.