Functions Flashcards

1
Q

A function that has a return type other than void must

A

have a return statement

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

Calling a Function in the Same .c file

A

In C you may only call a function in a file after it has been declared (you can call it before it has
been defined, but only after it has been declared).

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

If the type of the data provided as an argument doesn’t match the type of the
parameter

A

it will perform an implicit type conversion of the argument to the type of the parameter
without telling you (if such a conversion is possible). If such a conversion is not possible, there
will a compiler error.

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

Order of the typecast in implicit conversion.

A

bool -> char -> short int -> int -> unsigned int -> long int -> unsigned long int -> long long int -> float -> double -> long double

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

rule of implicit conversion

A

Generally, it is ok to pass a numeric argument that
has a smaller bit-width than the type expected, but not vice versa.

passing a long int where an int is expected can be disastrous because the type conversion will be achieved
by removing some bits of the long int to make it fit into the memory space for an int which
potentially modifies the value being passed!

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

What should be known when Calling Functions in other C Files

A

the header of the function
being called must be provided as a function prototype within the file from which the function is being
called.

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

What is a function prototype

A

consists of just the header of the function, using the same syntax as for a function definition, followed by a semi-colon
also called a declaration of
a function (but not a definition).

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

Calling Functions in Libraries

A

you include header file (e.g # include <math .h>/ # include <stdio .h>)
we get access
to the functions defined in the math library. For example, the square root function in the math library
is called sqrt().

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

compiling programs with a call to a library

A

-l<library_name></library_name>

for math lib its -lm

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

How to access library function information on the terminal

A

Most C libraries are documented with manual pages or, more commonly man pages. These can
be accessed by the UNIX command man

try typing man sqrt on the LINUX command line.

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

difference between declaration and definition

A

Function definition: tells the compiler what it does.
it is the actual function ( header and block of code)

A function prototype or declaration: tells the compiler only that such a function is defined somewhere
else without actually defining it.

Function definition: actually allocates storage for the compiled code associated with the function.

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

What happens every time a function is called

A

each time a function is called, a
separate copy of the function’s parameters and local variables are created.
this information is stored on a stack

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

stack frame

A

The collection of information on the stack for a
a single function call is called a stack frame.

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

properties of a stack

A

stack grows from “high”
(greater address) memory locations to “lower”(smaller address) memory locations

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

how stacks work

A

information for the main function is loaded in the stack in a stack frame. When it calls another function all the information for that function is pushed onto the stack in another frame. When function is return, the stack frame is popped from the stack. Leaving the calling function on the top of the stack.

The CPU state is restored from the data in the stack frame for the calling function (now
the top stack frame), and execution is resumed at the return address stored there.

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

Calling a function

A