Professional Documents
Culture Documents
REZA KALAN 1
C /C++ Programming / Functıons
Functions in C/C++ :
Function is a block of code which only runs when it is called.
Example 1:
#include <stdio.h> Output
int main() {
return 0;
}
DR.KALAN 2
C /C++ Programming/ Functıons
Functions in C/C++ :
A function is a block of code which only runs when it is called.
We can pass data, known as parameters, into a function.
Functions are used to perform certain actions, and they are important for reusing code: Define the code once,
and use it many times.
Syntax:
DR.KALAN 3
C /C++ Programming / Functıons
Arguments vs Parameters in C/C++ :
There are four types of user-defined functions divided on the basis of arguments :
Function with no arguments and no return value.
DR.KALAN 4
C /C++ Programming / Functıons
Arguments vs Parameters in C/C++ :
int main() {
int x = 5, y = 7, results; // local variable declaration
DR.KALAN 5
C /C++ Programming / Functıons
Arguments vs Parameters in C/C++ :
#include <stdio.h>
Function with arguments and no return value.
}
Void returns nothing
int main() {
int x = 5, y = 7, results; // local variable declaration
return 0;
}
DR.KALAN 6
C /C++ Programming / Functıons
Arguments vs Parameters in C/C++ :
int main() {
myFunction("Reza");
myFunction("Ali");
myFunction("Yagmur");
return 0;
}
DR.KALAN 7
C /C++ Programming / Functıons
Example :
void myFunction(char name[], int age) { Hello Reza. You are 37 years old.
Hello Ali. You are 22 years old.
printf("Hello %s. You are %d years old.\n", name, age); Hello Yagmur. You are 18 years old.
}
int main() {
myFunction("Reza", 37);
myFunction("Ali", 22);
myFunction("Yagmur", 18);
return 0;
}
DR.KALAN 8
C /C++ Programming / Functıons
Example :
#include <stdio.h> Output
int main() {
float f_value = 32; // Set a fahrenheit value
float result = toCelsius(f_value); // Call the function with the fahrenheit value
return 0;
}
DR.KALAN 9
C /C++ Programming / Functıons
Example :
#include <iostream> Output
using namespace std;
Enter a number: -3
int abs (int a){ The absolute value if 'a' is: 3
if (a<0)
return (-a);
else
return a;
}
int main(){
int a;
cout<<"Enter a number: ";
cin>>a;
cout<<“\n The absolute value of 'a' is: " << abs(a);
return 0;
}
DR.KALAN 10
C /C++ Programming / Functıons
Example :
Output
#include <iostream>
using namespace std; Enter a number: 4
The square of '4' is: 16
int sqr (int a){
return a*a;
}
int main(){
int a;
cout<<"Enter a number: ";
cin>>a;
cout<<"The square of '"<< a <<"' is: " << sqr(a);
return 0;
}
DR.KALAN 11
C /C++ Programming / Functıons
Math Functions in C/C++ :
There is also a list of math functions available, that allows you to perform mathematical tasks on numbers.
To use them, you must include the math.h header file in your program:
return 0;
}
DR.KALAN 12
C /C++ Programming / Functıons
Call Type & Description :
Call by Value
This method copies the actual value of an argument into the formal parameter of the function.
In this case, changes made to the parameter inside the function have no effect on the argument.
Call by Reference
This method copies the address of an argument into the formal parameter.
Inside the function, the address is used to access the actual argument used in the call.
This means that changes made to the parameter affect the argument.
DR.KALAN 13
C /C++ Programming / Functıons
Call by Value, Example :
#include <iostream> Output
DR.KALAN 14
C /C++ Programming / Functıons
Call by Reference, Example :
#include <iostream> Output
DR.KALAN 15
C /C++ Programming
Static Variables in C/C++ :
Static variable only initialized one time #include <stdio.h> Output
int main(){
static_vars();
static_vars();
static_vars();
return 0;
}
DR.KALAN 16