Chap 5: Functions & Pointers Flashcards
Program execution begins with which function:
main()
is present in every C program. Calls other functions.
drives other function
Control flow when calling function calls a function:
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.
All function enjoy a state of equality?
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 to call a function? syntax..
functionName();
How to define a function?
FunctionName()
{
statement(s);
}
Order of defining and order of calling is how?
Not necessarily same. but for readability of program advised to keep it same
Recursion:
A program calling itself.
Where is defining done?
A program can be called from other function but cannot be defined in other function.
Types of function:
User-Defined
Or
Library functions
Why use functions?
- Reduce Retyping of code.
2. Allows separating code in modular functions, which makes it easier to design and understand and test.
Passing values between functions:
Syntax
Kernighan and Ritchie method:
fName( x, y, z)
int x,y,z;
OR
ANSI method:
fName( int x, int y, intz )
Formal and actual Arguments?
Rules:
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
Pass a, b, c to calsum() from main:
main() { a =4 ; b = 5; c = 6; sum = calsum( a, b, c); }
Return Statement:
to return a meaningful value back to the calling function.
On executing return the control is immediately sent to the calling function.
return (value);
What will happen?
return;
A garbage value is returned.
How to prevent function from returning any value?
using void keyword: void fName() { }
What will happen?
return(a,b);
return(x,12):
A function can return only one value hence they are both invalid.
What if the value of a formal argument is changed?
what happens to actual arguments value in the calling function?
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.
Default Scope of a variable?
It’s local to the function in which it is defined.
What does calling convention decide?
- The order in which the arguments are passed to the function.
- Which function(calling or called) performs clean Up of variables when control returns to called function.
When a funct call is encountered and the args are to be passed to a function, what possibilities exist?
- Args can be passed Left to right
or - R to L
The memory of function theory:
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.
Output? and explain it.
int a = 1
printf( “%d%d%d”, 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).
Standard calling convention:
Args are passed to function right to left.
Stack is cleaned up by the called function.