Professional Documents
Culture Documents
Actual Parameters
Formal Parameters
The actual parameters are the parameters that are speficified in calling
function. The formal parameters are the parameters that are declared at
called function. When a function gets executed, the copy of actual
parameter values are copied into formal parameters.
Call by Value
Call by Reference
Call by value:
This method uses in-mode semantics. Changes made to formal
parameter do not get transmitted back to the caller. Any modifications to
the formal parameter variable inside the called function or method affect
only the separate storage location and will not be reflected in the actual
parameter in the calling environment. This method is also called as call
by value.
#include <stdio.h>
void func(int a, int b)
{
a += b;
printf("In func, a = %d b = %d\n", a, b);
}
int main(void)
{
int x = 5, y = 7;
// Passing parameters
func(x, y);
printf("In main, x = %d y = %d\n", x, y);
return 0;
}
Output:
In func, a = 12 b = 7
In main, x = 5 y = 7
Call by reference:
#include <stdio.h>
void swapnum(int* i, int* j)
{
int temp = *i;
*i = *j;
*j = temp;
}
int main(void)
{
int a = 10, b = 20;
// passing parameters
swapnum(&a, &b);
printf("a is %d and b is %d\n", a, b);
return 0;
}
Output:
a is 20 and b is 10
Threading:
What is a Thread?
A thread is a single sequence stream within in a process. Because
threads have some of the properties of processes, they are sometimes
called lightweight processes.
Why Multithreading?
Threads are popular way to improve application through parallelism. For
example, in a browser, multiple tabs can be different threads. MS word
uses multiple threads, one thread to format the text, other thread to
process inputs, etc.
Threads operate faster than processes due to following reasons:
1) Thread creation is much faster.
2) Context switching between threads is much faster.
3) Threads can be terminated easily
4) Communication between threads is faster.
A simpe example:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //Header file for sleep(). man 3 sleep for details.
#include <pthread.h>
// A normal C function that is executed as a thread
// when its name is specified in pthread_create()
void *myThreadFun(void *vargp)
{
sleep(1);
printf("Printing GeeksQuiz from Thread \n");
return NULL;
}
int main()
{
pthread_t thread_id;
printf("Before Thread\n");
pthread_create(&thread_id, NULL, myThreadFun, NULL);
pthread_join(thread_id, NULL);
printf("After Thread\n");
exit(0);
}
In main() we declare a variable called thread_id, which is of type
pthread_t, which is an integer used to identify the thread in the system.
After declaring thread_id, we call pthread_create() function to create a
thread.
pthread_create() takes 4 arguments.
The first argument is a pointer to thread_id which is set by this function.
The second argument specifies attributes. If the value is NULL, then
default attributes shall be used.
The third argument is name of function to be executed for the thread to
be created.
The fourth argument is used to pass arguments to the function,
myThreadFun.
The pthread_join() function for threads is the equivalent of wait() for
processes. A call to pthread_join blocks the calling thread until the
thread with identifier equal to the first argument terminates.
https://www.cs.utah.edu/~germain/PPS/Topics/C_Language/c_functions
.html
https://www.geeksforgeeks.org/parameter-passing-techniques-in-c-cpp/
http://www.csc.villanova.edu/~mdamian/threads/posixthreads.html
https://www.personal.kent.edu/
%7Ermuhamma/OpSystems/Myos/threads.htm
http://www.btechsmartclass.com/c_programming/C-Parameter-
Passing.html
https://www.geeksforgeeks.org/multithreading-c-2/