Sec4of C Function
Sec4of C Function
Output
ADvANtAGe OF FUNctIONs IN c
❖There are many advantages of functions :
1) Code Reusability : By creating functions in C, you can call it many times.
⮚Example Explained :
⮚myFunction() is the name of the function
⮚void means that the function does not have a return value.
⮚inside the function (the body), add code that defines what the function
should do
Parameters and Arguments
Ø
Void Function
Output
Int Function
Output
Function Declaration and Definition
Output
cAll A FUNctION
❖To call a function, write the function's name followed by two parentheses () and a
semicolon ;
cAll A FUNctION “cONt”
⮚Example : myFunction() is used to print a text (the action), when it is called:
cAll A FUNctION “cONt”
⮚Example : A function can be called multiple times:
cAll A FUNctION “cONt”
⮚If a user-defined function, such as myFunction() is declared after the main()
function,an error will occur:
⮚Example :
cAll A FUNctION “cONt” “sOlvING tHIs pROBlem”
⮚ function declaration above main(), and function definition below main().
⮚Example :
cAll BY vAlUe & cAll BY ReFeReNce IN c
❖There are two ways to pass value or data to function in C language:
call by value and call by reference.
cAll BY vAlUe IN c
❖In call by value, original value is not modified.
❖If you change the value of function parameter, it is changed for the current function
only.It will not change the value of variable inside the caller method such as main().
⮚example :
cAll BY ReFeReNce IN c
❖In call by reference, original value is modified because we pass reference (address).
❖address of the value is passed in the function, Hence, value changed inside the
function,it is reflected inside as well as outside the function.
⮚example :
c FUNctION OveRlOADING
❖function overloading, multiple functions can have the same name with
different type of parameters :
⮚Example :
int myFunction(int x)
float myFunction(float x)
double myFunction(double x, double y)
c- FUNctION OveRlOADING “cONt”
⮚Example : two functions that add numbers of different type:
c- FUNctION OveRlOADING “cONt”
⮚Instead of defining two functions that should do the same thing, it is better to
overload one.
⮚Example : overload the plusFunc function to work for both int and double:
c- RecURsION
❖Recursion is the technique of making a function call itself.
❖Example : recursion is used to add a range of numbers together by breaking it down
into the simple task of adding two numbers:
❖Example Explained :
❖When the sum() function is called, it adds parameter k to the sum of all numbers
smaller than k and returns the result. When k becomes 0, the function just returns 0.
c- RecURsION “cONt”
❖Solve of example : When running, the program follows these steps:
⮚10 + sum(9)
⮚10 + ( 9 + sum(8) )
⮚10 + ( 9 + ( 8 + sum(7) ) )
⮚10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
⮚10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0
❖Since the function does not call itself when k is 0, the program stops there and
returns
the result.
Try
Ø Write a function to calculate the sum of n even integers
starting from a given even integer.
Ø Write a function to find the absolute value the integer
parameter .
Ø Write a function to takes a real number as its argument
and return the sum of digits of this number.
Ø Write a function isdigit which should return the digit if
the given number is a digit and zero if not.
tHANKs Dr/Ghada Maher
Eng/ Hossam Medhat