Functions in C Flashcards

1
Q

What are functions in C?

A

Functions are uniquely named groups of program statements that accept zero or more parameters and return zero or one value to the calling code. They are not methods.

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

What is the syntax to define a function in C?

A

<return> myFunctionName (<parameter>) {
/* zero or more statements */
return <value>;
}
</value></parameter></return>

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

What is the requirement for the main function in a C program?

A

Every program must have a main function of the form:

int main() {
/* zero or more statements */
return 0;
}

The main function serves as the entry point of a C program.

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

Can C functions be overloaded?

A

No, C does not support function overloading; each function must have a unique name.

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

How do you define a function with no parameters?

A

int myFunctionName(void) {
/* zero or more statements */
return value;
}

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

What is a procedure in C?

A

A procedure is a function with no return value, defined using the void keyword.

void printMessage(char message[]) {
printf(“%s\n”, message);
}

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

Why are function prototypes necessary?

A

Function prototypes are required because:

The compiler needs to know the function’s signature before it is called.

Functions can appear in any order in the program file.

Functions may call each other.

Function code may exist in other files.

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

How are function prototypes defined?

A

void functionName(parameterType1, parameterType2);

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

Where are function prototypes commonly stored?

A

In header (.h) files, such as <stdio.h> for standard input/output functions.</stdio.h>

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

What is the scope of a variable in C?

A

Variables exist only when they are in scope, defined by the curly brackets {}.

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

Why is using global variables discouraged?

A

It is considered bad practice due to potential unintended side effects. They should be avoided without valid justification.

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

What are static variables in C?

A

Static variables are initialized once when the function is first called and retain their value between calls.

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

How is a static variable declared and used?

A

declaration:
static int variableName = value;

usage example:
void printPageNumber(void) {
static int pageNum = 0;
printf(“Page: %d\n”, ++pageNum);
}

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

What topics are included in the summary of C functions?

A

Defining and naming
Parameters and return values
Prototypes
Standard C library functions
Scope
Static variables

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