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.