SlideShare a Scribd company logo
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
Ad

More Related Content

What's hot (20)

C programming session7
C programming  session7C programming  session7
C programming session7
Keroles karam khalil
 
C programming part2
C programming part2C programming part2
C programming part2
Keroles karam khalil
 
Automotive embedded systems part1 v1
Automotive embedded systems part1 v1Automotive embedded systems part1 v1
Automotive embedded systems part1 v1
Keroles karam khalil
 
Automotive embedded systems part6 v1
Automotive embedded systems part6 v1Automotive embedded systems part6 v1
Automotive embedded systems part6 v1
Keroles karam khalil
 
Automotive embedded systems part2 v1
Automotive embedded systems part2 v1Automotive embedded systems part2 v1
Automotive embedded systems part2 v1
Keroles karam khalil
 
Automotive embedded systems part8 v1
Automotive embedded systems part8 v1Automotive embedded systems part8 v1
Automotive embedded systems part8 v1
Keroles karam khalil
 
C programming session8
C programming  session8C programming  session8
C programming session8
Keroles karam khalil
 
C basics quiz part 1_solution
C basics quiz part 1_solutionC basics quiz part 1_solution
C basics quiz part 1_solution
Keroles karam khalil
 
Autosar software component
Autosar software componentAutosar software component
Autosar software component
Farzad Sadeghi
 
Autosar Basics hand book_v1
Autosar Basics  hand book_v1Autosar Basics  hand book_v1
Autosar Basics hand book_v1
Keroles karam khalil
 
Embedded C programming session10
Embedded C programming  session10Embedded C programming  session10
Embedded C programming session10
Keroles karam khalil
 
Linux Kernel Module - For NLKB
Linux Kernel Module - For NLKBLinux Kernel Module - For NLKB
Linux Kernel Module - For NLKB
shimosawa
 
UEFI Spec Version 2.4 Facilitates Secure Update
UEFI Spec Version 2.4 Facilitates Secure UpdateUEFI Spec Version 2.4 Facilitates Secure Update
UEFI Spec Version 2.4 Facilitates Secure Update
insydesoftware
 
Microcontroller part 1
Microcontroller part 1Microcontroller part 1
Microcontroller part 1
Keroles karam khalil
 
Automative basics v3
Automative basics v3Automative basics v3
Automative basics v3
Keroles karam khalil
 
Automotive embedded systems part7 v1
Automotive embedded systems part7 v1Automotive embedded systems part7 v1
Automotive embedded systems part7 v1
Keroles karam khalil
 
Intro to Embedded OS, RTOS and Communication Protocols
Intro to Embedded OS, RTOS and Communication ProtocolsIntro to Embedded OS, RTOS and Communication Protocols
Intro to Embedded OS, RTOS and Communication Protocols
Emertxe Information Technologies Pvt Ltd
 
Automotive embedded systems part6 v2
Automotive embedded systems part6 v2Automotive embedded systems part6 v2
Automotive embedded systems part6 v2
Keroles karam khalil
 
Introduction to Modern U-Boot
Introduction to Modern U-BootIntroduction to Modern U-Boot
Introduction to Modern U-Boot
GlobalLogic Ukraine
 
Misra c rules
Misra c rulesMisra c rules
Misra c rules
kiranyeligati
 
Automotive embedded systems part1 v1
Automotive embedded systems part1 v1Automotive embedded systems part1 v1
Automotive embedded systems part1 v1
Keroles karam khalil
 
Automotive embedded systems part6 v1
Automotive embedded systems part6 v1Automotive embedded systems part6 v1
Automotive embedded systems part6 v1
Keroles karam khalil
 
Automotive embedded systems part2 v1
Automotive embedded systems part2 v1Automotive embedded systems part2 v1
Automotive embedded systems part2 v1
Keroles karam khalil
 
Automotive embedded systems part8 v1
Automotive embedded systems part8 v1Automotive embedded systems part8 v1
Automotive embedded systems part8 v1
Keroles karam khalil
 
Autosar software component
Autosar software componentAutosar software component
Autosar software component
Farzad Sadeghi
 
Linux Kernel Module - For NLKB
Linux Kernel Module - For NLKBLinux Kernel Module - For NLKB
Linux Kernel Module - For NLKB
shimosawa
 
UEFI Spec Version 2.4 Facilitates Secure Update
UEFI Spec Version 2.4 Facilitates Secure UpdateUEFI Spec Version 2.4 Facilitates Secure Update
UEFI Spec Version 2.4 Facilitates Secure Update
insydesoftware
 
Automotive embedded systems part7 v1
Automotive embedded systems part7 v1Automotive embedded systems part7 v1
Automotive embedded systems part7 v1
Keroles karam khalil
 
Automotive embedded systems part6 v2
Automotive embedded systems part6 v2Automotive embedded systems part6 v2
Automotive embedded systems part6 v2
Keroles karam khalil
 

Viewers also liked (20)

C programming part4
C programming part4C programming part4
C programming part4
Keroles karam khalil
 
Notes part3
Notes part3Notes part3
Notes part3
Keroles karam khalil
 
Homework 3
Homework 3Homework 3
Homework 3
Keroles karam khalil
 
