SlideShare a Scribd company logo
1 of 38
Download to read offline
C_Programming
Part 5
ENG. KEROLES SHENOUDA
1
Functions 2
3
The call a = calcm(5,6) works as follows:
1. Copies the values 5 and 6 to the variables x and y
2. Performs the internal calculations to calculate m
3. When executing the line (return m), the computer copies the value inside m and
return it to the line (a = calcm(5,6))
4. Copies the return value to (a) variable
Function Definition 4
Function Name: like variable, must have
no spaces and no special characters and
must starts
with letters
Input Parameters: supplied parameters
types and names. You can define any
number of
inputs; also you can define zero number of
inputs.
Return Type: the data type of the
function output, if the function has no
output use (void)
keyword.
Function Body: performs specific
function operation.
Return Statement: this statement tells
the computer that the function execution
is completed
and the required function output is ready
for the caller. The computer takes the
returned
value and supplies it to the caller.
5
Prototype
6
C functions aspects syntax
function definition
Return_type
function_name (arguments
list)
{ Body of function; }
function call
function_name (arguments
list);
function declaration
return_type function_name
(argument list);
7
8
The compiler gives an error at the
line printWelcome(); in the
main function, the error
state that “the function
printWelcome is undefined”.
Which means that the compiler
cannot locate the function before the
main, even if it is located after the
main?
9
Calculate the Factorial
 Write a program uses a function to calculate the factorial of any positive
number
10
11
Lab:Calculate the Minimum Value of any
Given Array
int calcMin(int values[], int n) ;
12
Important: calcMin function takes two parameters an array and (int) value containing
the array size. Know that function could not expect the given array size, you must supply
it by yourself.
13
Finding a Name in a Set of Names
int findName(char names[][14], int n, char name[]);
14
void main()
{
char name[14];
char names[5][14] = {"Alaa", "Osama", "Mamdouh",
"Samy", "Hossain"};
puts("Enter your first name:");
gets(name);
if(findName(names, 5, name)==1)
puts("Welcome");
else
puts("Goodby");
}
Difference between Passing Single
Values and Arrays
15
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.
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.
16
17
18
19
20
21
22
Write a program that produces
the following output:
23
What is memory layout of C-program ?
 when you run any C-program, its
executable image is loaded into RAM
 This memory layout is organized in
following fashion:
 Text segment
 Data segment
 Heap segment
 Stack segment
 Unmapped or reserved
24
Text segment
 Text segment contain executable instructions
of your C program, its also called code segment.
This is the machine language representation of
the program steps to be carried out, including
all functions making up the program, both user
defined and system
25
Data segment
 There are two sub section of this segment called initialized & uninitialized
data segment
 Initialized data:- It contains both static and global data that are initialized
with non-zero values.
 This segment can be further classified into read-only area and read-write area.
 For example : The global string defined by char string[ ] = “hello world” and a statement
like int count=1 outside the main (i.e. global) would be stored in initialized read-write
area. And a global statement like const int A=3 makes the variable ‘A’ read-only and to
be stored in initialized read-only area.
 Uninitialized data (bss segment):- Uninitialized data segment is also called BSS segment.
BSS stands for ‘Block Started by Symbol’ named after an ancient assembler operator.
Uninitialized data segment contains all global and static variables that are initialized to zero
or do not have explicit initialization in source code.
 For example : The global variable declared as int A would be stored in uninitialized data
segment. A statement like static int X=0 will also stored in this segment cause it
initialized with zero.
26
Heap segment
 The heap segment is area where dynamically allocated memory (allocated by
malloc(), calloc(), realloc() and new for C++) resides.
 When we allocate memory through dynamic allocation techniques(in simple
word, run time memory allocation), program acquire space from system and
process address space grows that’s why we saw upward arrow indication in
figure for Heap.
27
Stack segment
 The stack segment is area where local variables are stored. By saying local
variable means that all those variables which are declared in every function
including main( ) in your C program.
 When we call any function, stack frame is created and when function returns,
stack frame is destroyed including all local variables of that particular
function.
 Stack frame contain some data like return address, arguments passed to it,
local variables, and any other information needed by the invoked function.
 A “stack pointer (SP)” keeps track of stack by each push & pop operation
onto it, by adjusted stack pointer to next or previous address.
28
Local Variables 29
Above program have three functions each with
different scopes; each scope holds different
local variables. The variable a, b and c of main
function are inaccessible through myAdd or
myMull functions. The variables x, y and z of
myMull function are inaccessible through
myAdd or main functions, even if myAdd function
has the same variables names
Global Variables
 In the following program the variable
