Functions in C Flashcards
What are functions in C?
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.
What is the syntax to define a function in C?
<return> myFunctionName (<parameter>) {
/* zero or more statements */
return <value>;
}
</value></parameter></return>
What is the requirement for the main function in a C program?
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.
Can C functions be overloaded?
No, C does not support function overloading; each function must have a unique name.
How do you define a function with no parameters?
int myFunctionName(void) {
/* zero or more statements */
return value;
}
What is a procedure in C?
A procedure is a function with no return value, defined using the void keyword.
void printMessage(char message[]) {
printf(“%s\n”, message);
}
Why are function prototypes necessary?
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 are function prototypes defined?
void functionName(parameterType1, parameterType2);
Where are function prototypes commonly stored?
In header (.h) files, such as <stdio.h> for standard input/output functions.</stdio.h>
What is the scope of a variable in C?
Variables exist only when they are in scope, defined by the curly brackets {}.
Why is using global variables discouraged?
It is considered bad practice due to potential unintended side effects. They should be avoided without valid justification.
What are static variables in C?
Static variables are initialized once when the function is first called and retain their value between calls.
How is a static variable declared and used?
declaration:
static int variableName = value;
usage example:
void printPageNumber(void) {
static int pageNum = 0;
printf(“Page: %d\n”, ++pageNum);
}
What topics are included in the summary of C functions?
Defining and naming
Parameters and return values
Prototypes
Standard C library functions
Scope
Static variables