Homework 2 solution
Homework 2 solutionHomework 2 solution
Homework 2 solution
Keroles karam khalil
 
C programming session7
C programming  session7C programming  session7
C programming session7
Keroles karam khalil
 
Microcontroller part 4
Microcontroller part 4Microcontroller part 4
Microcontroller part 4
Keroles karam khalil
 
C programming part4
C programming part4C programming part4
C programming part4
Keroles karam khalil
 
Microcontroller part 2
Microcontroller part 2Microcontroller part 2
Microcontroller part 2
Keroles karam khalil
 
C programming first_session
C programming first_sessionC programming first_session
C programming first_session
Keroles karam khalil
 
K vector embedded_linux_workshop
K vector embedded_linux_workshopK vector embedded_linux_workshop
K vector embedded_linux_workshop
Keroles karam khalil
 
C programming session3
C programming  session3C programming  session3
C programming session3
Keroles karam khalil
 
C programming part2
C programming part2C programming part2
C programming part2
Keroles karam khalil
 
C programming session8
C programming  session8C programming  session8
C programming session8
Keroles karam khalil
 
Microcontroller part 3
Microcontroller part 3Microcontroller part 3
Microcontroller part 3
Keroles karam khalil
 
Microcontroller part 5
Microcontroller part 5Microcontroller part 5
Microcontroller part 5
Keroles karam khalil
 
C programming part2
C programming part2C programming part2
C programming part2
Keroles karam khalil
 
Microcontroller part 2
Microcontroller part 2Microcontroller part 2
Microcontroller part 2
Keroles karam khalil
 
Microcontroller part 7_v1
Microcontroller part 7_v1Microcontroller part 7_v1
Microcontroller part 7_v1
Keroles karam khalil
 
Microcontroller part 9_v1
Microcontroller part 9_v1Microcontroller part 9_v1
Microcontroller part 9_v1
Keroles karam khalil
 
Microcontroller part 3
Microcontroller part 3Microcontroller part 3
Microcontroller part 3
Keroles karam khalil
 
Ad

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++
Muhammad Hammad Waseem
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
Saranya saran
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT
TheVerse1
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
Sampath Kumar
 
c-Functions power point presentation on c functions
c-Functions power point presentation on c functionsc-Functions power point presentation on c functions
c-Functions power point presentation on c functions
10300PEDDIKISHOR
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 
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
TeshaleSiyum
 
functions _
functions                                 _functions                                 _
functions _
SwatiHans10
 
Function in c
Function in cFunction in c
Function in c
Raj Tandukar
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
Amarjith C K
 
Function
Function Function
Function
Kathmandu University
 
JNTUK python programming python unit 3.pptx
JNTUK python programming python unit 3.pptxJNTUK python programming python unit 3.pptx
JNTUK python programming python unit 3.pptx
Venkateswara Babu Ravipati
 
Function
FunctionFunction
Function
Rajat Patel
 
Functions in c
Functions in cFunctions in c
Functions in c
SunithaVesalpu
 
11 functions
11 functions11 functions
11 functions
Rohit Shrivastava
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
Mohammed Saleh
 
Detailed concept of function in c programming
Detailed concept of function  in c programmingDetailed concept of function  in c programming
Detailed concept of function in c programming
anjanasharma77573
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
Mehul Desai
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
UMA PARAMESWARI
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
Saranya saran
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT
TheVerse1
 
c-Functions power point presentation on c functions
c-Functions power point presentation on c functionsc-Functions power point presentation on c functions
c-Functions power point presentation on c functions
10300PEDDIKISHOR
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 
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
TeshaleSiyum
 
Detailed concept of function in c programming
Detailed concept of function  in c programmingDetailed concept of function  in c programming
Detailed concept of function in c programming
anjanasharma77573
 
Ad

More from Keroles karam khalil (14)

Quiz 9
Quiz 9Quiz 9
Quiz 9
Keroles karam khalil
 
C programming session10
C programming  session10C programming  session10
C programming session10
Keroles karam khalil
 
C programming session9 -
C programming  session9 -C programming  session9 -
C programming session9 -
Keroles karam khalil
 
Quiz 10
Quiz 10Quiz 10
Quiz 10
Keroles karam khalil
 
Homework 6
Homework 6Homework 6
Homework 6
Keroles karam khalil
 
Homework 5 solution
Homework 5 solutionHomework 5 solution
Homework 5 solution
Keroles karam khalil
 
Notes part7
Notes part7Notes part7
Notes part7
Keroles karam khalil
 
Homework 5
Homework 5Homework 5
Homework 5
Keroles karam khalil
 
Notes part6
Notes part6Notes part6
Notes part6
Keroles karam khalil
 
Homework 4 solution
Homework 4 solutionHomework 4 solution
Homework 4 solution
Keroles karam khalil
 
Notes part5
Notes part5Notes part5
Notes part5
Keroles karam khalil
 
Homework 4
Homework 4Homework 4
Homework 4
Keroles karam khalil
 
Homework 3 solution
Homework 3 solutionHomework 3 solution
Homework 3 solution
Keroles karam khalil
 
Session 5-exersice
Session 5-exersiceSession 5-exersice
Session 5-exersice
Keroles karam khalil
 

Recently uploaded (20)

YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 

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