name is defined as a global variable, all
program
function can access this variable.
30
Static Variables
 Static variables are defined by the modifier static.
Static variables are initialized once in the
program life. For example if the variable (x) is
defined inside a function, the variable is
initialized only at first function call, further
function calls do not perform the initialization
step, this means that if the variable is modified by
any call the modification result remains
for the next call. Following example illustrate this
idea
31
Static Variables 32
Calling
Mechanism
33
Recursion
 Recursion is a situation happens
when a function calls itself directly
or indirectly.
34
Recursion
 Is recursion useful?
Recursion is an alternative way to
repeat the function execution with
the same or variant
parameters values. In another way
recursion is an indirect way to
make a loop; simply you
can convert any recursion to a
normal loop
35
Infinite Printing Loop
Recursion
 Is recursion useful?
Recursion is an alternative way to
repeat the function execution with
the same or variant
parameters values. In another way
recursion is an indirect way to
make a loop; simply you
can convert any recursion to a
normal loop
36
Finite Printing Loop
Follow Chapter 4/5:
Controlling Program
Flow
C PROGRAMMING FOR ENGINEERS, DR. MOHAMED SOBH
37The next Part we will talk about Variables Scope
References 38
 The Case for Learning C as Your First Programming Language
 A Tutorial on Data Representation
 std::printf, std::fprintf, std::sprintf, std::snprintf…..
 C Programming for Engineers, Dr. Mohamed Sobh
 C programming expert.
 fresh2refresh.com/c-programming
 Memory Layout of C Program

More Related Content

What's hot

Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programmingTejaswiB4
 
Computer programming(CP)
Computer programming(CP)Computer programming(CP)
Computer programming(CP)nmahi96
 
C language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageC language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageRakesh Roshan
 
Recursive Function
Recursive FunctionRecursive Function
Recursive FunctionHarsh Pathak
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in CSyed Mustafa
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to cHattori Sidek
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programmingRutvik Pensionwar
 
Basic c programming and explanation PPT1
Basic c programming and explanation PPT1Basic c programming and explanation PPT1
Basic c programming and explanation PPT1Rumman Ansari
 

What's hot (18)

Ch3 repetition
Ch3 repetitionCh3 repetition
Ch3 repetition
 
C language
C languageC language
C language
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
Fortran 90 Basics
Fortran 90 BasicsFortran 90 Basics
Fortran 90 Basics
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
C programming session10
C programming  session10C programming  session10
C programming session10
 
Computer programming(CP)
Computer programming(CP)Computer programming(CP)
Computer programming(CP)
 
C language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageC language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C language
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
[ITP - Lecture 07] Comments in C/C++
[ITP - Lecture 07] Comments in C/C++[ITP - Lecture 07] Comments in C/C++
[ITP - Lecture 07] Comments in C/C++
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
 
Learn C
Learn CLearn C
Learn C
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Basic c programming and explanation PPT1
Basic c programming and explanation PPT1Basic c programming and explanation PPT1
Basic c programming and explanation PPT1
 

Viewers also liked (20)

C programming part4
C programming part4C programming part4
C programming part4
 
Notes part3
Notes part3Notes part3
Notes part3
 
Homework 3
Homework 3Homework 3
Homework 3
 
Homework 2 solution
Homework 2 solutionHomework 2 solution
Homework 2 solution
 
Microcontroller part 1
Microcontroller part 1Microcontroller part 1
Microcontroller part 1
 
Microcontroller part 4
Microcontroller part 4Microcontroller part 4
Microcontroller part 4
 
Embedded C programming session10
Embedded C programming  session10Embedded C programming  session10
Embedded C programming session10
 
C programming part4
C programming part4C programming part4
C programming part4
 
Microcontroller part 2
Microcontroller part 2Microcontroller part 2
Microcontroller part 2
 
C programming first_session
C programming first_sessionC programming first_session
C programming first_session
 
K vector embedded_linux_workshop
K vector embedded_linux_workshopK vector embedded_linux_workshop
K vector embedded_linux_workshop
 
Automative basics v3
Automative basics v3Automative basics v3
Automative basics v3
 
C programming part2
C programming part2C programming part2
C programming part2
 
Microcontroller part 3
Microcontroller part 3Microcontroller part 3
Microcontroller part 3
 
Microcontroller part 5
Microcontroller part 5Microcontroller part 5
Microcontroller part 5
 
C programming part2
C programming part2C programming part2
C programming part2
 
Microcontroller part 2
Microcontroller part 2Microcontroller part 2
Microcontroller part 2
 
Microcontroller part 7_v1
Microcontroller part 7_v1Microcontroller part 7_v1
Microcontroller part 7_v1
 
