Lecture 07
We will cover these skills:
Function in C/C++
Arguments & Parameters
User-defıned Functıons
Call by Value & Reference
Static variables in C/C++
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
void myFunction() { This body of myFunction ...... !
This body of main Function ...... !
printf("This body of myFunction ...... !");
}
int main() {
myFunction(); // where we call function
printf("\nThis body of main Function ...... !");
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.
Function with no arguments and a return value.
Function with arguments and no return value.
Function with arguments and with return value.
DR.KALAN 4
C /C++ Programming / Functıons
Arguments vs Parameters in C/C++ :
#include <stdio.h> Input as an integer
Function with arguments and with return value
int sum(int a, int b) // sum: Function definition. a and b are parameters
{
return a + b; // returns results of “a+b” to the function that
// called this function ( in this case “main” function)
}
Returns integer value as well
int main() {
int x = 5, y = 7, results; // local variable declaration
results = sum(x, y); // sum() is called with x & y as arguments
printf(“Sum is: %d", results); // Displaying the result
return 0;
}
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 sum(int a, int b)
{
printf(“%d”, a + b); // print results of “a+b”
}
Void returns nothing
int main() {
int x = 5, y = 7, results; // local variable declaration
sum(x, y); // sum() is called with x & y as arguments
return 0;
}
DR.KALAN 6
C /C++ Programming / Functıons
Arguments vs Parameters in C/C++ :
#include <stdio.h> Output
void myFunction(char name[]) { Hello Reza ! welcome to C/++ programming
printf("Hello %s\t! welcome to C/++ programming\n", name); Hello Ali ! welcome to C/++ programming
} Hello Yagmur ! welcome to C/++ programming
int main() {
myFunction("Reza");
myFunction("Ali");
myFunction("Yagmur");
return 0;
}
DR.KALAN 7
C /C++ Programming / Functıons
Example :
#include <stdio.h> Output
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
float toCelsius(float fahrenheit) { // Function to convert Fahrenheit to Celsius Fahrenheit: 32.00
return (5.0 / 9.0) * (fahrenheit - 32.0); Convert Fahrenheit to Celsius: 0.00
}
int main() {
float f_value = 32; // Set a fahrenheit value
float result = toCelsius(f_value); // Call the function with the fahrenheit value
printf("Fahrenheit: %.2f\n", f_value);
printf("Convert Fahrenheit to Celsius: %.2f\n", result);
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:
#include <iostream> Output
#include <math.h>
4.00
int main (){ 3.00
printf("\n%.2f", ceil(3.4)); 125.00
printf("\n%.2f", floor(3.4)); 5.00
printf("\n%.2f", pow(5, 3));
printf("\n%.2f", sqrt(25));
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
void swap(int x, int y){ After swap, value of a : 100
After swap, value of b : 200
int temp;
temp = x; //save the value of x
x = y; // put y into x
y = temp; // put temp into y
}
int main (){
int a = 100;
int b = 200;
swap(a, b);
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
return 0;
}
DR.KALAN 14
C /C++ Programming / Functıons
Call by Reference, Example :
#include <iostream> Output
void swap(int *x, int *y){ After swap, value of a : 200
After swap, value of b : 100
int temp;
temp = *x; //save the value at the address x
*x = *y; // put y into x
*y = temp; // put temp into y
}
int main (){
int a = 100;
int b = 200;
swap(&a, &b);
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
return 0;
}
DR.KALAN 15
C /C++ Programming
Static Variables in C/C++ :
Static variable only initialized one time #include <stdio.h> Output
void static_vars( void ){ i = 1, f = 0.100000, c = c
static int i = 0;
static float f = 0.0; i = 2, f = 0.200000, c = d
static char c = ‘b'; // static char c = '\0';
i++; i = 3, f = 0.300000, c = e
f += 0.1;
c++;
printf("i = %d, f = %f, c = %c\n", i, f, c);
}
int main(){
static_vars();
static_vars();
static_vars();
return 0;
}
DR.KALAN 16