0% found this document useful (0 votes)
8 views24 pages

Sec4of C Function

Uploaded by

Ibrahim Wael
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views24 pages

Sec4of C Function

Uploaded by

Ibrahim Wael
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

pROGRAmmING IN c Dr/Ghada Maher

sectION 4 (FUNctIONs) Eng/ Hossam Medhat


c- FUNctIONs
❖A function is a block of code which only runs when it is called.
❖The function in C- language is also known as procedure or subroutine
in other programming languages.
❖You can pass data, known as parameters, into a function.
❖Functions are used to perform certain actions, and they are important for
reusing code.
❖There are two types of function:
1) Standard Library Functions: Predefined in C ,such as ceil(x), cos(x), exp(x),
etc.
2) User-defined Function: Created by users.
Math Functions
Standard Library Functions: Predefined functions in C

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.

2) Code optimization : It makes the code optimized, we don't need to write


much code.

3) reduces complexity of a big program , So we don't need to write the same


code again and again.
c- FUNctIONs “cONt”
❖Create a Function :
⮚Syntax :
void myFunction() {
// code to be executed
}

⮚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

You might also like