Microcontroller part 1
Microcontroller part 1Microcontroller part 1
Microcontroller part 1
 
Microcontroller part 9_v1
Microcontroller part 9_v1Microcontroller part 9_v1
Microcontroller part 9_v1
 

Similar to C programming session5

Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPTTheVerse1
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfTeshaleSiyum
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdfTeshaleSiyum
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxSangeetaBorde3
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptxhara69
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 

Similar to C programming session5 (20)

[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 
Function in c
Function in cFunction in c
Function in c
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 
Function
Function Function
Function
 
Function
FunctionFunction
Function
 
Functions in c
Functions in cFunctions in c
Functions in c
 
11 functions
11 functions11 functions
11 functions
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Function in c
Function in cFunction in c
Function in c
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 

More from Keroles karam khalil (20)

C basics quiz part 1_solution
C basics quiz part 1_solutionC basics quiz part 1_solution
C basics quiz part 1_solution
 
Autosar Basics hand book_v1
Autosar Basics  hand book_v1Autosar Basics  hand book_v1
Autosar Basics hand book_v1
 
Automotive embedded systems part6 v2
Automotive embedded systems part6 v2Automotive embedded systems part6 v2
Automotive embedded systems part6 v2
 
Automotive embedded systems part5 v2
Automotive embedded systems part5 v2Automotive embedded systems part5 v2
Automotive embedded systems part5 v2
 
EMBEDDED C
EMBEDDED CEMBEDDED C
EMBEDDED C
 
Automotive embedded systems part7 v1
Automotive embedded systems part7 v1Automotive embedded systems part7 v1
Automotive embedded systems part7 v1
 
Automotive embedded systems part6 v1
Automotive embedded systems part6 v1Automotive embedded systems part6 v1
Automotive embedded systems part6 v1
 
Automotive embedded systems part5 v1
Automotive embedded systems part5 v1Automotive embedded systems part5 v1
Automotive embedded systems part5 v1
 
Automotive embedded systems part4 v1
Automotive embedded systems part4 v1Automotive embedded systems part4 v1
Automotive embedded systems part4 v1
 
Automotive embedded systems part3 v1
Automotive embedded systems part3 v1Automotive embedded systems part3 v1
Automotive embedded systems part3 v1
 
Automotive embedded systems part2 v1
Automotive embedded systems part2 v1Automotive embedded systems part2 v1
Automotive embedded systems part2 v1
 
Automotive embedded systems part1 v1
Automotive embedded systems part1 v1Automotive embedded systems part1 v1
Automotive embedded systems part1 v1
 
Automotive embedded systems part8 v1
Automotive embedded systems part8 v1Automotive embedded systems part8 v1
Automotive embedded systems part8 v1
 
Quiz 9
Quiz 9Quiz 9
Quiz 9
 
C programming session9 -
C programming  session9 -C programming  session9 -
C programming session9 -
 
Quiz 10
Quiz 10Quiz 10
Quiz 10
 
Homework 6
Homework 6Homework 6
Homework 6
 
Homework 5 solution
Homework 5 solutionHomework 5 solution
Homework 5 solution
 
Notes part7
Notes part7Notes part7
Notes part7
 
Homework 5
Homework 5Homework 5
Homework 5
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 

C programming session5

  • 3. 3 The call a = calcm(5,6) works as follows: 1. Copies the values 5 and 6 to the variables x and y 2. Performs the internal calculations to calculate m 3. When executing the line (return m), the computer copies the value inside m and return it to the line (a = calcm(5,6)) 4. Copies the return value to (a) variable
  • 4. Function Definition 4 Function Name: like variable, must have no spaces and no special characters and must starts with letters Input Parameters: supplied parameters types and names. You can define any number of inputs; also you can define zero number of inputs. Return Type: the data type of the function output, if the function has no output use (void) keyword. Function Body: performs specific function operation. Return Statement: this statement tells the computer that the function execution is completed and the required function output is ready for the caller. The computer takes the returned value and supplies it to the caller.
  • 6. 6
  • 7. C functions aspects syntax function definition Return_type function_name (arguments list) { Body of function; } function call function_name (arguments list); function declaration return_type function_name (argument list); 7
  • 8. 8 The compiler gives an error at the line printWelcome(); in the main function, the error state that “the function printWelcome is undefined”. Which means that the compiler cannot locate the function before the main, even if it is located after the main?
  • 9. 9
  • 10. Calculate the Factorial  Write a program uses a function to calculate the factorial of any positive number 10
  • 11. 11
  • 12. Lab:Calculate the Minimum Value of any Given Array int calcMin(int values[], int n) ; 12 Important: calcMin function takes two parameters an array and (int) value containing the array size. Know that function could not expect the given array size, you must supply it by yourself.
  • 13. 13
  • 14. Finding a Name in a Set of Names int findName(char names[][14], int n, char name[]); 14 void main() { char name[14]; char names[5][14] = {"Alaa", "Osama", "Mamdouh", "Samy", "Hossain"}; puts("Enter your first name:"); gets(name); if(findName(names, 5, name)==1) puts("Welcome"); else puts("Goodby"); }
  • 15. Difference between Passing Single Values and Arrays 15 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. 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.
  • 16. 16
  • 17. 17
  • 18. 18
  • 19. 19
  • 20. 20
  • 21. 21
  • 22. 22
  • 23. Write a program that produces the following output: 23
  • 24. What is memory layout of C-program ?  when you run any C-program, its executable image is loaded into RAM  This memory layout is organized in following fashion:  Text segment  Data segment  Heap segment  Stack segment  Unmapped or reserved 24
  • 25. Text segment  Text segment contain executable instructions of your C program, its also called code segment. This is the machine language representation of the program steps to be carried out, including all functions making up the program, both user defined and system 25
  • 26. Data segment  There are two sub section of this segment called initialized & uninitialized data segment  Initialized data:- It contains both static and global data that are initialized with non-zero values.  This segment can be further classified into read-only area and read-write area.  For example : The global string defined by char string[ ] = “hello world” and a statement like int count=1 outside the main (i.e. global) would be stored in initialized read-write area. And a global statement like const int A=3 makes the variable ‘A’ read-only and to be stored in initialized read-only area.  Uninitialized data (bss segment):- Uninitialized data segment is also called BSS segment. BSS stands for ‘Block Started by Symbol’ named after an ancient assembler operator. Uninitialized data segment contains all global and static variables that are initialized to zero or do not have explicit initialization in source code.  For example : The global variable declared as int A would be stored in uninitialized data segment. A statement like static int X=0 will also stored in this segment cause it initialized with zero. 26
  • 27. Heap segment  The heap segment is area where dynamically allocated memory (allocated by malloc(), calloc(), realloc() and new for C++) resides.  When we allocate memory through dynamic allocation techniques(in simple word, run time memory allocation), program acquire space from system and process address space grows that’s why we saw upward arrow indication in figure for Heap. 27
  • 28. Stack segment  The stack segment is area where local variables are stored. By saying local variable means that all those variables which are declared in every function including main( ) in your C program.  When we call any function, stack frame is created and when function returns, stack frame is destroyed including all local variables of that particular function.  Stack frame contain some data like return address, arguments passed to it, local variables, and any other information needed by the invoked function.  A “stack pointer (SP)” keeps track of stack by each push & pop operation onto it, by adjusted stack pointer to next or previous address. 28
  • 29. Local Variables 29 Above program have three functions each with different scopes; each scope holds different local variables. The variable a, b and c of main function are inaccessible through myAdd or myMull functions. The variables x, y and z of myMull function are inaccessible through myAdd or main functions, even if myAdd function has the same variables names
  • 30. Global Variables  In the following program the variable name is defined as a global variable, all program function can access this variable. 30
  • 31. Static Variables  Static variables are defined by the modifier static. Static variables are initialized once in the program life. For example if the variable (x) is defined inside a function, the variable is initialized only at first function call, further function calls do not perform the initialization step, this means that if the variable is modified by any call the modification result remains for the next call. Following example illustrate this idea 31
  • 34. Recursion  Recursion is a situation happens when a function calls itself directly or indirectly. 34
  • 35. Recursion  Is recursion useful? Recursion is an alternative way to repeat the function execution with the same or variant parameters values. In another way recursion is an indirect way to make a loop; simply you can convert any recursion to a normal loop 35 Infinite Printing Loop
  • 36. Recursion  Is recursion useful? Recursion is an alternative way to repeat the function execution with the same or variant parameters values. In another way recursion is an indirect way to make a loop; simply you can convert any recursion to a normal loop 36 Finite Printing Loop
  • 37. Follow Chapter 4/5: Controlling Program Flow C PROGRAMMING FOR ENGINEERS, DR. MOHAMED SOBH 37The next Part we will talk about Variables Scope
  • 38. References 38  The Case for Learning C as Your First Programming Language  A Tutorial on Data Representation  std::printf, std::fprintf, std::sprintf, std::snprintf…..  C Programming for Engineers, Dr. Mohamed Sobh  C programming expert.  fresh2refresh.com/c-programming  Memory Layout of C Program