Chapter:5 Functions and Pointers
LUC 5.1 Calling a function
After this exercise, I got to know that : called functions after calling must end with ‘ ; ’ .
main() does not end with semi-colon as it is always the calling function. A function name
cannot contain space and other characters except for alphanumeric. Once all called
functions are over, the control is given back to the main. When the main() function runs
out of function calls, the program ends.
LUC 5.2 Correct way of calling a function by another called function.
A function gets called when its name ends with a semi-colon. A function is defined when its
name ends with a pair of braces with one or more statements in it.
Any function can be called by any function. Even main() can be called by any other
function. A function can be called any number of times. A function can also call itself . It is
known as recursion. A function cannot be defined inside another function.
While declaring any function , we declare it as –
return type <function_name> (acceptance type & quantity)
For eg,: int fact(int) tells us that the function name is fact , it returns integer type values
and accepts only one integer type value.
LUC 5.3 Passing values between functions
main() function user ke dwara input teen number a,b,c ko leta hai aur fir calsum()
function ko bulakar use de deta hai. calsum()function ke paas teen container hai . Vo ek
me ‘a’ , doosre me ‘b’ aur teesre mein ‘c’ ko rakhta hai , apna kaam karta hai aur fine
product ‘d’ main() function ko return kar deta hai.
We can pass any number of arguments to the called function . The type , order and number
of the actual arguments and the formal arguments must be same.
a,b,c are formal arguments. x,y,z are actual arguments.
Instead of ‘x , y , z’ we could also have used ‘a , b , c’ . Compiler will treat them as different
variables as they are used in different functions.
/* There is a mistake in the above program. The last statement should be return(d).
Parentheses should be included.*/
calsum(x,y,z)
int x,y,z ; should be represented as calsum(int x, int y, int z)
It is most commonly used in ANSI.
While using return statements , we can type alphabet , int or float in between the braces. If
nothing is written , then the function returns garbage.
A function can return only one value at a time. Therefore return(a,b) is an invalid
statement.
If we want that the called function should not return any value , then we must use the
keyword void b4 the name of the function .Like : void italy()
*****LUC 5.4 Calling Convention in C /* HUGE DOUBT .DID NOT UNDERSTAND WHY
SUCH OUTPUTS!!! */
In C , calling convention is from right to left.
PROGRAM OUTPUT
321
331
32
22
4341
4424
FUNCTION SLIDES P1
FUNCTION SLIDES P2
FUNCTION SLIDES P3
Functions should be called properly nahi to vo bura maan jaate hain. Like it should be
square (a) and not like square a.
RETURN DATA-TYPE | NAME OF THE FUNCTION | FUNCTION DECLARATION (parameter)
int square int(x)
The above statement is called the prototype declaration of the square() function.
If we don’t mention the return-data type , compiler assumes that the function will return a
value of the data type int.
For decimal numbers we must mention float square (float x) else we would get results
like 1.52 = 2.000000 and 2.52 = 6.000000.
Will be back to function calls after POINTERS...
POINTERS
LUC 5.4
The address of ‘i’ is an integer (of course unsigned therefore we used %u).
‘ & ’ is used to refer to a particular memory location where the variable is stored.
‘ * ’ is used to refer to the value stored in a particular memory location.
If we want to specify a variable that stores only the address of a variable , we specify it by –
int *j ;This declaration tells the compiler that j will be used to store the address of an
integer value. In other words, j points to an integer.
Other examples: int *alpha; | char *ch; | float *s;
Here alpha, ch and s have been declared as pointer variables i.e. variables capable of
holding addresses. Addresses are always whole numbers .Therefore pointers contain only
whole numbers. Here float *s; does not mean that ‘ s ’ will contain a floating value. It
means that it will store the address of a floating value
So now we can define POINTERS as variables that contain address and as address are
always in whole numbers, pointers always contain whole numbers
LUC 5.5
We got the above error because we did not specify what exactly are *j and **k .
Here is the correct program –
&i matlab-i ka address.j matlab-i ka address.*k matlab k ke andar –yani j- matlab-i ka
address. i matlab i ki value. *j matlab – j mein jo baitha hai uski value-yani i ki value. **k
matlab k ke andar jo baitha hai – uske andar jo baitha hai uski value- yani*j matlab – j mein
jo baitha hai uski value-yani i ki value.
Back to FUNCTION CALLS
LUC 5.6 Call by value
Incomplete... writing left S
LUC 5.8
Ghar pe ghus ke mare. Isliye badly hui shakal ke saath baahar aata hai.